• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* POSIX module implementation */
3 
4 /* 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
6    functions are either unimplemented or implemented differently.  The source
7    assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8    of the compiler used.  Different compilers define their own feature
9    test macro, e.g. '_MSC_VER'. */
10 
11 
12 
13 #ifdef __APPLE__
14    /*
15     * Step 1 of support for weak-linking a number of symbols existing on
16     * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
17     * at the end of this file for more information.
18     */
19 #  pragma weak lchown
20 #  pragma weak statvfs
21 #  pragma weak fstatvfs
22 
23 #endif /* __APPLE__ */
24 
25 #define PY_SSIZE_T_CLEAN
26 
27 #include "Python.h"
28 #ifdef MS_WINDOWS
29    /* include <windows.h> early to avoid conflict with pycore_condvar.h:
30 
31         #define WIN32_LEAN_AND_MEAN
32         #include <windows.h>
33 
34       FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
35 #  include <windows.h>
36 #endif
37 
38 #include "pycore_ceval.h"     /* _PyEval_ReInitThreads() */
39 #include "pycore_pystate.h"   /* _PyRuntime */
40 #include "pythread.h"
41 #include "structmember.h"
42 #ifndef MS_WINDOWS
43 #  include "posixmodule.h"
44 #else
45 #  include "winreparse.h"
46 #endif
47 
48 /* On android API level 21, 'AT_EACCESS' is not declared although
49  * HAVE_FACCESSAT is defined. */
50 #ifdef __ANDROID__
51 #undef HAVE_FACCESSAT
52 #endif
53 
54 #include <stdio.h>  /* needed for ctermid() */
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 PyDoc_STRVAR(posix__doc__,
61 "This module provides access to operating system functionality that is\n\
62 standardized by the C Standard and the POSIX standard (a thinly\n\
63 disguised Unix interface).  Refer to the library manual and\n\
64 corresponding Unix manual entries for more information on calls.");
65 
66 
67 #ifdef HAVE_SYS_UIO_H
68 #include <sys/uio.h>
69 #endif
70 
71 #ifdef HAVE_SYS_SYSMACROS_H
72 /* GNU C Library: major(), minor(), makedev() */
73 #include <sys/sysmacros.h>
74 #endif
75 
76 #ifdef HAVE_SYS_TYPES_H
77 #include <sys/types.h>
78 #endif /* HAVE_SYS_TYPES_H */
79 
80 #ifdef HAVE_SYS_STAT_H
81 #include <sys/stat.h>
82 #endif /* HAVE_SYS_STAT_H */
83 
84 #ifdef HAVE_SYS_WAIT_H
85 #include <sys/wait.h>           /* For WNOHANG */
86 #endif
87 
88 #ifdef HAVE_SIGNAL_H
89 #include <signal.h>
90 #endif
91 
92 #ifdef HAVE_FCNTL_H
93 #include <fcntl.h>
94 #endif /* HAVE_FCNTL_H */
95 
96 #ifdef HAVE_GRP_H
97 #include <grp.h>
98 #endif
99 
100 #ifdef HAVE_SYSEXITS_H
101 #include <sysexits.h>
102 #endif /* HAVE_SYSEXITS_H */
103 
104 #ifdef HAVE_SYS_LOADAVG_H
105 #include <sys/loadavg.h>
106 #endif
107 
108 #ifdef HAVE_SYS_SENDFILE_H
109 #include <sys/sendfile.h>
110 #endif
111 
112 #if defined(__APPLE__)
113 #include <copyfile.h>
114 #endif
115 
116 #ifdef HAVE_SCHED_H
117 #include <sched.h>
118 #endif
119 
120 #ifdef HAVE_COPY_FILE_RANGE
121 #include <unistd.h>
122 #endif
123 
124 #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
125 #undef HAVE_SCHED_SETAFFINITY
126 #endif
127 
128 #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
129 #define USE_XATTRS
130 #endif
131 
132 #ifdef USE_XATTRS
133 #include <sys/xattr.h>
134 #endif
135 
136 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
137 #ifdef HAVE_SYS_SOCKET_H
138 #include <sys/socket.h>
139 #endif
140 #endif
141 
142 #ifdef HAVE_DLFCN_H
143 #include <dlfcn.h>
144 #endif
145 
146 #ifdef __hpux
147 #include <sys/mpctl.h>
148 #endif
149 
150 #if defined(__DragonFly__) || \
151     defined(__OpenBSD__)   || \
152     defined(__FreeBSD__)   || \
153     defined(__NetBSD__)    || \
154     defined(__APPLE__)
155 #include <sys/sysctl.h>
156 #endif
157 
158 #ifdef HAVE_LINUX_RANDOM_H
159 #  include <linux/random.h>
160 #endif
161 #ifdef HAVE_GETRANDOM_SYSCALL
162 #  include <sys/syscall.h>
163 #endif
164 
165 #if defined(MS_WINDOWS)
166 #  define TERMSIZE_USE_CONIO
167 #elif defined(HAVE_SYS_IOCTL_H)
168 #  include <sys/ioctl.h>
169 #  if defined(HAVE_TERMIOS_H)
170 #    include <termios.h>
171 #  endif
172 #  if defined(TIOCGWINSZ)
173 #    define TERMSIZE_USE_IOCTL
174 #  endif
175 #endif /* MS_WINDOWS */
176 
177 /* Various compilers have only certain posix functions */
178 /* XXX Gosh I wish these were all moved into pyconfig.h */
179 #if defined(__WATCOMC__) && !defined(__QNX__)           /* Watcom compiler */
180 #define HAVE_OPENDIR    1
181 #define HAVE_SYSTEM     1
182 #include <process.h>
183 #else
184 #ifdef _MSC_VER         /* Microsoft compiler */
185 #define HAVE_GETPPID    1
186 #define HAVE_GETLOGIN   1
187 #define HAVE_SPAWNV     1
188 #define HAVE_EXECV      1
189 #define HAVE_WSPAWNV    1
190 #define HAVE_WEXECV     1
191 #define HAVE_PIPE       1
192 #define HAVE_SYSTEM     1
193 #define HAVE_CWAIT      1
194 #define HAVE_FSYNC      1
195 #define fsync _commit
196 #else
197 /* Unix functions that the configure script doesn't check for */
198 #ifndef __VXWORKS__
199 #define HAVE_EXECV      1
200 #define HAVE_FORK       1
201 #if defined(__USLC__) && defined(__SCO_VERSION__)       /* SCO UDK Compiler */
202 #define HAVE_FORK1      1
203 #endif
204 #endif
205 #define HAVE_GETEGID    1
206 #define HAVE_GETEUID    1
207 #define HAVE_GETGID     1
208 #define HAVE_GETPPID    1
209 #define HAVE_GETUID     1
210 #define HAVE_KILL       1
211 #define HAVE_OPENDIR    1
212 #define HAVE_PIPE       1
213 #define HAVE_SYSTEM     1
214 #define HAVE_WAIT       1
215 #define HAVE_TTYNAME    1
216 #endif  /* _MSC_VER */
217 #endif  /* ! __WATCOMC__ || __QNX__ */
218 
219 
220 /*[clinic input]
221 # one of the few times we lie about this name!
222 module os
223 [clinic start generated code]*/
224 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
225 
226 #ifndef _MSC_VER
227 
228 #if defined(__sgi)&&_COMPILER_VERSION>=700
229 /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
230    (default) */
231 extern char        *ctermid_r(char *);
232 #endif
233 
234 #endif /* !_MSC_VER */
235 
236 #if defined(__VXWORKS__)
237 #include <vxCpuLib.h>
238 #include <rtpLib.h>
239 #include <wait.h>
240 #include <taskLib.h>
241 #ifndef _P_WAIT
242 #define _P_WAIT          0
243 #define _P_NOWAIT        1
244 #define _P_NOWAITO       1
245 #endif
246 #endif /* __VXWORKS__ */
247 
248 #ifdef HAVE_POSIX_SPAWN
249 #include <spawn.h>
250 #endif
251 
252 #ifdef HAVE_UTIME_H
253 #include <utime.h>
254 #endif /* HAVE_UTIME_H */
255 
256 #ifdef HAVE_SYS_UTIME_H
257 #include <sys/utime.h>
258 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
259 #endif /* HAVE_SYS_UTIME_H */
260 
261 #ifdef HAVE_SYS_TIMES_H
262 #include <sys/times.h>
263 #endif /* HAVE_SYS_TIMES_H */
264 
265 #ifdef HAVE_SYS_PARAM_H
266 #include <sys/param.h>
267 #endif /* HAVE_SYS_PARAM_H */
268 
269 #ifdef HAVE_SYS_UTSNAME_H
270 #include <sys/utsname.h>
271 #endif /* HAVE_SYS_UTSNAME_H */
272 
273 #ifdef HAVE_DIRENT_H
274 #include <dirent.h>
275 #define NAMLEN(dirent) strlen((dirent)->d_name)
276 #else
277 #if defined(__WATCOMC__) && !defined(__QNX__)
278 #include <direct.h>
279 #define NAMLEN(dirent) strlen((dirent)->d_name)
280 #else
281 #define dirent direct
282 #define NAMLEN(dirent) (dirent)->d_namlen
283 #endif
284 #ifdef HAVE_SYS_NDIR_H
285 #include <sys/ndir.h>
286 #endif
287 #ifdef HAVE_SYS_DIR_H
288 #include <sys/dir.h>
289 #endif
290 #ifdef HAVE_NDIR_H
291 #include <ndir.h>
292 #endif
293 #endif
294 
295 #ifdef _MSC_VER
296 #ifdef HAVE_DIRECT_H
297 #include <direct.h>
298 #endif
299 #ifdef HAVE_IO_H
300 #include <io.h>
301 #endif
302 #ifdef HAVE_PROCESS_H
303 #include <process.h>
304 #endif
305 #ifndef IO_REPARSE_TAG_SYMLINK
306 #define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
307 #endif
308 #ifndef IO_REPARSE_TAG_MOUNT_POINT
309 #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
310 #endif
311 #include "osdefs.h"
312 #include <malloc.h>
313 #include <windows.h>
314 #include <shellapi.h>   /* for ShellExecute() */
315 #include <lmcons.h>     /* for UNLEN */
316 #define HAVE_SYMLINK
317 #endif /* _MSC_VER */
318 
319 #ifndef MAXPATHLEN
320 #if defined(PATH_MAX) && PATH_MAX > 1024
321 #define MAXPATHLEN PATH_MAX
322 #else
323 #define MAXPATHLEN 1024
324 #endif
325 #endif /* MAXPATHLEN */
326 
327 #ifdef UNION_WAIT
328 /* Emulate some macros on systems that have a union instead of macros */
329 
330 #ifndef WIFEXITED
331 #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
332 #endif
333 
334 #ifndef WEXITSTATUS
335 #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
336 #endif
337 
338 #ifndef WTERMSIG
339 #define WTERMSIG(u_wait) ((u_wait).w_termsig)
340 #endif
341 
342 #define WAIT_TYPE union wait
343 #define WAIT_STATUS_INT(s) (s.w_status)
344 
345 #else /* !UNION_WAIT */
346 #define WAIT_TYPE int
347 #define WAIT_STATUS_INT(s) (s)
348 #endif /* UNION_WAIT */
349 
350 /* Don't use the "_r" form if we don't need it (also, won't have a
351    prototype for it, at least on Solaris -- maybe others as well?). */
352 #if defined(HAVE_CTERMID_R)
353 #define USE_CTERMID_R
354 #endif
355 
356 /* choose the appropriate stat and fstat functions and return structs */
357 #undef STAT
358 #undef FSTAT
359 #undef STRUCT_STAT
360 #ifdef MS_WINDOWS
361 #       define STAT win32_stat
362 #       define LSTAT win32_lstat
363 #       define FSTAT _Py_fstat_noraise
364 #       define STRUCT_STAT struct _Py_stat_struct
365 #else
366 #       define STAT stat
367 #       define LSTAT lstat
368 #       define FSTAT fstat
369 #       define STRUCT_STAT struct stat
370 #endif
371 
372 #if defined(MAJOR_IN_MKDEV)
373 #include <sys/mkdev.h>
374 #else
375 #if defined(MAJOR_IN_SYSMACROS)
376 #include <sys/sysmacros.h>
377 #endif
378 #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
379 #include <sys/mkdev.h>
380 #endif
381 #endif
382 
383 #ifdef MS_WINDOWS
384 #define INITFUNC PyInit_nt
385 #define MODNAME "nt"
386 #else
387 #define INITFUNC PyInit_posix
388 #define MODNAME "posix"
389 #endif
390 
391 #if defined(__sun)
392 /* Something to implement in autoconf, not present in autoconf 2.69 */
393 #define HAVE_STRUCT_STAT_ST_FSTYPE 1
394 #endif
395 
396 /* memfd_create is either defined in sys/mman.h or sys/memfd.h
397  * linux/memfd.h defines additional flags
398  */
399 #ifdef HAVE_SYS_MMAN_H
400 #include <sys/mman.h>
401 #endif
402 #ifdef HAVE_SYS_MEMFD_H
403 #include <sys/memfd.h>
404 #endif
405 #ifdef HAVE_LINUX_MEMFD_H
406 #include <linux/memfd.h>
407 #endif
408 
409 #ifdef _Py_MEMORY_SANITIZER
410 # include <sanitizer/msan_interface.h>
411 #endif
412 
413 #ifdef HAVE_FORK
414 static void
run_at_forkers(PyObject * lst,int reverse)415 run_at_forkers(PyObject *lst, int reverse)
416 {
417     Py_ssize_t i;
418     PyObject *cpy;
419 
420     if (lst != NULL) {
421         assert(PyList_CheckExact(lst));
422 
423         /* Use a list copy in case register_at_fork() is called from
424          * one of the callbacks.
425          */
426         cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
427         if (cpy == NULL)
428             PyErr_WriteUnraisable(lst);
429         else {
430             if (reverse)
431                 PyList_Reverse(cpy);
432             for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
433                 PyObject *func, *res;
434                 func = PyList_GET_ITEM(cpy, i);
435                 res = PyObject_CallObject(func, NULL);
436                 if (res == NULL)
437                     PyErr_WriteUnraisable(func);
438                 else
439                     Py_DECREF(res);
440             }
441             Py_DECREF(cpy);
442         }
443     }
444 }
445 
446 void
PyOS_BeforeFork(void)447 PyOS_BeforeFork(void)
448 {
449     run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
450 
451     _PyImport_AcquireLock();
452 }
453 
454 void
PyOS_AfterFork_Parent(void)455 PyOS_AfterFork_Parent(void)
456 {
457     if (_PyImport_ReleaseLock() <= 0)
458         Py_FatalError("failed releasing import lock after fork");
459 
460     run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
461 }
462 
463 void
PyOS_AfterFork_Child(void)464 PyOS_AfterFork_Child(void)
465 {
466     _PyRuntimeState *runtime = &_PyRuntime;
467     _PyGILState_Reinit(runtime);
468     _PyEval_ReInitThreads(runtime);
469     _PyImport_ReInitLock();
470     _PySignal_AfterFork();
471     _PyRuntimeState_ReInitThreads(runtime);
472     _PyInterpreterState_DeleteExceptMain(runtime);
473 
474     run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
475 }
476 
477 static int
register_at_forker(PyObject ** lst,PyObject * func)478 register_at_forker(PyObject **lst, PyObject *func)
479 {
480     if (func == NULL)  /* nothing to register? do nothing. */
481         return 0;
482     if (*lst == NULL) {
483         *lst = PyList_New(0);
484         if (*lst == NULL)
485             return -1;
486     }
487     return PyList_Append(*lst, func);
488 }
489 #endif
490 
491 /* Legacy wrapper */
492 void
PyOS_AfterFork(void)493 PyOS_AfterFork(void)
494 {
495 #ifdef HAVE_FORK
496     PyOS_AfterFork_Child();
497 #endif
498 }
499 
500 
501 #ifdef MS_WINDOWS
502 /* defined in fileutils.c */
503 void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
504 void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
505                                             ULONG, struct _Py_stat_struct *);
506 #endif
507 
508 
509 #ifndef MS_WINDOWS
510 PyObject *
_PyLong_FromUid(uid_t uid)511 _PyLong_FromUid(uid_t uid)
512 {
513     if (uid == (uid_t)-1)
514         return PyLong_FromLong(-1);
515     return PyLong_FromUnsignedLong(uid);
516 }
517 
518 PyObject *
_PyLong_FromGid(gid_t gid)519 _PyLong_FromGid(gid_t gid)
520 {
521     if (gid == (gid_t)-1)
522         return PyLong_FromLong(-1);
523     return PyLong_FromUnsignedLong(gid);
524 }
525 
526 int
_Py_Uid_Converter(PyObject * obj,void * p)527 _Py_Uid_Converter(PyObject *obj, void *p)
528 {
529     uid_t uid;
530     PyObject *index;
531     int overflow;
532     long result;
533     unsigned long uresult;
534 
535     index = PyNumber_Index(obj);
536     if (index == NULL) {
537         PyErr_Format(PyExc_TypeError,
538                      "uid should be integer, not %.200s",
539                      Py_TYPE(obj)->tp_name);
540         return 0;
541     }
542 
543     /*
544      * Handling uid_t is complicated for two reasons:
545      *  * Although uid_t is (always?) unsigned, it still
546      *    accepts -1.
547      *  * We don't know its size in advance--it may be
548      *    bigger than an int, or it may be smaller than
549      *    a long.
550      *
551      * So a bit of defensive programming is in order.
552      * Start with interpreting the value passed
553      * in as a signed long and see if it works.
554      */
555 
556     result = PyLong_AsLongAndOverflow(index, &overflow);
557 
558     if (!overflow) {
559         uid = (uid_t)result;
560 
561         if (result == -1) {
562             if (PyErr_Occurred())
563                 goto fail;
564             /* It's a legitimate -1, we're done. */
565             goto success;
566         }
567 
568         /* Any other negative number is disallowed. */
569         if (result < 0)
570             goto underflow;
571 
572         /* Ensure the value wasn't truncated. */
573         if (sizeof(uid_t) < sizeof(long) &&
574             (long)uid != result)
575             goto underflow;
576         goto success;
577     }
578 
579     if (overflow < 0)
580         goto underflow;
581 
582     /*
583      * Okay, the value overflowed a signed long.  If it
584      * fits in an *unsigned* long, it may still be okay,
585      * as uid_t may be unsigned long on this platform.
586      */
587     uresult = PyLong_AsUnsignedLong(index);
588     if (PyErr_Occurred()) {
589         if (PyErr_ExceptionMatches(PyExc_OverflowError))
590             goto overflow;
591         goto fail;
592     }
593 
594     uid = (uid_t)uresult;
595 
596     /*
597      * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
598      * but this value would get interpreted as (uid_t)-1  by chown
599      * and its siblings.   That's not what the user meant!  So we
600      * throw an overflow exception instead.   (We already
601      * handled a real -1 with PyLong_AsLongAndOverflow() above.)
602      */
603     if (uid == (uid_t)-1)
604         goto overflow;
605 
606     /* Ensure the value wasn't truncated. */
607     if (sizeof(uid_t) < sizeof(long) &&
608         (unsigned long)uid != uresult)
609         goto overflow;
610     /* fallthrough */
611 
612 success:
613     Py_DECREF(index);
614     *(uid_t *)p = uid;
615     return 1;
616 
617 underflow:
618     PyErr_SetString(PyExc_OverflowError,
619                     "uid is less than minimum");
620     goto fail;
621 
622 overflow:
623     PyErr_SetString(PyExc_OverflowError,
624                     "uid is greater than maximum");
625     /* fallthrough */
626 
627 fail:
628     Py_DECREF(index);
629     return 0;
630 }
631 
632 int
_Py_Gid_Converter(PyObject * obj,void * p)633 _Py_Gid_Converter(PyObject *obj, void *p)
634 {
635     gid_t gid;
636     PyObject *index;
637     int overflow;
638     long result;
639     unsigned long uresult;
640 
641     index = PyNumber_Index(obj);
642     if (index == NULL) {
643         PyErr_Format(PyExc_TypeError,
644                      "gid should be integer, not %.200s",
645                      Py_TYPE(obj)->tp_name);
646         return 0;
647     }
648 
649     /*
650      * Handling gid_t is complicated for two reasons:
651      *  * Although gid_t is (always?) unsigned, it still
652      *    accepts -1.
653      *  * We don't know its size in advance--it may be
654      *    bigger than an int, or it may be smaller than
655      *    a long.
656      *
657      * So a bit of defensive programming is in order.
658      * Start with interpreting the value passed
659      * in as a signed long and see if it works.
660      */
661 
662     result = PyLong_AsLongAndOverflow(index, &overflow);
663 
664     if (!overflow) {
665         gid = (gid_t)result;
666 
667         if (result == -1) {
668             if (PyErr_Occurred())
669                 goto fail;
670             /* It's a legitimate -1, we're done. */
671             goto success;
672         }
673 
674         /* Any other negative number is disallowed. */
675         if (result < 0) {
676             goto underflow;
677         }
678 
679         /* Ensure the value wasn't truncated. */
680         if (sizeof(gid_t) < sizeof(long) &&
681             (long)gid != result)
682             goto underflow;
683         goto success;
684     }
685 
686     if (overflow < 0)
687         goto underflow;
688 
689     /*
690      * Okay, the value overflowed a signed long.  If it
691      * fits in an *unsigned* long, it may still be okay,
692      * as gid_t may be unsigned long on this platform.
693      */
694     uresult = PyLong_AsUnsignedLong(index);
695     if (PyErr_Occurred()) {
696         if (PyErr_ExceptionMatches(PyExc_OverflowError))
697             goto overflow;
698         goto fail;
699     }
700 
701     gid = (gid_t)uresult;
702 
703     /*
704      * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
705      * but this value would get interpreted as (gid_t)-1  by chown
706      * and its siblings.   That's not what the user meant!  So we
707      * throw an overflow exception instead.   (We already
708      * handled a real -1 with PyLong_AsLongAndOverflow() above.)
709      */
710     if (gid == (gid_t)-1)
711         goto overflow;
712 
713     /* Ensure the value wasn't truncated. */
714     if (sizeof(gid_t) < sizeof(long) &&
715         (unsigned long)gid != uresult)
716         goto overflow;
717     /* fallthrough */
718 
719 success:
720     Py_DECREF(index);
721     *(gid_t *)p = gid;
722     return 1;
723 
724 underflow:
725     PyErr_SetString(PyExc_OverflowError,
726                     "gid is less than minimum");
727     goto fail;
728 
729 overflow:
730     PyErr_SetString(PyExc_OverflowError,
731                     "gid is greater than maximum");
732     /* fallthrough */
733 
734 fail:
735     Py_DECREF(index);
736     return 0;
737 }
738 #endif /* MS_WINDOWS */
739 
740 
741 #define _PyLong_FromDev PyLong_FromLongLong
742 
743 
744 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
745 static int
_Py_Dev_Converter(PyObject * obj,void * p)746 _Py_Dev_Converter(PyObject *obj, void *p)
747 {
748     *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
749     if (PyErr_Occurred())
750         return 0;
751     return 1;
752 }
753 #endif /* HAVE_MKNOD && HAVE_MAKEDEV */
754 
755 
756 #ifdef AT_FDCWD
757 /*
758  * Why the (int) cast?  Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
759  * without the int cast, the value gets interpreted as uint (4291925331),
760  * which doesn't play nicely with all the initializer lines in this file that
761  * look like this:
762  *      int dir_fd = DEFAULT_DIR_FD;
763  */
764 #define DEFAULT_DIR_FD (int)AT_FDCWD
765 #else
766 #define DEFAULT_DIR_FD (-100)
767 #endif
768 
769 static int
_fd_converter(PyObject * o,int * p)770 _fd_converter(PyObject *o, int *p)
771 {
772     int overflow;
773     long long_value;
774 
775     PyObject *index = PyNumber_Index(o);
776     if (index == NULL) {
777         return 0;
778     }
779 
780     assert(PyLong_Check(index));
781     long_value = PyLong_AsLongAndOverflow(index, &overflow);
782     Py_DECREF(index);
783     assert(!PyErr_Occurred());
784     if (overflow > 0 || long_value > INT_MAX) {
785         PyErr_SetString(PyExc_OverflowError,
786                         "fd is greater than maximum");
787         return 0;
788     }
789     if (overflow < 0 || long_value < INT_MIN) {
790         PyErr_SetString(PyExc_OverflowError,
791                         "fd is less than minimum");
792         return 0;
793     }
794 
795     *p = (int)long_value;
796     return 1;
797 }
798 
799 static int
dir_fd_converter(PyObject * o,void * p)800 dir_fd_converter(PyObject *o, void *p)
801 {
802     if (o == Py_None) {
803         *(int *)p = DEFAULT_DIR_FD;
804         return 1;
805     }
806     else if (PyIndex_Check(o)) {
807         return _fd_converter(o, (int *)p);
808     }
809     else {
810         PyErr_Format(PyExc_TypeError,
811                      "argument should be integer or None, not %.200s",
812                      Py_TYPE(o)->tp_name);
813         return 0;
814     }
815 }
816 
817 
818 /*
819  * A PyArg_ParseTuple "converter" function
820  * that handles filesystem paths in the manner
821  * preferred by the os module.
822  *
823  * path_converter accepts (Unicode) strings and their
824  * subclasses, and bytes and their subclasses.  What
825  * it does with the argument depends on the platform:
826  *
827  *   * On Windows, if we get a (Unicode) string we
828  *     extract the wchar_t * and return it; if we get
829  *     bytes we decode to wchar_t * and return that.
830  *
831  *   * On all other platforms, strings are encoded
832  *     to bytes using PyUnicode_FSConverter, then we
833  *     extract the char * from the bytes object and
834  *     return that.
835  *
836  * path_converter also optionally accepts signed
837  * integers (representing open file descriptors) instead
838  * of path strings.
839  *
840  * Input fields:
841  *   path.nullable
842  *     If nonzero, the path is permitted to be None.
843  *   path.allow_fd
844  *     If nonzero, the path is permitted to be a file handle
845  *     (a signed int) instead of a string.
846  *   path.function_name
847  *     If non-NULL, path_converter will use that as the name
848  *     of the function in error messages.
849  *     (If path.function_name is NULL it omits the function name.)
850  *   path.argument_name
851  *     If non-NULL, path_converter will use that as the name
852  *     of the parameter in error messages.
853  *     (If path.argument_name is NULL it uses "path".)
854  *
855  * Output fields:
856  *   path.wide
857  *     Points to the path if it was expressed as Unicode
858  *     and was not encoded.  (Only used on Windows.)
859  *   path.narrow
860  *     Points to the path if it was expressed as bytes,
861  *     or it was Unicode and was encoded to bytes. (On Windows,
862  *     is a non-zero integer if the path was expressed as bytes.
863  *     The type is deliberately incompatible to prevent misuse.)
864  *   path.fd
865  *     Contains a file descriptor if path.accept_fd was true
866  *     and the caller provided a signed integer instead of any
867  *     sort of string.
868  *
869  *     WARNING: if your "path" parameter is optional, and is
870  *     unspecified, path_converter will never get called.
871  *     So if you set allow_fd, you *MUST* initialize path.fd = -1
872  *     yourself!
873  *   path.length
874  *     The length of the path in characters, if specified as
875  *     a string.
876  *   path.object
877  *     The original object passed in (if get a PathLike object,
878  *     the result of PyOS_FSPath() is treated as the original object).
879  *     Own a reference to the object.
880  *   path.cleanup
881  *     For internal use only.  May point to a temporary object.
882  *     (Pay no attention to the man behind the curtain.)
883  *
884  *   At most one of path.wide or path.narrow will be non-NULL.
885  *   If path was None and path.nullable was set,
886  *     or if path was an integer and path.allow_fd was set,
887  *     both path.wide and path.narrow will be NULL
888  *     and path.length will be 0.
889  *
890  *   path_converter takes care to not write to the path_t
891  *   unless it's successful.  However it must reset the
892  *   "cleanup" field each time it's called.
893  *
894  * Use as follows:
895  *      path_t path;
896  *      memset(&path, 0, sizeof(path));
897  *      PyArg_ParseTuple(args, "O&", path_converter, &path);
898  *      // ... use values from path ...
899  *      path_cleanup(&path);
900  *
901  * (Note that if PyArg_Parse fails you don't need to call
902  * path_cleanup().  However it is safe to do so.)
903  */
904 typedef struct {
905     const char *function_name;
906     const char *argument_name;
907     int nullable;
908     int allow_fd;
909     const wchar_t *wide;
910 #ifdef MS_WINDOWS
911     BOOL narrow;
912 #else
913     const char *narrow;
914 #endif
915     int fd;
916     Py_ssize_t length;
917     PyObject *object;
918     PyObject *cleanup;
919 } path_t;
920 
921 #ifdef MS_WINDOWS
922 #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
923     {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
924 #else
925 #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
926     {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
927 #endif
928 
929 static void
path_cleanup(path_t * path)930 path_cleanup(path_t *path)
931 {
932     Py_CLEAR(path->object);
933     Py_CLEAR(path->cleanup);
934 }
935 
936 static int
path_converter(PyObject * o,void * p)937 path_converter(PyObject *o, void *p)
938 {
939     path_t *path = (path_t *)p;
940     PyObject *bytes = NULL;
941     Py_ssize_t length = 0;
942     int is_index, is_buffer, is_bytes, is_unicode;
943     const char *narrow;
944 #ifdef MS_WINDOWS
945     PyObject *wo = NULL;
946     const wchar_t *wide;
947 #endif
948 
949 #define FORMAT_EXCEPTION(exc, fmt) \
950     PyErr_Format(exc, "%s%s" fmt, \
951         path->function_name ? path->function_name : "", \
952         path->function_name ? ": "                : "", \
953         path->argument_name ? path->argument_name : "path")
954 
955     /* Py_CLEANUP_SUPPORTED support */
956     if (o == NULL) {
957         path_cleanup(path);
958         return 1;
959     }
960 
961     /* Ensure it's always safe to call path_cleanup(). */
962     path->object = path->cleanup = NULL;
963     /* path->object owns a reference to the original object */
964     Py_INCREF(o);
965 
966     if ((o == Py_None) && path->nullable) {
967         path->wide = NULL;
968 #ifdef MS_WINDOWS
969         path->narrow = FALSE;
970 #else
971         path->narrow = NULL;
972 #endif
973         path->fd = -1;
974         goto success_exit;
975     }
976 
977     /* Only call this here so that we don't treat the return value of
978        os.fspath() as an fd or buffer. */
979     is_index = path->allow_fd && PyIndex_Check(o);
980     is_buffer = PyObject_CheckBuffer(o);
981     is_bytes = PyBytes_Check(o);
982     is_unicode = PyUnicode_Check(o);
983 
984     if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
985         /* Inline PyOS_FSPath() for better error messages. */
986         _Py_IDENTIFIER(__fspath__);
987         PyObject *func, *res;
988 
989         func = _PyObject_LookupSpecial(o, &PyId___fspath__);
990         if (NULL == func) {
991             goto error_format;
992         }
993         res = _PyObject_CallNoArg(func);
994         Py_DECREF(func);
995         if (NULL == res) {
996             goto error_exit;
997         }
998         else if (PyUnicode_Check(res)) {
999             is_unicode = 1;
1000         }
1001         else if (PyBytes_Check(res)) {
1002             is_bytes = 1;
1003         }
1004         else {
1005             PyErr_Format(PyExc_TypeError,
1006                  "expected %.200s.__fspath__() to return str or bytes, "
1007                  "not %.200s", Py_TYPE(o)->tp_name,
1008                  Py_TYPE(res)->tp_name);
1009             Py_DECREF(res);
1010             goto error_exit;
1011         }
1012 
1013         /* still owns a reference to the original object */
1014         Py_DECREF(o);
1015         o = res;
1016     }
1017 
1018     if (is_unicode) {
1019 #ifdef MS_WINDOWS
1020         wide = PyUnicode_AsUnicodeAndSize(o, &length);
1021         if (!wide) {
1022             goto error_exit;
1023         }
1024         if (length > 32767) {
1025             FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
1026             goto error_exit;
1027         }
1028         if (wcslen(wide) != length) {
1029             FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
1030             goto error_exit;
1031         }
1032 
1033         path->wide = wide;
1034         path->narrow = FALSE;
1035         path->fd = -1;
1036         goto success_exit;
1037 #else
1038         if (!PyUnicode_FSConverter(o, &bytes)) {
1039             goto error_exit;
1040         }
1041 #endif
1042     }
1043     else if (is_bytes) {
1044         bytes = o;
1045         Py_INCREF(bytes);
1046     }
1047     else if (is_buffer) {
1048         /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
1049            after removing support of non-bytes buffer objects. */
1050         if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1051             "%s%s%s should be %s, not %.200s",
1052             path->function_name ? path->function_name : "",
1053             path->function_name ? ": "                : "",
1054             path->argument_name ? path->argument_name : "path",
1055             path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1056                                                "integer or None" :
1057             path->allow_fd ? "string, bytes, os.PathLike or integer" :
1058             path->nullable ? "string, bytes, os.PathLike or None" :
1059                              "string, bytes or os.PathLike",
1060             Py_TYPE(o)->tp_name)) {
1061             goto error_exit;
1062         }
1063         bytes = PyBytes_FromObject(o);
1064         if (!bytes) {
1065             goto error_exit;
1066         }
1067     }
1068     else if (is_index) {
1069         if (!_fd_converter(o, &path->fd)) {
1070             goto error_exit;
1071         }
1072         path->wide = NULL;
1073 #ifdef MS_WINDOWS
1074         path->narrow = FALSE;
1075 #else
1076         path->narrow = NULL;
1077 #endif
1078         goto success_exit;
1079     }
1080     else {
1081  error_format:
1082         PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1083             path->function_name ? path->function_name : "",
1084             path->function_name ? ": "                : "",
1085             path->argument_name ? path->argument_name : "path",
1086             path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1087                                                "integer or None" :
1088             path->allow_fd ? "string, bytes, os.PathLike or integer" :
1089             path->nullable ? "string, bytes, os.PathLike or None" :
1090                              "string, bytes or os.PathLike",
1091             Py_TYPE(o)->tp_name);
1092         goto error_exit;
1093     }
1094 
1095     length = PyBytes_GET_SIZE(bytes);
1096     narrow = PyBytes_AS_STRING(bytes);
1097     if ((size_t)length != strlen(narrow)) {
1098         FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
1099         goto error_exit;
1100     }
1101 
1102 #ifdef MS_WINDOWS
1103     wo = PyUnicode_DecodeFSDefaultAndSize(
1104         narrow,
1105         length
1106     );
1107     if (!wo) {
1108         goto error_exit;
1109     }
1110 
1111     wide = PyUnicode_AsUnicodeAndSize(wo, &length);
1112     if (!wide) {
1113         goto error_exit;
1114     }
1115     if (length > 32767) {
1116         FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
1117         goto error_exit;
1118     }
1119     if (wcslen(wide) != length) {
1120         FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
1121         goto error_exit;
1122     }
1123     path->wide = wide;
1124     path->narrow = TRUE;
1125     path->cleanup = wo;
1126     Py_DECREF(bytes);
1127 #else
1128     path->wide = NULL;
1129     path->narrow = narrow;
1130     if (bytes == o) {
1131         /* Still a reference owned by path->object, don't have to
1132            worry about path->narrow is used after free. */
1133         Py_DECREF(bytes);
1134     }
1135     else {
1136         path->cleanup = bytes;
1137     }
1138 #endif
1139     path->fd = -1;
1140 
1141  success_exit:
1142     path->length = length;
1143     path->object = o;
1144     return Py_CLEANUP_SUPPORTED;
1145 
1146  error_exit:
1147     Py_XDECREF(o);
1148     Py_XDECREF(bytes);
1149 #ifdef MS_WINDOWS
1150     Py_XDECREF(wo);
1151 #endif
1152     return 0;
1153 }
1154 
1155 static void
argument_unavailable_error(const char * function_name,const char * argument_name)1156 argument_unavailable_error(const char *function_name, const char *argument_name)
1157 {
1158     PyErr_Format(PyExc_NotImplementedError,
1159         "%s%s%s unavailable on this platform",
1160         (function_name != NULL) ? function_name : "",
1161         (function_name != NULL) ? ": ": "",
1162         argument_name);
1163 }
1164 
1165 static int
dir_fd_unavailable(PyObject * o,void * p)1166 dir_fd_unavailable(PyObject *o, void *p)
1167 {
1168     int dir_fd;
1169     if (!dir_fd_converter(o, &dir_fd))
1170         return 0;
1171     if (dir_fd != DEFAULT_DIR_FD) {
1172         argument_unavailable_error(NULL, "dir_fd");
1173         return 0;
1174     }
1175     *(int *)p = dir_fd;
1176     return 1;
1177 }
1178 
1179 static int
fd_specified(const char * function_name,int fd)1180 fd_specified(const char *function_name, int fd)
1181 {
1182     if (fd == -1)
1183         return 0;
1184 
1185     argument_unavailable_error(function_name, "fd");
1186     return 1;
1187 }
1188 
1189 static int
follow_symlinks_specified(const char * function_name,int follow_symlinks)1190 follow_symlinks_specified(const char *function_name, int follow_symlinks)
1191 {
1192     if (follow_symlinks)
1193         return 0;
1194 
1195     argument_unavailable_error(function_name, "follow_symlinks");
1196     return 1;
1197 }
1198 
1199 static int
path_and_dir_fd_invalid(const char * function_name,path_t * path,int dir_fd)1200 path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1201 {
1202     if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1203 #ifndef MS_WINDOWS
1204         && !path->narrow
1205 #endif
1206     ) {
1207         PyErr_Format(PyExc_ValueError,
1208                      "%s: can't specify dir_fd without matching path",
1209                      function_name);
1210         return 1;
1211     }
1212     return 0;
1213 }
1214 
1215 static int
dir_fd_and_fd_invalid(const char * function_name,int dir_fd,int fd)1216 dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1217 {
1218     if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1219         PyErr_Format(PyExc_ValueError,
1220                      "%s: can't specify both dir_fd and fd",
1221                      function_name);
1222         return 1;
1223     }
1224     return 0;
1225 }
1226 
1227 static int
fd_and_follow_symlinks_invalid(const char * function_name,int fd,int follow_symlinks)1228 fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1229                                int follow_symlinks)
1230 {
1231     if ((fd > 0) && (!follow_symlinks)) {
1232         PyErr_Format(PyExc_ValueError,
1233                      "%s: cannot use fd and follow_symlinks together",
1234                      function_name);
1235         return 1;
1236     }
1237     return 0;
1238 }
1239 
1240 static int
dir_fd_and_follow_symlinks_invalid(const char * function_name,int dir_fd,int follow_symlinks)1241 dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1242                                    int follow_symlinks)
1243 {
1244     if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1245         PyErr_Format(PyExc_ValueError,
1246                      "%s: cannot use dir_fd and follow_symlinks together",
1247                      function_name);
1248         return 1;
1249     }
1250     return 0;
1251 }
1252 
1253 #ifdef MS_WINDOWS
1254     typedef long long Py_off_t;
1255 #else
1256     typedef off_t Py_off_t;
1257 #endif
1258 
1259 static int
Py_off_t_converter(PyObject * arg,void * addr)1260 Py_off_t_converter(PyObject *arg, void *addr)
1261 {
1262 #ifdef HAVE_LARGEFILE_SUPPORT
1263     *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1264 #else
1265     *((Py_off_t *)addr) = PyLong_AsLong(arg);
1266 #endif
1267     if (PyErr_Occurred())
1268         return 0;
1269     return 1;
1270 }
1271 
1272 static PyObject *
PyLong_FromPy_off_t(Py_off_t offset)1273 PyLong_FromPy_off_t(Py_off_t offset)
1274 {
1275 #ifdef HAVE_LARGEFILE_SUPPORT
1276     return PyLong_FromLongLong(offset);
1277 #else
1278     return PyLong_FromLong(offset);
1279 #endif
1280 }
1281 
1282 #ifdef HAVE_SIGSET_T
1283 /* Convert an iterable of integers to a sigset.
1284    Return 1 on success, return 0 and raise an exception on error. */
1285 int
_Py_Sigset_Converter(PyObject * obj,void * addr)1286 _Py_Sigset_Converter(PyObject *obj, void *addr)
1287 {
1288     sigset_t *mask = (sigset_t *)addr;
1289     PyObject *iterator, *item;
1290     long signum;
1291     int overflow;
1292 
1293     // The extra parens suppress the unreachable-code warning with clang on MacOS
1294     if (sigemptyset(mask) < (0)) {
1295         /* Probably only if mask == NULL. */
1296         PyErr_SetFromErrno(PyExc_OSError);
1297         return 0;
1298     }
1299 
1300     iterator = PyObject_GetIter(obj);
1301     if (iterator == NULL) {
1302         return 0;
1303     }
1304 
1305     while ((item = PyIter_Next(iterator)) != NULL) {
1306         signum = PyLong_AsLongAndOverflow(item, &overflow);
1307         Py_DECREF(item);
1308         if (signum <= 0 || signum >= NSIG) {
1309             if (overflow || signum != -1 || !PyErr_Occurred()) {
1310                 PyErr_Format(PyExc_ValueError,
1311                              "signal number %ld out of range", signum);
1312             }
1313             goto error;
1314         }
1315         if (sigaddset(mask, (int)signum)) {
1316             if (errno != EINVAL) {
1317                 /* Probably impossible */
1318                 PyErr_SetFromErrno(PyExc_OSError);
1319                 goto error;
1320             }
1321             /* For backwards compatibility, allow idioms such as
1322              * `range(1, NSIG)` but warn about invalid signal numbers
1323              */
1324             const char msg[] =
1325                 "invalid signal number %ld, please use valid_signals()";
1326             if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1327                 goto error;
1328             }
1329         }
1330     }
1331     if (!PyErr_Occurred()) {
1332         Py_DECREF(iterator);
1333         return 1;
1334     }
1335 
1336 error:
1337     Py_DECREF(iterator);
1338     return 0;
1339 }
1340 #endif /* HAVE_SIGSET_T */
1341 
1342 #ifdef MS_WINDOWS
1343 
1344 static int
win32_get_reparse_tag(HANDLE reparse_point_handle,ULONG * reparse_tag)1345 win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
1346 {
1347     char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1348     _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
1349     DWORD n_bytes_returned;
1350 
1351     if (0 == DeviceIoControl(
1352         reparse_point_handle,
1353         FSCTL_GET_REPARSE_POINT,
1354         NULL, 0, /* in buffer */
1355         target_buffer, sizeof(target_buffer),
1356         &n_bytes_returned,
1357         NULL)) /* we're not using OVERLAPPED_IO */
1358         return FALSE;
1359 
1360     if (reparse_tag)
1361         *reparse_tag = rdb->ReparseTag;
1362 
1363     return TRUE;
1364 }
1365 
1366 #endif /* MS_WINDOWS */
1367 
1368 /* Return a dictionary corresponding to the POSIX environment table */
1369 #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1370 /* On Darwin/MacOSX a shared library or framework has no access to
1371 ** environ directly, we must obtain it with _NSGetEnviron(). See also
1372 ** man environ(7).
1373 */
1374 #include <crt_externs.h>
1375 #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
1376 extern char **environ;
1377 #endif /* !_MSC_VER */
1378 
1379 static PyObject *
convertenviron(void)1380 convertenviron(void)
1381 {
1382     PyObject *d;
1383 #ifdef MS_WINDOWS
1384     wchar_t **e;
1385 #else
1386     char **e;
1387 #endif
1388 
1389     d = PyDict_New();
1390     if (d == NULL)
1391         return NULL;
1392 #ifdef MS_WINDOWS
1393     /* _wenviron must be initialized in this way if the program is started
1394        through main() instead of wmain(). */
1395     _wgetenv(L"");
1396     e = _wenviron;
1397 #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1398     /* environ is not accessible as an extern in a shared object on OSX; use
1399        _NSGetEnviron to resolve it. The value changes if you add environment
1400        variables between calls to Py_Initialize, so don't cache the value. */
1401     e = *_NSGetEnviron();
1402 #else
1403     e = environ;
1404 #endif
1405     if (e == NULL)
1406         return d;
1407     for (; *e != NULL; e++) {
1408         PyObject *k;
1409         PyObject *v;
1410 #ifdef MS_WINDOWS
1411         const wchar_t *p = wcschr(*e, L'=');
1412 #else
1413         const char *p = strchr(*e, '=');
1414 #endif
1415         if (p == NULL)
1416             continue;
1417 #ifdef MS_WINDOWS
1418         k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1419 #else
1420         k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
1421 #endif
1422         if (k == NULL) {
1423             Py_DECREF(d);
1424             return NULL;
1425         }
1426 #ifdef MS_WINDOWS
1427         v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1428 #else
1429         v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
1430 #endif
1431         if (v == NULL) {
1432             Py_DECREF(k);
1433             Py_DECREF(d);
1434             return NULL;
1435         }
1436         if (PyDict_GetItemWithError(d, k) == NULL) {
1437             if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1438                 Py_DECREF(v);
1439                 Py_DECREF(k);
1440                 Py_DECREF(d);
1441                 return NULL;
1442             }
1443         }
1444         Py_DECREF(k);
1445         Py_DECREF(v);
1446     }
1447     return d;
1448 }
1449 
1450 /* Set a POSIX-specific error from errno, and return NULL */
1451 
1452 static PyObject *
posix_error(void)1453 posix_error(void)
1454 {
1455     return PyErr_SetFromErrno(PyExc_OSError);
1456 }
1457 
1458 #ifdef MS_WINDOWS
1459 static PyObject *
win32_error(const char * function,const char * filename)1460 win32_error(const char* function, const char* filename)
1461 {
1462     /* XXX We should pass the function name along in the future.
1463        (winreg.c also wants to pass the function name.)
1464        This would however require an additional param to the
1465        Windows error object, which is non-trivial.
1466     */
1467     errno = GetLastError();
1468     if (filename)
1469         return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1470     else
1471         return PyErr_SetFromWindowsErr(errno);
1472 }
1473 
1474 static PyObject *
win32_error_object_err(const char * function,PyObject * filename,DWORD err)1475 win32_error_object_err(const char* function, PyObject* filename, DWORD err)
1476 {
1477     /* XXX - see win32_error for comments on 'function' */
1478     if (filename)
1479         return PyErr_SetExcFromWindowsErrWithFilenameObject(
1480                     PyExc_OSError,
1481                     err,
1482                     filename);
1483     else
1484         return PyErr_SetFromWindowsErr(err);
1485 }
1486 
1487 static PyObject *
win32_error_object(const char * function,PyObject * filename)1488 win32_error_object(const char* function, PyObject* filename)
1489 {
1490     errno = GetLastError();
1491     return win32_error_object_err(function, filename, errno);
1492 }
1493 
1494 #endif /* MS_WINDOWS */
1495 
1496 static PyObject *
posix_path_object_error(PyObject * path)1497 posix_path_object_error(PyObject *path)
1498 {
1499     return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1500 }
1501 
1502 static PyObject *
path_object_error(PyObject * path)1503 path_object_error(PyObject *path)
1504 {
1505 #ifdef MS_WINDOWS
1506     return PyErr_SetExcFromWindowsErrWithFilenameObject(
1507                 PyExc_OSError, 0, path);
1508 #else
1509     return posix_path_object_error(path);
1510 #endif
1511 }
1512 
1513 static PyObject *
path_object_error2(PyObject * path,PyObject * path2)1514 path_object_error2(PyObject *path, PyObject *path2)
1515 {
1516 #ifdef MS_WINDOWS
1517     return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1518                 PyExc_OSError, 0, path, path2);
1519 #else
1520     return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1521 #endif
1522 }
1523 
1524 static PyObject *
path_error(path_t * path)1525 path_error(path_t *path)
1526 {
1527     return path_object_error(path->object);
1528 }
1529 
1530 static PyObject *
posix_path_error(path_t * path)1531 posix_path_error(path_t *path)
1532 {
1533     return posix_path_object_error(path->object);
1534 }
1535 
1536 static PyObject *
path_error2(path_t * path,path_t * path2)1537 path_error2(path_t *path, path_t *path2)
1538 {
1539     return path_object_error2(path->object, path2->object);
1540 }
1541 
1542 
1543 /* POSIX generic methods */
1544 
1545 static int
fildes_converter(PyObject * o,void * p)1546 fildes_converter(PyObject *o, void *p)
1547 {
1548     int fd;
1549     int *pointer = (int *)p;
1550     fd = PyObject_AsFileDescriptor(o);
1551     if (fd < 0)
1552         return 0;
1553     *pointer = fd;
1554     return 1;
1555 }
1556 
1557 static PyObject *
posix_fildes_fd(int fd,int (* func)(int))1558 posix_fildes_fd(int fd, int (*func)(int))
1559 {
1560     int res;
1561     int async_err = 0;
1562 
1563     do {
1564         Py_BEGIN_ALLOW_THREADS
1565         _Py_BEGIN_SUPPRESS_IPH
1566         res = (*func)(fd);
1567         _Py_END_SUPPRESS_IPH
1568         Py_END_ALLOW_THREADS
1569     } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1570     if (res != 0)
1571         return (!async_err) ? posix_error() : NULL;
1572     Py_RETURN_NONE;
1573 }
1574 
1575 
1576 #ifdef MS_WINDOWS
1577 /* This is a reimplementation of the C library's chdir function,
1578    but one that produces Win32 errors instead of DOS error codes.
1579    chdir is essentially a wrapper around SetCurrentDirectory; however,
1580    it also needs to set "magic" environment variables indicating
1581    the per-drive current directory, which are of the form =<drive>: */
1582 static BOOL __stdcall
win32_wchdir(LPCWSTR path)1583 win32_wchdir(LPCWSTR path)
1584 {
1585     wchar_t path_buf[MAX_PATH], *new_path = path_buf;
1586     int result;
1587     wchar_t env[4] = L"=x:";
1588 
1589     if(!SetCurrentDirectoryW(path))
1590         return FALSE;
1591     result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
1592     if (!result)
1593         return FALSE;
1594     if (result > Py_ARRAY_LENGTH(path_buf)) {
1595         new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
1596         if (!new_path) {
1597             SetLastError(ERROR_OUTOFMEMORY);
1598             return FALSE;
1599         }
1600         result = GetCurrentDirectoryW(result, new_path);
1601         if (!result) {
1602             PyMem_RawFree(new_path);
1603             return FALSE;
1604         }
1605     }
1606     int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1607                             wcsncmp(new_path, L"//", 2) == 0);
1608     if (!is_unc_like_path) {
1609         env[1] = new_path[0];
1610         result = SetEnvironmentVariableW(env, new_path);
1611     }
1612     if (new_path != path_buf)
1613         PyMem_RawFree(new_path);
1614     return result ? TRUE : FALSE;
1615 }
1616 #endif
1617 
1618 #ifdef MS_WINDOWS
1619 /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1620    - time stamps are restricted to second resolution
1621    - file modification times suffer from forth-and-back conversions between
1622      UTC and local time
1623    Therefore, we implement our own stat, based on the Win32 API directly.
1624 */
1625 #define HAVE_STAT_NSEC 1
1626 #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
1627 #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
1628 
1629 static void
find_data_to_file_info(WIN32_FIND_DATAW * pFileData,BY_HANDLE_FILE_INFORMATION * info,ULONG * reparse_tag)1630 find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1631                        BY_HANDLE_FILE_INFORMATION *info,
1632                        ULONG *reparse_tag)
1633 {
1634     memset(info, 0, sizeof(*info));
1635     info->dwFileAttributes = pFileData->dwFileAttributes;
1636     info->ftCreationTime   = pFileData->ftCreationTime;
1637     info->ftLastAccessTime = pFileData->ftLastAccessTime;
1638     info->ftLastWriteTime  = pFileData->ftLastWriteTime;
1639     info->nFileSizeHigh    = pFileData->nFileSizeHigh;
1640     info->nFileSizeLow     = pFileData->nFileSizeLow;
1641 /*  info->nNumberOfLinks   = 1; */
1642     if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1643         *reparse_tag = pFileData->dwReserved0;
1644     else
1645         *reparse_tag = 0;
1646 }
1647 
1648 static BOOL
attributes_from_dir(LPCWSTR pszFile,BY_HANDLE_FILE_INFORMATION * info,ULONG * reparse_tag)1649 attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
1650 {
1651     HANDLE hFindFile;
1652     WIN32_FIND_DATAW FileData;
1653     hFindFile = FindFirstFileW(pszFile, &FileData);
1654     if (hFindFile == INVALID_HANDLE_VALUE)
1655         return FALSE;
1656     FindClose(hFindFile);
1657     find_data_to_file_info(&FileData, info, reparse_tag);
1658     return TRUE;
1659 }
1660 
1661 static int
win32_xstat_impl(const wchar_t * path,struct _Py_stat_struct * result,BOOL traverse)1662 win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
1663                  BOOL traverse)
1664 {
1665     HANDLE hFile;
1666     BY_HANDLE_FILE_INFORMATION fileInfo;
1667     FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1668     DWORD fileType, error;
1669     BOOL isUnhandledTag = FALSE;
1670     int retval = 0;
1671 
1672     DWORD access = FILE_READ_ATTRIBUTES;
1673     DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1674     if (!traverse) {
1675         flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1676     }
1677 
1678     hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
1679     if (hFile == INVALID_HANDLE_VALUE) {
1680         /* Either the path doesn't exist, or the caller lacks access. */
1681         error = GetLastError();
1682         switch (error) {
1683         case ERROR_ACCESS_DENIED:     /* Cannot sync or read attributes. */
1684         case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1685             /* Try reading the parent directory. */
1686             if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1687                 /* Cannot read the parent directory. */
1688                 SetLastError(error);
1689                 return -1;
1690             }
1691             if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1692                 if (traverse ||
1693                     !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1694                     /* The stat call has to traverse but cannot, so fail. */
1695                     SetLastError(error);
1696                     return -1;
1697                 }
1698             }
1699             break;
1700 
1701         case ERROR_INVALID_PARAMETER:
1702             /* \\.\con requires read or write access. */
1703             hFile = CreateFileW(path, access | GENERIC_READ,
1704                         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1705                         OPEN_EXISTING, flags, NULL);
1706             if (hFile == INVALID_HANDLE_VALUE) {
1707                 SetLastError(error);
1708                 return -1;
1709             }
1710             break;
1711 
1712         case ERROR_CANT_ACCESS_FILE:
1713             /* bpo37834: open unhandled reparse points if traverse fails. */
1714             if (traverse) {
1715                 traverse = FALSE;
1716                 isUnhandledTag = TRUE;
1717                 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1718                             flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1719             }
1720             if (hFile == INVALID_HANDLE_VALUE) {
1721                 SetLastError(error);
1722                 return -1;
1723             }
1724             break;
1725 
1726         default:
1727             return -1;
1728         }
1729     }
1730 
1731     if (hFile != INVALID_HANDLE_VALUE) {
1732         /* Handle types other than files on disk. */
1733         fileType = GetFileType(hFile);
1734         if (fileType != FILE_TYPE_DISK) {
1735             if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1736                 retval = -1;
1737                 goto cleanup;
1738             }
1739             DWORD fileAttributes = GetFileAttributesW(path);
1740             memset(result, 0, sizeof(*result));
1741             if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1742                 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1743                 /* \\.\pipe\ or \\.\mailslot\ */
1744                 result->st_mode = _S_IFDIR;
1745             } else if (fileType == FILE_TYPE_CHAR) {
1746                 /* \\.\nul */
1747                 result->st_mode = _S_IFCHR;
1748             } else if (fileType == FILE_TYPE_PIPE) {
1749                 /* \\.\pipe\spam */
1750                 result->st_mode = _S_IFIFO;
1751             }
1752             /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1753             goto cleanup;
1754         }
1755 
1756         /* Query the reparse tag, and traverse a non-link. */
1757         if (!traverse) {
1758             if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1759                     &tagInfo, sizeof(tagInfo))) {
1760                 /* Allow devices that do not support FileAttributeTagInfo. */
1761                 switch (GetLastError()) {
1762                 case ERROR_INVALID_PARAMETER:
1763                 case ERROR_INVALID_FUNCTION:
1764                 case ERROR_NOT_SUPPORTED:
1765                     tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1766                     tagInfo.ReparseTag = 0;
1767                     break;
1768                 default:
1769                     retval = -1;
1770                     goto cleanup;
1771                 }
1772             } else if (tagInfo.FileAttributes &
1773                          FILE_ATTRIBUTE_REPARSE_POINT) {
1774                 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1775                     if (isUnhandledTag) {
1776                         /* Traversing previously failed for either this link
1777                            or its target. */
1778                         SetLastError(ERROR_CANT_ACCESS_FILE);
1779                         retval = -1;
1780                         goto cleanup;
1781                     }
1782                 /* Traverse a non-link, but not if traversing already failed
1783                    for an unhandled tag. */
1784                 } else if (!isUnhandledTag) {
1785                     CloseHandle(hFile);
1786                     return win32_xstat_impl(path, result, TRUE);
1787                 }
1788             }
1789         }
1790 
1791         if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1792             switch (GetLastError()) {
1793             case ERROR_INVALID_PARAMETER:
1794             case ERROR_INVALID_FUNCTION:
1795             case ERROR_NOT_SUPPORTED:
1796                 /* Volumes and physical disks are block devices, e.g.
1797                    \\.\C: and \\.\PhysicalDrive0. */
1798                 memset(result, 0, sizeof(*result));
1799                 result->st_mode = 0x6000; /* S_IFBLK */
1800                 goto cleanup;
1801             }
1802             retval = -1;
1803             goto cleanup;
1804         }
1805     }
1806 
1807     _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1808 
1809     if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1810         /* Fix the file execute permissions. This hack sets S_IEXEC if
1811            the filename has an extension that is commonly used by files
1812            that CreateProcessW can execute. A real implementation calls
1813            GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1814            AccessCheck to check for generic read, write, and execute
1815            access. */
1816         const wchar_t *fileExtension = wcsrchr(path, '.');
1817         if (fileExtension) {
1818             if (_wcsicmp(fileExtension, L".exe") == 0 ||
1819                 _wcsicmp(fileExtension, L".bat") == 0 ||
1820                 _wcsicmp(fileExtension, L".cmd") == 0 ||
1821                 _wcsicmp(fileExtension, L".com") == 0) {
1822                 result->st_mode |= 0111;
1823             }
1824         }
1825     }
1826 
1827 cleanup:
1828     if (hFile != INVALID_HANDLE_VALUE) {
1829         /* Preserve last error if we are failing */
1830         error = retval ? GetLastError() : 0;
1831         if (!CloseHandle(hFile)) {
1832             retval = -1;
1833         } else if (retval) {
1834             /* Restore last error */
1835             SetLastError(error);
1836         }
1837     }
1838 
1839     return retval;
1840 }
1841 
1842 static int
win32_xstat(const wchar_t * path,struct _Py_stat_struct * result,BOOL traverse)1843 win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
1844 {
1845     /* Protocol violation: we explicitly clear errno, instead of
1846        setting it to a POSIX error. Callers should use GetLastError. */
1847     int code = win32_xstat_impl(path, result, traverse);
1848     errno = 0;
1849     return code;
1850 }
1851 /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
1852 
1853    In Posix, stat automatically traverses symlinks and returns the stat
1854    structure for the target.  In Windows, the equivalent GetFileAttributes by
1855    default does not traverse symlinks and instead returns attributes for
1856    the symlink.
1857 
1858    Instead, we will open the file (which *does* traverse symlinks by default)
1859    and GetFileInformationByHandle(). */
1860 
1861 static int
win32_lstat(const wchar_t * path,struct _Py_stat_struct * result)1862 win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
1863 {
1864     return win32_xstat(path, result, FALSE);
1865 }
1866 
1867 static int
win32_stat(const wchar_t * path,struct _Py_stat_struct * result)1868 win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
1869 {
1870     return win32_xstat(path, result, TRUE);
1871 }
1872 
1873 #endif /* MS_WINDOWS */
1874 
1875 PyDoc_STRVAR(stat_result__doc__,
1876 "stat_result: Result from stat, fstat, or lstat.\n\n\
1877 This object may be accessed either as a tuple of\n\
1878   (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1879 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1880 \n\
1881 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1882 or st_flags, they are available as attributes only.\n\
1883 \n\
1884 See os.stat for more information.");
1885 
1886 static PyStructSequence_Field stat_result_fields[] = {
1887     {"st_mode",    "protection bits"},
1888     {"st_ino",     "inode"},
1889     {"st_dev",     "device"},
1890     {"st_nlink",   "number of hard links"},
1891     {"st_uid",     "user ID of owner"},
1892     {"st_gid",     "group ID of owner"},
1893     {"st_size",    "total size, in bytes"},
1894     /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1895     {NULL,   "integer time of last access"},
1896     {NULL,   "integer time of last modification"},
1897     {NULL,   "integer time of last change"},
1898     {"st_atime",   "time of last access"},
1899     {"st_mtime",   "time of last modification"},
1900     {"st_ctime",   "time of last change"},
1901     {"st_atime_ns",   "time of last access in nanoseconds"},
1902     {"st_mtime_ns",   "time of last modification in nanoseconds"},
1903     {"st_ctime_ns",   "time of last change in nanoseconds"},
1904 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1905     {"st_blksize", "blocksize for filesystem I/O"},
1906 #endif
1907 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1908     {"st_blocks",  "number of blocks allocated"},
1909 #endif
1910 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1911     {"st_rdev",    "device type (if inode device)"},
1912 #endif
1913 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1914     {"st_flags",   "user defined flags for file"},
1915 #endif
1916 #ifdef HAVE_STRUCT_STAT_ST_GEN
1917     {"st_gen",    "generation number"},
1918 #endif
1919 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1920     {"st_birthtime",   "time of creation"},
1921 #endif
1922 #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1923     {"st_file_attributes", "Windows file attribute bits"},
1924 #endif
1925 #ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1926     {"st_fstype",  "Type of filesystem"},
1927 #endif
1928 #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1929     {"st_reparse_tag", "Windows reparse tag"},
1930 #endif
1931     {0}
1932 };
1933 
1934 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1935 #define ST_BLKSIZE_IDX 16
1936 #else
1937 #define ST_BLKSIZE_IDX 15
1938 #endif
1939 
1940 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1941 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1942 #else
1943 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1944 #endif
1945 
1946 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1947 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1948 #else
1949 #define ST_RDEV_IDX ST_BLOCKS_IDX
1950 #endif
1951 
1952 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1953 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1954 #else
1955 #define ST_FLAGS_IDX ST_RDEV_IDX
1956 #endif
1957 
1958 #ifdef HAVE_STRUCT_STAT_ST_GEN
1959 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
1960 #else
1961 #define ST_GEN_IDX ST_FLAGS_IDX
1962 #endif
1963 
1964 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1965 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1966 #else
1967 #define ST_BIRTHTIME_IDX ST_GEN_IDX
1968 #endif
1969 
1970 #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1971 #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1972 #else
1973 #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1974 #endif
1975 
1976 #ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1977 #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1978 #else
1979 #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1980 #endif
1981 
1982 #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1983 #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
1984 #else
1985 #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
1986 #endif
1987 
1988 static PyStructSequence_Desc stat_result_desc = {
1989     "stat_result", /* name */
1990     stat_result__doc__, /* doc */
1991     stat_result_fields,
1992     10
1993 };
1994 
1995 PyDoc_STRVAR(statvfs_result__doc__,
1996 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
1997 This object may be accessed either as a tuple of\n\
1998   (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
1999 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
2000 \n\
2001 See os.statvfs for more information.");
2002 
2003 static PyStructSequence_Field statvfs_result_fields[] = {
2004     {"f_bsize",  },
2005     {"f_frsize", },
2006     {"f_blocks", },
2007     {"f_bfree",  },
2008     {"f_bavail", },
2009     {"f_files",  },
2010     {"f_ffree",  },
2011     {"f_favail", },
2012     {"f_flag",   },
2013     {"f_namemax",},
2014     {"f_fsid",   },
2015     {0}
2016 };
2017 
2018 static PyStructSequence_Desc statvfs_result_desc = {
2019     "statvfs_result", /* name */
2020     statvfs_result__doc__, /* doc */
2021     statvfs_result_fields,
2022     10
2023 };
2024 
2025 #if defined(HAVE_WAITID) && !defined(__APPLE__)
2026 PyDoc_STRVAR(waitid_result__doc__,
2027 "waitid_result: Result from waitid.\n\n\
2028 This object may be accessed either as a tuple of\n\
2029   (si_pid, si_uid, si_signo, si_status, si_code),\n\
2030 or via the attributes si_pid, si_uid, and so on.\n\
2031 \n\
2032 See os.waitid for more information.");
2033 
2034 static PyStructSequence_Field waitid_result_fields[] = {
2035     {"si_pid",  },
2036     {"si_uid", },
2037     {"si_signo", },
2038     {"si_status",  },
2039     {"si_code", },
2040     {0}
2041 };
2042 
2043 static PyStructSequence_Desc waitid_result_desc = {
2044     "waitid_result", /* name */
2045     waitid_result__doc__, /* doc */
2046     waitid_result_fields,
2047     5
2048 };
2049 static PyTypeObject* WaitidResultType;
2050 #endif
2051 
2052 static int initialized;
2053 static PyTypeObject* StatResultType;
2054 static PyTypeObject* StatVFSResultType;
2055 #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
2056 static PyTypeObject* SchedParamType;
2057 #endif
2058 static newfunc structseq_new;
2059 
2060 static PyObject *
statresult_new(PyTypeObject * type,PyObject * args,PyObject * kwds)2061 statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2062 {
2063     PyStructSequence *result;
2064     int i;
2065 
2066     result = (PyStructSequence*)structseq_new(type, args, kwds);
2067     if (!result)
2068         return NULL;
2069     /* If we have been initialized from a tuple,
2070        st_?time might be set to None. Initialize it
2071        from the int slots.  */
2072     for (i = 7; i <= 9; i++) {
2073         if (result->ob_item[i+3] == Py_None) {
2074             Py_DECREF(Py_None);
2075             Py_INCREF(result->ob_item[i]);
2076             result->ob_item[i+3] = result->ob_item[i];
2077         }
2078     }
2079     return (PyObject*)result;
2080 }
2081 
2082 
2083 static PyObject *billion = NULL;
2084 
2085 static void
fill_time(PyObject * v,int index,time_t sec,unsigned long nsec)2086 fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
2087 {
2088     PyObject *s = _PyLong_FromTime_t(sec);
2089     PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2090     PyObject *s_in_ns = NULL;
2091     PyObject *ns_total = NULL;
2092     PyObject *float_s = NULL;
2093 
2094     if (!(s && ns_fractional))
2095         goto exit;
2096 
2097     s_in_ns = PyNumber_Multiply(s, billion);
2098     if (!s_in_ns)
2099         goto exit;
2100 
2101     ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2102     if (!ns_total)
2103         goto exit;
2104 
2105     float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2106     if (!float_s) {
2107         goto exit;
2108     }
2109 
2110     PyStructSequence_SET_ITEM(v, index, s);
2111     PyStructSequence_SET_ITEM(v, index+3, float_s);
2112     PyStructSequence_SET_ITEM(v, index+6, ns_total);
2113     s = NULL;
2114     float_s = NULL;
2115     ns_total = NULL;
2116 exit:
2117     Py_XDECREF(s);
2118     Py_XDECREF(ns_fractional);
2119     Py_XDECREF(s_in_ns);
2120     Py_XDECREF(ns_total);
2121     Py_XDECREF(float_s);
2122 }
2123 
2124 /* pack a system stat C structure into the Python stat tuple
2125    (used by posix_stat() and posix_fstat()) */
2126 static PyObject*
_pystat_fromstructstat(STRUCT_STAT * st)2127 _pystat_fromstructstat(STRUCT_STAT *st)
2128 {
2129     unsigned long ansec, mnsec, cnsec;
2130     PyObject *v = PyStructSequence_New(StatResultType);
2131     if (v == NULL)
2132         return NULL;
2133 
2134     PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
2135     Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
2136     PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
2137 #ifdef MS_WINDOWS
2138     PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
2139 #else
2140     PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
2141 #endif
2142     PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
2143 #if defined(MS_WINDOWS)
2144     PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2145     PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2146 #else
2147     PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2148     PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2149 #endif
2150     Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2151     PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
2152 
2153 #if defined(HAVE_STAT_TV_NSEC)
2154     ansec = st->st_atim.tv_nsec;
2155     mnsec = st->st_mtim.tv_nsec;
2156     cnsec = st->st_ctim.tv_nsec;
2157 #elif defined(HAVE_STAT_TV_NSEC2)
2158     ansec = st->st_atimespec.tv_nsec;
2159     mnsec = st->st_mtimespec.tv_nsec;
2160     cnsec = st->st_ctimespec.tv_nsec;
2161 #elif defined(HAVE_STAT_NSEC)
2162     ansec = st->st_atime_nsec;
2163     mnsec = st->st_mtime_nsec;
2164     cnsec = st->st_ctime_nsec;
2165 #else
2166     ansec = mnsec = cnsec = 0;
2167 #endif
2168     fill_time(v, 7, st->st_atime, ansec);
2169     fill_time(v, 8, st->st_mtime, mnsec);
2170     fill_time(v, 9, st->st_ctime, cnsec);
2171 
2172 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2173     PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2174                               PyLong_FromLong((long)st->st_blksize));
2175 #endif
2176 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2177     PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2178                               PyLong_FromLong((long)st->st_blocks));
2179 #endif
2180 #ifdef HAVE_STRUCT_STAT_ST_RDEV
2181     PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2182                               PyLong_FromLong((long)st->st_rdev));
2183 #endif
2184 #ifdef HAVE_STRUCT_STAT_ST_GEN
2185     PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2186                               PyLong_FromLong((long)st->st_gen));
2187 #endif
2188 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2189     {
2190       PyObject *val;
2191       unsigned long bsec,bnsec;
2192       bsec = (long)st->st_birthtime;
2193 #ifdef HAVE_STAT_TV_NSEC2
2194       bnsec = st->st_birthtimespec.tv_nsec;
2195 #else
2196       bnsec = 0;
2197 #endif
2198       val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2199       PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2200                                 val);
2201     }
2202 #endif
2203 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
2204     PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2205                               PyLong_FromLong((long)st->st_flags));
2206 #endif
2207 #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2208     PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2209                               PyLong_FromUnsignedLong(st->st_file_attributes));
2210 #endif
2211 #ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2212    PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2213                               PyUnicode_FromString(st->st_fstype));
2214 #endif
2215 #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2216     PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2217                               PyLong_FromUnsignedLong(st->st_reparse_tag));
2218 #endif
2219 
2220     if (PyErr_Occurred()) {
2221         Py_DECREF(v);
2222         return NULL;
2223     }
2224 
2225     return v;
2226 }
2227 
2228 /* POSIX methods */
2229 
2230 
2231 static PyObject *
posix_do_stat(const char * function_name,path_t * path,int dir_fd,int follow_symlinks)2232 posix_do_stat(const char *function_name, path_t *path,
2233               int dir_fd, int follow_symlinks)
2234 {
2235     STRUCT_STAT st;
2236     int result;
2237 
2238 #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2239     if (follow_symlinks_specified(function_name, follow_symlinks))
2240         return NULL;
2241 #endif
2242 
2243     if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2244         dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2245         fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2246         return NULL;
2247 
2248     Py_BEGIN_ALLOW_THREADS
2249     if (path->fd != -1)
2250         result = FSTAT(path->fd, &st);
2251 #ifdef MS_WINDOWS
2252     else if (follow_symlinks)
2253         result = win32_stat(path->wide, &st);
2254     else
2255         result = win32_lstat(path->wide, &st);
2256 #else
2257     else
2258 #if defined(HAVE_LSTAT)
2259     if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2260         result = LSTAT(path->narrow, &st);
2261     else
2262 #endif /* HAVE_LSTAT */
2263 #ifdef HAVE_FSTATAT
2264     if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2265         result = fstatat(dir_fd, path->narrow, &st,
2266                          follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2267     else
2268 #endif /* HAVE_FSTATAT */
2269         result = STAT(path->narrow, &st);
2270 #endif /* MS_WINDOWS */
2271     Py_END_ALLOW_THREADS
2272 
2273     if (result != 0) {
2274         return path_error(path);
2275     }
2276 
2277     return _pystat_fromstructstat(&st);
2278 }
2279 
2280 /*[python input]
2281 
2282 for s in """
2283 
2284 FACCESSAT
2285 FCHMODAT
2286 FCHOWNAT
2287 FSTATAT
2288 LINKAT
2289 MKDIRAT
2290 MKFIFOAT
2291 MKNODAT
2292 OPENAT
2293 READLINKAT
2294 SYMLINKAT
2295 UNLINKAT
2296 
2297 """.strip().split():
2298     s = s.strip()
2299     print("""
2300 #ifdef HAVE_{s}
2301     #define {s}_DIR_FD_CONVERTER dir_fd_converter
2302 #else
2303     #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
2304 #endif
2305 """.rstrip().format(s=s))
2306 
2307 for s in """
2308 
2309 FCHDIR
2310 FCHMOD
2311 FCHOWN
2312 FDOPENDIR
2313 FEXECVE
2314 FPATHCONF
2315 FSTATVFS
2316 FTRUNCATE
2317 
2318 """.strip().split():
2319     s = s.strip()
2320     print("""
2321 #ifdef HAVE_{s}
2322     #define PATH_HAVE_{s} 1
2323 #else
2324     #define PATH_HAVE_{s} 0
2325 #endif
2326 
2327 """.rstrip().format(s=s))
2328 [python start generated code]*/
2329 
2330 #ifdef HAVE_FACCESSAT
2331     #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2332 #else
2333     #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2334 #endif
2335 
2336 #ifdef HAVE_FCHMODAT
2337     #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2338 #else
2339     #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2340 #endif
2341 
2342 #ifdef HAVE_FCHOWNAT
2343     #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2344 #else
2345     #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2346 #endif
2347 
2348 #ifdef HAVE_FSTATAT
2349     #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2350 #else
2351     #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2352 #endif
2353 
2354 #ifdef HAVE_LINKAT
2355     #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2356 #else
2357     #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2358 #endif
2359 
2360 #ifdef HAVE_MKDIRAT
2361     #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2362 #else
2363     #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2364 #endif
2365 
2366 #ifdef HAVE_MKFIFOAT
2367     #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2368 #else
2369     #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2370 #endif
2371 
2372 #ifdef HAVE_MKNODAT
2373     #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2374 #else
2375     #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2376 #endif
2377 
2378 #ifdef HAVE_OPENAT
2379     #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2380 #else
2381     #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2382 #endif
2383 
2384 #ifdef HAVE_READLINKAT
2385     #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2386 #else
2387     #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2388 #endif
2389 
2390 #ifdef HAVE_SYMLINKAT
2391     #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2392 #else
2393     #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2394 #endif
2395 
2396 #ifdef HAVE_UNLINKAT
2397     #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2398 #else
2399     #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2400 #endif
2401 
2402 #ifdef HAVE_FCHDIR
2403     #define PATH_HAVE_FCHDIR 1
2404 #else
2405     #define PATH_HAVE_FCHDIR 0
2406 #endif
2407 
2408 #ifdef HAVE_FCHMOD
2409     #define PATH_HAVE_FCHMOD 1
2410 #else
2411     #define PATH_HAVE_FCHMOD 0
2412 #endif
2413 
2414 #ifdef HAVE_FCHOWN
2415     #define PATH_HAVE_FCHOWN 1
2416 #else
2417     #define PATH_HAVE_FCHOWN 0
2418 #endif
2419 
2420 #ifdef HAVE_FDOPENDIR
2421     #define PATH_HAVE_FDOPENDIR 1
2422 #else
2423     #define PATH_HAVE_FDOPENDIR 0
2424 #endif
2425 
2426 #ifdef HAVE_FEXECVE
2427     #define PATH_HAVE_FEXECVE 1
2428 #else
2429     #define PATH_HAVE_FEXECVE 0
2430 #endif
2431 
2432 #ifdef HAVE_FPATHCONF
2433     #define PATH_HAVE_FPATHCONF 1
2434 #else
2435     #define PATH_HAVE_FPATHCONF 0
2436 #endif
2437 
2438 #ifdef HAVE_FSTATVFS
2439     #define PATH_HAVE_FSTATVFS 1
2440 #else
2441     #define PATH_HAVE_FSTATVFS 0
2442 #endif
2443 
2444 #ifdef HAVE_FTRUNCATE
2445     #define PATH_HAVE_FTRUNCATE 1
2446 #else
2447     #define PATH_HAVE_FTRUNCATE 0
2448 #endif
2449 /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
2450 
2451 #ifdef MS_WINDOWS
2452     #undef PATH_HAVE_FTRUNCATE
2453     #define PATH_HAVE_FTRUNCATE 1
2454 #endif
2455 
2456 /*[python input]
2457 
2458 class path_t_converter(CConverter):
2459 
2460     type = "path_t"
2461     impl_by_reference = True
2462     parse_by_reference = True
2463 
2464     converter = 'path_converter'
2465 
2466     def converter_init(self, *, allow_fd=False, nullable=False):
2467         # right now path_t doesn't support default values.
2468         # to support a default value, you'll need to override initialize().
2469         if self.default not in (unspecified, None):
2470             fail("Can't specify a default to the path_t converter!")
2471 
2472         if self.c_default not in (None, 'Py_None'):
2473             raise RuntimeError("Can't specify a c_default to the path_t converter!")
2474 
2475         self.nullable = nullable
2476         self.allow_fd = allow_fd
2477 
2478     def pre_render(self):
2479         def strify(value):
2480             if isinstance(value, str):
2481                 return value
2482             return str(int(bool(value)))
2483 
2484         # add self.py_name here when merging with posixmodule conversion
2485         self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
2486             self.function.name,
2487             self.name,
2488             strify(self.nullable),
2489             strify(self.allow_fd),
2490             )
2491 
2492     def cleanup(self):
2493         return "path_cleanup(&" + self.name + ");\n"
2494 
2495 
2496 class dir_fd_converter(CConverter):
2497     type = 'int'
2498 
2499     def converter_init(self, requires=None):
2500         if self.default in (unspecified, None):
2501             self.c_default = 'DEFAULT_DIR_FD'
2502         if isinstance(requires, str):
2503             self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2504         else:
2505             self.converter = 'dir_fd_converter'
2506 
2507 class fildes_converter(CConverter):
2508     type = 'int'
2509     converter = 'fildes_converter'
2510 
2511 class uid_t_converter(CConverter):
2512     type = "uid_t"
2513     converter = '_Py_Uid_Converter'
2514 
2515 class gid_t_converter(CConverter):
2516     type = "gid_t"
2517     converter = '_Py_Gid_Converter'
2518 
2519 class dev_t_converter(CConverter):
2520     type = 'dev_t'
2521     converter = '_Py_Dev_Converter'
2522 
2523 class dev_t_return_converter(unsigned_long_return_converter):
2524     type = 'dev_t'
2525     conversion_fn = '_PyLong_FromDev'
2526     unsigned_cast = '(dev_t)'
2527 
2528 class FSConverter_converter(CConverter):
2529     type = 'PyObject *'
2530     converter = 'PyUnicode_FSConverter'
2531     def converter_init(self):
2532         if self.default is not unspecified:
2533             fail("FSConverter_converter does not support default values")
2534         self.c_default = 'NULL'
2535 
2536     def cleanup(self):
2537         return "Py_XDECREF(" + self.name + ");\n"
2538 
2539 class pid_t_converter(CConverter):
2540     type = 'pid_t'
2541     format_unit = '" _Py_PARSE_PID "'
2542 
2543 class idtype_t_converter(int_converter):
2544     type = 'idtype_t'
2545 
2546 class id_t_converter(CConverter):
2547     type = 'id_t'
2548     format_unit = '" _Py_PARSE_PID "'
2549 
2550 class intptr_t_converter(CConverter):
2551     type = 'intptr_t'
2552     format_unit = '" _Py_PARSE_INTPTR "'
2553 
2554 class Py_off_t_converter(CConverter):
2555     type = 'Py_off_t'
2556     converter = 'Py_off_t_converter'
2557 
2558 class Py_off_t_return_converter(long_return_converter):
2559     type = 'Py_off_t'
2560     conversion_fn = 'PyLong_FromPy_off_t'
2561 
2562 class path_confname_converter(CConverter):
2563     type="int"
2564     converter="conv_path_confname"
2565 
2566 class confstr_confname_converter(path_confname_converter):
2567     converter='conv_confstr_confname'
2568 
2569 class sysconf_confname_converter(path_confname_converter):
2570     converter="conv_sysconf_confname"
2571 
2572 class sched_param_converter(CConverter):
2573     type = 'struct sched_param'
2574     converter = 'convert_sched_param'
2575     impl_by_reference = True;
2576 
2577 [python start generated code]*/
2578 /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
2579 
2580 /*[clinic input]
2581 
2582 os.stat
2583 
2584     path : path_t(allow_fd=True)
2585         Path to be examined; can be string, bytes, a path-like object or
2586         open-file-descriptor int.
2587 
2588     *
2589 
2590     dir_fd : dir_fd(requires='fstatat') = None
2591         If not None, it should be a file descriptor open to a directory,
2592         and path should be a relative string; path will then be relative to
2593         that directory.
2594 
2595     follow_symlinks: bool = True
2596         If False, and the last element of the path is a symbolic link,
2597         stat will examine the symbolic link itself instead of the file
2598         the link points to.
2599 
2600 Perform a stat system call on the given path.
2601 
2602 dir_fd and follow_symlinks may not be implemented
2603   on your platform.  If they are unavailable, using them will raise a
2604   NotImplementedError.
2605 
2606 It's an error to use dir_fd or follow_symlinks when specifying path as
2607   an open file descriptor.
2608 
2609 [clinic start generated code]*/
2610 
2611 static PyObject *
os_stat_impl(PyObject * module,path_t * path,int dir_fd,int follow_symlinks)2612 os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
2613 /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
2614 {
2615     return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2616 }
2617 
2618 
2619 /*[clinic input]
2620 os.lstat
2621 
2622     path : path_t
2623 
2624     *
2625 
2626     dir_fd : dir_fd(requires='fstatat') = None
2627 
2628 Perform a stat system call on the given path, without following symbolic links.
2629 
2630 Like stat(), but do not follow symbolic links.
2631 Equivalent to stat(path, follow_symlinks=False).
2632 [clinic start generated code]*/
2633 
2634 static PyObject *
os_lstat_impl(PyObject * module,path_t * path,int dir_fd)2635 os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2636 /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
2637 {
2638     int follow_symlinks = 0;
2639     return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2640 }
2641 
2642 
2643 /*[clinic input]
2644 os.access -> bool
2645 
2646     path: path_t
2647         Path to be tested; can be string, bytes, or a path-like object.
2648 
2649     mode: int
2650         Operating-system mode bitfield.  Can be F_OK to test existence,
2651         or the inclusive-OR of R_OK, W_OK, and X_OK.
2652 
2653     *
2654 
2655     dir_fd : dir_fd(requires='faccessat') = None
2656         If not None, it should be a file descriptor open to a directory,
2657         and path should be relative; path will then be relative to that
2658         directory.
2659 
2660     effective_ids: bool = False
2661         If True, access will use the effective uid/gid instead of
2662         the real uid/gid.
2663 
2664     follow_symlinks: bool = True
2665         If False, and the last element of the path is a symbolic link,
2666         access will examine the symbolic link itself instead of the file
2667         the link points to.
2668 
2669 Use the real uid/gid to test for access to a path.
2670 
2671 {parameters}
2672 dir_fd, effective_ids, and follow_symlinks may not be implemented
2673   on your platform.  If they are unavailable, using them will raise a
2674   NotImplementedError.
2675 
2676 Note that most operations will use the effective uid/gid, therefore this
2677   routine can be used in a suid/sgid environment to test if the invoking user
2678   has the specified access to the path.
2679 
2680 [clinic start generated code]*/
2681 
2682 static int
os_access_impl(PyObject * module,path_t * path,int mode,int dir_fd,int effective_ids,int follow_symlinks)2683 os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
2684                int effective_ids, int follow_symlinks)
2685 /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
2686 {
2687     int return_value;
2688 
2689 #ifdef MS_WINDOWS
2690     DWORD attr;
2691 #else
2692     int result;
2693 #endif
2694 
2695 #ifndef HAVE_FACCESSAT
2696     if (follow_symlinks_specified("access", follow_symlinks))
2697         return -1;
2698 
2699     if (effective_ids) {
2700         argument_unavailable_error("access", "effective_ids");
2701         return -1;
2702     }
2703 #endif
2704 
2705 #ifdef MS_WINDOWS
2706     Py_BEGIN_ALLOW_THREADS
2707     attr = GetFileAttributesW(path->wide);
2708     Py_END_ALLOW_THREADS
2709 
2710     /*
2711      * Access is possible if
2712      *   * we didn't get a -1, and
2713      *     * write access wasn't requested,
2714      *     * or the file isn't read-only,
2715      *     * or it's a directory.
2716      * (Directories cannot be read-only on Windows.)
2717     */
2718     return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
2719             (!(mode & 2) ||
2720             !(attr & FILE_ATTRIBUTE_READONLY) ||
2721             (attr & FILE_ATTRIBUTE_DIRECTORY));
2722 #else
2723 
2724     Py_BEGIN_ALLOW_THREADS
2725 #ifdef HAVE_FACCESSAT
2726     if ((dir_fd != DEFAULT_DIR_FD) ||
2727         effective_ids ||
2728         !follow_symlinks) {
2729         int flags = 0;
2730         if (!follow_symlinks)
2731             flags |= AT_SYMLINK_NOFOLLOW;
2732         if (effective_ids)
2733             flags |= AT_EACCESS;
2734         result = faccessat(dir_fd, path->narrow, mode, flags);
2735     }
2736     else
2737 #endif
2738         result = access(path->narrow, mode);
2739     Py_END_ALLOW_THREADS
2740     return_value = !result;
2741 #endif
2742 
2743     return return_value;
2744 }
2745 
2746 #ifndef F_OK
2747 #define F_OK 0
2748 #endif
2749 #ifndef R_OK
2750 #define R_OK 4
2751 #endif
2752 #ifndef W_OK
2753 #define W_OK 2
2754 #endif
2755 #ifndef X_OK
2756 #define X_OK 1
2757 #endif
2758 
2759 
2760 #ifdef HAVE_TTYNAME
2761 /*[clinic input]
2762 os.ttyname
2763 
2764     fd: int
2765         Integer file descriptor handle.
2766 
2767     /
2768 
2769 Return the name of the terminal device connected to 'fd'.
2770 [clinic start generated code]*/
2771 
2772 static PyObject *
os_ttyname_impl(PyObject * module,int fd)2773 os_ttyname_impl(PyObject *module, int fd)
2774 /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
2775 {
2776     char *ret;
2777 
2778     ret = ttyname(fd);
2779     if (ret == NULL) {
2780         return posix_error();
2781     }
2782     return PyUnicode_DecodeFSDefault(ret);
2783 }
2784 #endif
2785 
2786 #ifdef HAVE_CTERMID
2787 /*[clinic input]
2788 os.ctermid
2789 
2790 Return the name of the controlling terminal for this process.
2791 [clinic start generated code]*/
2792 
2793 static PyObject *
os_ctermid_impl(PyObject * module)2794 os_ctermid_impl(PyObject *module)
2795 /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
2796 {
2797     char *ret;
2798     char buffer[L_ctermid];
2799 
2800 #ifdef USE_CTERMID_R
2801     ret = ctermid_r(buffer);
2802 #else
2803     ret = ctermid(buffer);
2804 #endif
2805     if (ret == NULL)
2806         return posix_error();
2807     return PyUnicode_DecodeFSDefault(buffer);
2808 }
2809 #endif /* HAVE_CTERMID */
2810 
2811 
2812 /*[clinic input]
2813 os.chdir
2814 
2815     path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2816 
2817 Change the current working directory to the specified path.
2818 
2819 path may always be specified as a string.
2820 On some platforms, path may also be specified as an open file descriptor.
2821   If this functionality is unavailable, using it raises an exception.
2822 [clinic start generated code]*/
2823 
2824 static PyObject *
os_chdir_impl(PyObject * module,path_t * path)2825 os_chdir_impl(PyObject *module, path_t *path)
2826 /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
2827 {
2828     int result;
2829 
2830     if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
2831         return NULL;
2832     }
2833 
2834     Py_BEGIN_ALLOW_THREADS
2835 #ifdef MS_WINDOWS
2836     /* on unix, success = 0, on windows, success = !0 */
2837     result = !win32_wchdir(path->wide);
2838 #else
2839 #ifdef HAVE_FCHDIR
2840     if (path->fd != -1)
2841         result = fchdir(path->fd);
2842     else
2843 #endif
2844         result = chdir(path->narrow);
2845 #endif
2846     Py_END_ALLOW_THREADS
2847 
2848     if (result) {
2849         return path_error(path);
2850     }
2851 
2852     Py_RETURN_NONE;
2853 }
2854 
2855 
2856 #ifdef HAVE_FCHDIR
2857 /*[clinic input]
2858 os.fchdir
2859 
2860     fd: fildes
2861 
2862 Change to the directory of the given file descriptor.
2863 
2864 fd must be opened on a directory, not a file.
2865 Equivalent to os.chdir(fd).
2866 
2867 [clinic start generated code]*/
2868 
2869 static PyObject *
os_fchdir_impl(PyObject * module,int fd)2870 os_fchdir_impl(PyObject *module, int fd)
2871 /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
2872 {
2873     if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
2874         return NULL;
2875     }
2876     return posix_fildes_fd(fd, fchdir);
2877 }
2878 #endif /* HAVE_FCHDIR */
2879 
2880 
2881 /*[clinic input]
2882 os.chmod
2883 
2884     path: path_t(allow_fd='PATH_HAVE_FCHMOD')
2885         Path to be modified.  May always be specified as a str, bytes, or a path-like object.
2886         On some platforms, path may also be specified as an open file descriptor.
2887         If this functionality is unavailable, using it raises an exception.
2888 
2889     mode: int
2890         Operating-system mode bitfield.
2891 
2892     *
2893 
2894     dir_fd : dir_fd(requires='fchmodat') = None
2895         If not None, it should be a file descriptor open to a directory,
2896         and path should be relative; path will then be relative to that
2897         directory.
2898 
2899     follow_symlinks: bool = True
2900         If False, and the last element of the path is a symbolic link,
2901         chmod will modify the symbolic link itself instead of the file
2902         the link points to.
2903 
2904 Change the access permissions of a file.
2905 
2906 It is an error to use dir_fd or follow_symlinks when specifying path as
2907   an open file descriptor.
2908 dir_fd and follow_symlinks may not be implemented on your platform.
2909   If they are unavailable, using them will raise a NotImplementedError.
2910 
2911 [clinic start generated code]*/
2912 
2913 static PyObject *
os_chmod_impl(PyObject * module,path_t * path,int mode,int dir_fd,int follow_symlinks)2914 os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
2915               int follow_symlinks)
2916 /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
2917 {
2918     int result;
2919 
2920 #ifdef MS_WINDOWS
2921     DWORD attr;
2922 #endif
2923 
2924 #ifdef HAVE_FCHMODAT
2925     int fchmodat_nofollow_unsupported = 0;
2926 #endif
2927 
2928 #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2929     if (follow_symlinks_specified("chmod", follow_symlinks))
2930         return NULL;
2931 #endif
2932 
2933     if (PySys_Audit("os.chmod", "Oii", path->object, mode,
2934                     dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
2935         return NULL;
2936     }
2937 
2938 #ifdef MS_WINDOWS
2939     Py_BEGIN_ALLOW_THREADS
2940     attr = GetFileAttributesW(path->wide);
2941     if (attr == INVALID_FILE_ATTRIBUTES)
2942         result = 0;
2943     else {
2944         if (mode & _S_IWRITE)
2945             attr &= ~FILE_ATTRIBUTE_READONLY;
2946         else
2947             attr |= FILE_ATTRIBUTE_READONLY;
2948         result = SetFileAttributesW(path->wide, attr);
2949     }
2950     Py_END_ALLOW_THREADS
2951 
2952     if (!result) {
2953         return path_error(path);
2954     }
2955 #else /* MS_WINDOWS */
2956     Py_BEGIN_ALLOW_THREADS
2957 #ifdef HAVE_FCHMOD
2958     if (path->fd != -1)
2959         result = fchmod(path->fd, mode);
2960     else
2961 #endif
2962 #ifdef HAVE_LCHMOD
2963     if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2964         result = lchmod(path->narrow, mode);
2965     else
2966 #endif
2967 #ifdef HAVE_FCHMODAT
2968     if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2969         /*
2970          * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2971          * The documentation specifically shows how to use it,
2972          * and then says it isn't implemented yet.
2973          * (true on linux with glibc 2.15, and openindiana 3.x)
2974          *
2975          * Once it is supported, os.chmod will automatically
2976          * support dir_fd and follow_symlinks=False.  (Hopefully.)
2977          * Until then, we need to be careful what exception we raise.
2978          */
2979         result = fchmodat(dir_fd, path->narrow, mode,
2980                           follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2981         /*
2982          * But wait!  We can't throw the exception without allowing threads,
2983          * and we can't do that in this nested scope.  (Macro trickery, sigh.)
2984          */
2985         fchmodat_nofollow_unsupported =
2986                          result &&
2987                          ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2988                          !follow_symlinks;
2989     }
2990     else
2991 #endif
2992         result = chmod(path->narrow, mode);
2993     Py_END_ALLOW_THREADS
2994 
2995     if (result) {
2996 #ifdef HAVE_FCHMODAT
2997         if (fchmodat_nofollow_unsupported) {
2998             if (dir_fd != DEFAULT_DIR_FD)
2999                 dir_fd_and_follow_symlinks_invalid("chmod",
3000                                                    dir_fd, follow_symlinks);
3001             else
3002                 follow_symlinks_specified("chmod", follow_symlinks);
3003             return NULL;
3004         }
3005         else
3006 #endif
3007         return path_error(path);
3008     }
3009 #endif
3010 
3011     Py_RETURN_NONE;
3012 }
3013 
3014 
3015 #ifdef HAVE_FCHMOD
3016 /*[clinic input]
3017 os.fchmod
3018 
3019     fd: int
3020     mode: int
3021 
3022 Change the access permissions of the file given by file descriptor fd.
3023 
3024 Equivalent to os.chmod(fd, mode).
3025 [clinic start generated code]*/
3026 
3027 static PyObject *
os_fchmod_impl(PyObject * module,int fd,int mode)3028 os_fchmod_impl(PyObject *module, int fd, int mode)
3029 /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
3030 {
3031     int res;
3032     int async_err = 0;
3033 
3034     if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
3035         return NULL;
3036     }
3037 
3038     do {
3039         Py_BEGIN_ALLOW_THREADS
3040         res = fchmod(fd, mode);
3041         Py_END_ALLOW_THREADS
3042     } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3043     if (res != 0)
3044         return (!async_err) ? posix_error() : NULL;
3045 
3046     Py_RETURN_NONE;
3047 }
3048 #endif /* HAVE_FCHMOD */
3049 
3050 
3051 #ifdef HAVE_LCHMOD
3052 /*[clinic input]
3053 os.lchmod
3054 
3055     path: path_t
3056     mode: int
3057 
3058 Change the access permissions of a file, without following symbolic links.
3059 
3060 If path is a symlink, this affects the link itself rather than the target.
3061 Equivalent to chmod(path, mode, follow_symlinks=False)."
3062 [clinic start generated code]*/
3063 
3064 static PyObject *
os_lchmod_impl(PyObject * module,path_t * path,int mode)3065 os_lchmod_impl(PyObject *module, path_t *path, int mode)
3066 /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
3067 {
3068     int res;
3069     if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
3070         return NULL;
3071     }
3072     Py_BEGIN_ALLOW_THREADS
3073     res = lchmod(path->narrow, mode);
3074     Py_END_ALLOW_THREADS
3075     if (res < 0) {
3076         path_error(path);
3077         return NULL;
3078     }
3079     Py_RETURN_NONE;
3080 }
3081 #endif /* HAVE_LCHMOD */
3082 
3083 
3084 #ifdef HAVE_CHFLAGS
3085 /*[clinic input]
3086 os.chflags
3087 
3088     path: path_t
3089     flags: unsigned_long(bitwise=True)
3090     follow_symlinks: bool=True
3091 
3092 Set file flags.
3093 
3094 If follow_symlinks is False, and the last element of the path is a symbolic
3095   link, chflags will change flags on the symbolic link itself instead of the
3096   file the link points to.
3097 follow_symlinks may not be implemented on your platform.  If it is
3098 unavailable, using it will raise a NotImplementedError.
3099 
3100 [clinic start generated code]*/
3101 
3102 static PyObject *
os_chflags_impl(PyObject * module,path_t * path,unsigned long flags,int follow_symlinks)3103 os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
3104                 int follow_symlinks)
3105 /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
3106 {
3107     int result;
3108 
3109 #ifndef HAVE_LCHFLAGS
3110     if (follow_symlinks_specified("chflags", follow_symlinks))
3111         return NULL;
3112 #endif
3113 
3114     if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3115         return NULL;
3116     }
3117 
3118     Py_BEGIN_ALLOW_THREADS
3119 #ifdef HAVE_LCHFLAGS
3120     if (!follow_symlinks)
3121         result = lchflags(path->narrow, flags);
3122     else
3123 #endif
3124         result = chflags(path->narrow, flags);
3125     Py_END_ALLOW_THREADS
3126 
3127     if (result)
3128         return path_error(path);
3129 
3130     Py_RETURN_NONE;
3131 }
3132 #endif /* HAVE_CHFLAGS */
3133 
3134 
3135 #ifdef HAVE_LCHFLAGS
3136 /*[clinic input]
3137 os.lchflags
3138 
3139     path: path_t
3140     flags: unsigned_long(bitwise=True)
3141 
3142 Set file flags.
3143 
3144 This function will not follow symbolic links.
3145 Equivalent to chflags(path, flags, follow_symlinks=False).
3146 [clinic start generated code]*/
3147 
3148 static PyObject *
os_lchflags_impl(PyObject * module,path_t * path,unsigned long flags)3149 os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3150 /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
3151 {
3152     int res;
3153     if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3154         return NULL;
3155     }
3156     Py_BEGIN_ALLOW_THREADS
3157     res = lchflags(path->narrow, flags);
3158     Py_END_ALLOW_THREADS
3159     if (res < 0) {
3160         return path_error(path);
3161     }
3162     Py_RETURN_NONE;
3163 }
3164 #endif /* HAVE_LCHFLAGS */
3165 
3166 
3167 #ifdef HAVE_CHROOT
3168 /*[clinic input]
3169 os.chroot
3170     path: path_t
3171 
3172 Change root directory to path.
3173 
3174 [clinic start generated code]*/
3175 
3176 static PyObject *
os_chroot_impl(PyObject * module,path_t * path)3177 os_chroot_impl(PyObject *module, path_t *path)
3178 /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
3179 {
3180     int res;
3181     Py_BEGIN_ALLOW_THREADS
3182     res = chroot(path->narrow);
3183     Py_END_ALLOW_THREADS
3184     if (res < 0)
3185         return path_error(path);
3186     Py_RETURN_NONE;
3187 }
3188 #endif /* HAVE_CHROOT */
3189 
3190 
3191 #ifdef HAVE_FSYNC
3192 /*[clinic input]
3193 os.fsync
3194 
3195     fd: fildes
3196 
3197 Force write of fd to disk.
3198 [clinic start generated code]*/
3199 
3200 static PyObject *
os_fsync_impl(PyObject * module,int fd)3201 os_fsync_impl(PyObject *module, int fd)
3202 /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
3203 {
3204     return posix_fildes_fd(fd, fsync);
3205 }
3206 #endif /* HAVE_FSYNC */
3207 
3208 
3209 #ifdef HAVE_SYNC
3210 /*[clinic input]
3211 os.sync
3212 
3213 Force write of everything to disk.
3214 [clinic start generated code]*/
3215 
3216 static PyObject *
os_sync_impl(PyObject * module)3217 os_sync_impl(PyObject *module)
3218 /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
3219 {
3220     Py_BEGIN_ALLOW_THREADS
3221     sync();
3222     Py_END_ALLOW_THREADS
3223     Py_RETURN_NONE;
3224 }
3225 #endif /* HAVE_SYNC */
3226 
3227 
3228 #ifdef HAVE_FDATASYNC
3229 #ifdef __hpux
3230 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3231 #endif
3232 
3233 /*[clinic input]
3234 os.fdatasync
3235 
3236     fd: fildes
3237 
3238 Force write of fd to disk without forcing update of metadata.
3239 [clinic start generated code]*/
3240 
3241 static PyObject *
os_fdatasync_impl(PyObject * module,int fd)3242 os_fdatasync_impl(PyObject *module, int fd)
3243 /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
3244 {
3245     return posix_fildes_fd(fd, fdatasync);
3246 }
3247 #endif /* HAVE_FDATASYNC */
3248 
3249 
3250 #ifdef HAVE_CHOWN
3251 /*[clinic input]
3252 os.chown
3253 
3254     path : path_t(allow_fd='PATH_HAVE_FCHOWN')
3255         Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
3256 
3257     uid: uid_t
3258 
3259     gid: gid_t
3260 
3261     *
3262 
3263     dir_fd : dir_fd(requires='fchownat') = None
3264         If not None, it should be a file descriptor open to a directory,
3265         and path should be relative; path will then be relative to that
3266         directory.
3267 
3268     follow_symlinks: bool = True
3269         If False, and the last element of the path is a symbolic link,
3270         stat will examine the symbolic link itself instead of the file
3271         the link points to.
3272 
3273 Change the owner and group id of path to the numeric uid and gid.\
3274 
3275 path may always be specified as a string.
3276 On some platforms, path may also be specified as an open file descriptor.
3277   If this functionality is unavailable, using it raises an exception.
3278 If dir_fd is not None, it should be a file descriptor open to a directory,
3279   and path should be relative; path will then be relative to that directory.
3280 If follow_symlinks is False, and the last element of the path is a symbolic
3281   link, chown will modify the symbolic link itself instead of the file the
3282   link points to.
3283 It is an error to use dir_fd or follow_symlinks when specifying path as
3284   an open file descriptor.
3285 dir_fd and follow_symlinks may not be implemented on your platform.
3286   If they are unavailable, using them will raise a NotImplementedError.
3287 
3288 [clinic start generated code]*/
3289 
3290 static PyObject *
os_chown_impl(PyObject * module,path_t * path,uid_t uid,gid_t gid,int dir_fd,int follow_symlinks)3291 os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
3292               int dir_fd, int follow_symlinks)
3293 /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
3294 {
3295     int result;
3296 
3297 #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3298     if (follow_symlinks_specified("chown", follow_symlinks))
3299         return NULL;
3300 #endif
3301     if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3302         fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3303         return NULL;
3304 
3305 #ifdef __APPLE__
3306     /*
3307      * This is for Mac OS X 10.3, which doesn't have lchown.
3308      * (But we still have an lchown symbol because of weak-linking.)
3309      * It doesn't have fchownat either.  So there's no possibility
3310      * of a graceful failover.
3311      */
3312     if ((!follow_symlinks) && (lchown == NULL)) {
3313         follow_symlinks_specified("chown", follow_symlinks);
3314         return NULL;
3315     }
3316 #endif
3317 
3318     if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
3319                     dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3320         return NULL;
3321     }
3322 
3323     Py_BEGIN_ALLOW_THREADS
3324 #ifdef HAVE_FCHOWN
3325     if (path->fd != -1)
3326         result = fchown(path->fd, uid, gid);
3327     else
3328 #endif
3329 #ifdef HAVE_LCHOWN
3330     if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
3331         result = lchown(path->narrow, uid, gid);
3332     else
3333 #endif
3334 #ifdef HAVE_FCHOWNAT
3335     if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
3336         result = fchownat(dir_fd, path->narrow, uid, gid,
3337                           follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3338     else
3339 #endif
3340         result = chown(path->narrow, uid, gid);
3341     Py_END_ALLOW_THREADS
3342 
3343     if (result)
3344         return path_error(path);
3345 
3346     Py_RETURN_NONE;
3347 }
3348 #endif /* HAVE_CHOWN */
3349 
3350 
3351 #ifdef HAVE_FCHOWN
3352 /*[clinic input]
3353 os.fchown
3354 
3355     fd: int
3356     uid: uid_t
3357     gid: gid_t
3358 
3359 Change the owner and group id of the file specified by file descriptor.
3360 
3361 Equivalent to os.chown(fd, uid, gid).
3362 
3363 [clinic start generated code]*/
3364 
3365 static PyObject *
os_fchown_impl(PyObject * module,int fd,uid_t uid,gid_t gid)3366 os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3367 /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
3368 {
3369     int res;
3370     int async_err = 0;
3371 
3372     if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
3373         return NULL;
3374     }
3375 
3376     do {
3377         Py_BEGIN_ALLOW_THREADS
3378         res = fchown(fd, uid, gid);
3379         Py_END_ALLOW_THREADS
3380     } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3381     if (res != 0)
3382         return (!async_err) ? posix_error() : NULL;
3383 
3384     Py_RETURN_NONE;
3385 }
3386 #endif /* HAVE_FCHOWN */
3387 
3388 
3389 #ifdef HAVE_LCHOWN
3390 /*[clinic input]
3391 os.lchown
3392 
3393     path : path_t
3394     uid: uid_t
3395     gid: gid_t
3396 
3397 Change the owner and group id of path to the numeric uid and gid.
3398 
3399 This function will not follow symbolic links.
3400 Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3401 [clinic start generated code]*/
3402 
3403 static PyObject *
os_lchown_impl(PyObject * module,path_t * path,uid_t uid,gid_t gid)3404 os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3405 /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
3406 {
3407     int res;
3408     if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
3409         return NULL;
3410     }
3411     Py_BEGIN_ALLOW_THREADS
3412     res = lchown(path->narrow, uid, gid);
3413     Py_END_ALLOW_THREADS
3414     if (res < 0) {
3415         return path_error(path);
3416     }
3417     Py_RETURN_NONE;
3418 }
3419 #endif /* HAVE_LCHOWN */
3420 
3421 
3422 static PyObject *
posix_getcwd(int use_bytes)3423 posix_getcwd(int use_bytes)
3424 {
3425 #ifdef MS_WINDOWS
3426     wchar_t wbuf[MAXPATHLEN];
3427     wchar_t *wbuf2 = wbuf;
3428     DWORD len;
3429 
3430     Py_BEGIN_ALLOW_THREADS
3431     len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3432     /* If the buffer is large enough, len does not include the
3433        terminating \0. If the buffer is too small, len includes
3434        the space needed for the terminator. */
3435     if (len >= Py_ARRAY_LENGTH(wbuf)) {
3436         if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
3437             wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
3438         }
3439         else {
3440             wbuf2 = NULL;
3441         }
3442         if (wbuf2) {
3443             len = GetCurrentDirectoryW(len, wbuf2);
3444         }
3445     }
3446     Py_END_ALLOW_THREADS
3447 
3448     if (!wbuf2) {
3449         PyErr_NoMemory();
3450         return NULL;
3451     }
3452     if (!len) {
3453         if (wbuf2 != wbuf)
3454             PyMem_RawFree(wbuf2);
3455         return PyErr_SetFromWindowsErr(0);
3456     }
3457 
3458     PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3459     if (wbuf2 != wbuf) {
3460         PyMem_RawFree(wbuf2);
3461     }
3462 
3463     if (use_bytes) {
3464         if (resobj == NULL) {
3465             return NULL;
3466         }
3467         Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3468     }
3469 
3470     return resobj;
3471 #else
3472     const size_t chunk = 1024;
3473 
3474     char *buf = NULL;
3475     char *cwd = NULL;
3476     size_t buflen = 0;
3477 
3478     Py_BEGIN_ALLOW_THREADS
3479     do {
3480         char *newbuf;
3481         if (buflen <= PY_SSIZE_T_MAX - chunk) {
3482             buflen += chunk;
3483             newbuf = PyMem_RawRealloc(buf, buflen);
3484         }
3485         else {
3486             newbuf = NULL;
3487         }
3488         if (newbuf == NULL) {
3489             PyMem_RawFree(buf);
3490             buf = NULL;
3491             break;
3492         }
3493         buf = newbuf;
3494 
3495         cwd = getcwd(buf, buflen);
3496     } while (cwd == NULL && errno == ERANGE);
3497     Py_END_ALLOW_THREADS
3498 
3499     if (buf == NULL) {
3500         return PyErr_NoMemory();
3501     }
3502     if (cwd == NULL) {
3503         PyMem_RawFree(buf);
3504         return posix_error();
3505     }
3506 
3507     PyObject *obj;
3508     if (use_bytes) {
3509         obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3510     }
3511     else {
3512         obj = PyUnicode_DecodeFSDefault(buf);
3513     }
3514     PyMem_RawFree(buf);
3515 
3516     return obj;
3517 #endif   /* !MS_WINDOWS */
3518 }
3519 
3520 
3521 /*[clinic input]
3522 os.getcwd
3523 
3524 Return a unicode string representing the current working directory.
3525 [clinic start generated code]*/
3526 
3527 static PyObject *
os_getcwd_impl(PyObject * module)3528 os_getcwd_impl(PyObject *module)
3529 /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
3530 {
3531     return posix_getcwd(0);
3532 }
3533 
3534 
3535 /*[clinic input]
3536 os.getcwdb
3537 
3538 Return a bytes string representing the current working directory.
3539 [clinic start generated code]*/
3540 
3541 static PyObject *
os_getcwdb_impl(PyObject * module)3542 os_getcwdb_impl(PyObject *module)
3543 /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
3544 {
3545     return posix_getcwd(1);
3546 }
3547 
3548 
3549 #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3550 #define HAVE_LINK 1
3551 #endif
3552 
3553 #ifdef HAVE_LINK
3554 /*[clinic input]
3555 
3556 os.link
3557 
3558     src : path_t
3559     dst : path_t
3560     *
3561     src_dir_fd : dir_fd = None
3562     dst_dir_fd : dir_fd = None
3563     follow_symlinks: bool = True
3564 
3565 Create a hard link to a file.
3566 
3567 If either src_dir_fd or dst_dir_fd is not None, it should be a file
3568   descriptor open to a directory, and the respective path string (src or dst)
3569   should be relative; the path will then be relative to that directory.
3570 If follow_symlinks is False, and the last element of src is a symbolic
3571   link, link will create a link to the symbolic link itself instead of the
3572   file the link points to.
3573 src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3574   platform.  If they are unavailable, using them will raise a
3575   NotImplementedError.
3576 [clinic start generated code]*/
3577 
3578 static PyObject *
os_link_impl(PyObject * module,path_t * src,path_t * dst,int src_dir_fd,int dst_dir_fd,int follow_symlinks)3579 os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
3580              int dst_dir_fd, int follow_symlinks)
3581 /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
3582 {
3583 #ifdef MS_WINDOWS
3584     BOOL result = FALSE;
3585 #else
3586     int result;
3587 #endif
3588 
3589 #ifndef HAVE_LINKAT
3590     if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3591         argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
3592         return NULL;
3593     }
3594 #endif
3595 
3596 #ifndef MS_WINDOWS
3597     if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
3598         PyErr_SetString(PyExc_NotImplementedError,
3599                         "link: src and dst must be the same type");
3600         return NULL;
3601     }
3602 #endif
3603 
3604     if (PySys_Audit("os.link", "OOii", src->object, dst->object,
3605                     src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
3606                     dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
3607         return NULL;
3608     }
3609 
3610 #ifdef MS_WINDOWS
3611     Py_BEGIN_ALLOW_THREADS
3612     result = CreateHardLinkW(dst->wide, src->wide, NULL);
3613     Py_END_ALLOW_THREADS
3614 
3615     if (!result)
3616         return path_error2(src, dst);
3617 #else
3618     Py_BEGIN_ALLOW_THREADS
3619 #ifdef HAVE_LINKAT
3620     if ((src_dir_fd != DEFAULT_DIR_FD) ||
3621         (dst_dir_fd != DEFAULT_DIR_FD) ||
3622         (!follow_symlinks))
3623         result = linkat(src_dir_fd, src->narrow,
3624             dst_dir_fd, dst->narrow,
3625             follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3626     else
3627 #endif /* HAVE_LINKAT */
3628         result = link(src->narrow, dst->narrow);
3629     Py_END_ALLOW_THREADS
3630 
3631     if (result)
3632         return path_error2(src, dst);
3633 #endif /* MS_WINDOWS */
3634 
3635     Py_RETURN_NONE;
3636 }
3637 #endif
3638 
3639 
3640 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3641 static PyObject *
_listdir_windows_no_opendir(path_t * path,PyObject * list)3642 _listdir_windows_no_opendir(path_t *path, PyObject *list)
3643 {
3644     PyObject *v;
3645     HANDLE hFindFile = INVALID_HANDLE_VALUE;
3646     BOOL result;
3647     wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
3648     /* only claim to have space for MAX_PATH */
3649     Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
3650     wchar_t *wnamebuf = NULL;
3651 
3652     WIN32_FIND_DATAW wFileData;
3653     const wchar_t *po_wchars;
3654 
3655     if (!path->wide) { /* Default arg: "." */
3656         po_wchars = L".";
3657         len = 1;
3658     } else {
3659         po_wchars = path->wide;
3660         len = wcslen(path->wide);
3661     }
3662     /* The +5 is so we can append "\\*.*\0" */
3663     wnamebuf = PyMem_New(wchar_t, len + 5);
3664     if (!wnamebuf) {
3665         PyErr_NoMemory();
3666         goto exit;
3667     }
3668     wcscpy(wnamebuf, po_wchars);
3669     if (len > 0) {
3670         wchar_t wch = wnamebuf[len-1];
3671         if (wch != SEP && wch != ALTSEP && wch != L':')
3672             wnamebuf[len++] = SEP;
3673         wcscpy(wnamebuf + len, L"*.*");
3674     }
3675     if ((list = PyList_New(0)) == NULL) {
3676         goto exit;
3677     }
3678     Py_BEGIN_ALLOW_THREADS
3679     hFindFile = FindFirstFileW(wnamebuf, &wFileData);
3680     Py_END_ALLOW_THREADS
3681     if (hFindFile == INVALID_HANDLE_VALUE) {
3682         int error = GetLastError();
3683         if (error == ERROR_FILE_NOT_FOUND)
3684             goto exit;
3685         Py_DECREF(list);
3686         list = path_error(path);
3687         goto exit;
3688     }
3689     do {
3690         /* Skip over . and .. */
3691         if (wcscmp(wFileData.cFileName, L".") != 0 &&
3692             wcscmp(wFileData.cFileName, L"..") != 0) {
3693             v = PyUnicode_FromWideChar(wFileData.cFileName,
3694                                        wcslen(wFileData.cFileName));
3695             if (path->narrow && v) {
3696                 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3697             }
3698             if (v == NULL) {
3699                 Py_DECREF(list);
3700                 list = NULL;
3701                 break;
3702             }
3703             if (PyList_Append(list, v) != 0) {
3704                 Py_DECREF(v);
3705                 Py_DECREF(list);
3706                 list = NULL;
3707                 break;
3708             }
3709             Py_DECREF(v);
3710         }
3711         Py_BEGIN_ALLOW_THREADS
3712         result = FindNextFileW(hFindFile, &wFileData);
3713         Py_END_ALLOW_THREADS
3714         /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3715            it got to the end of the directory. */
3716         if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
3717             Py_DECREF(list);
3718             list = path_error(path);
3719             goto exit;
3720         }
3721     } while (result == TRUE);
3722 
3723 exit:
3724     if (hFindFile != INVALID_HANDLE_VALUE) {
3725         if (FindClose(hFindFile) == FALSE) {
3726             if (list != NULL) {
3727                 Py_DECREF(list);
3728                 list = path_error(path);
3729             }
3730         }
3731     }
3732     PyMem_Free(wnamebuf);
3733 
3734     return list;
3735 }  /* end of _listdir_windows_no_opendir */
3736 
3737 #else  /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3738 
3739 static PyObject *
_posix_listdir(path_t * path,PyObject * list)3740 _posix_listdir(path_t *path, PyObject *list)
3741 {
3742     PyObject *v;
3743     DIR *dirp = NULL;
3744     struct dirent *ep;
3745     int return_str; /* if false, return bytes */
3746 #ifdef HAVE_FDOPENDIR
3747     int fd = -1;
3748 #endif
3749 
3750     errno = 0;
3751 #ifdef HAVE_FDOPENDIR
3752     if (path->fd != -1) {
3753         /* closedir() closes the FD, so we duplicate it */
3754         fd = _Py_dup(path->fd);
3755         if (fd == -1)
3756             return NULL;
3757 
3758         return_str = 1;
3759 
3760         Py_BEGIN_ALLOW_THREADS
3761         dirp = fdopendir(fd);
3762         Py_END_ALLOW_THREADS
3763     }
3764     else
3765 #endif
3766     {
3767         const char *name;
3768         if (path->narrow) {
3769             name = path->narrow;
3770             /* only return bytes if they specified a bytes-like object */
3771             return_str = !PyObject_CheckBuffer(path->object);
3772         }
3773         else {
3774             name = ".";
3775             return_str = 1;
3776         }
3777 
3778         Py_BEGIN_ALLOW_THREADS
3779         dirp = opendir(name);
3780         Py_END_ALLOW_THREADS
3781     }
3782 
3783     if (dirp == NULL) {
3784         list = path_error(path);
3785 #ifdef HAVE_FDOPENDIR
3786         if (fd != -1) {
3787             Py_BEGIN_ALLOW_THREADS
3788             close(fd);
3789             Py_END_ALLOW_THREADS
3790         }
3791 #endif
3792         goto exit;
3793     }
3794     if ((list = PyList_New(0)) == NULL) {
3795         goto exit;
3796     }
3797     for (;;) {
3798         errno = 0;
3799         Py_BEGIN_ALLOW_THREADS
3800         ep = readdir(dirp);
3801         Py_END_ALLOW_THREADS
3802         if (ep == NULL) {
3803             if (errno == 0) {
3804                 break;
3805             } else {
3806                 Py_DECREF(list);
3807                 list = path_error(path);
3808                 goto exit;
3809             }
3810         }
3811         if (ep->d_name[0] == '.' &&
3812             (NAMLEN(ep) == 1 ||
3813              (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3814             continue;
3815         if (return_str)
3816             v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3817         else
3818             v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
3819         if (v == NULL) {
3820             Py_CLEAR(list);
3821             break;
3822         }
3823         if (PyList_Append(list, v) != 0) {
3824             Py_DECREF(v);
3825             Py_CLEAR(list);
3826             break;
3827         }
3828         Py_DECREF(v);
3829     }
3830 
3831 exit:
3832     if (dirp != NULL) {
3833         Py_BEGIN_ALLOW_THREADS
3834 #ifdef HAVE_FDOPENDIR
3835         if (fd > -1)
3836             rewinddir(dirp);
3837 #endif
3838         closedir(dirp);
3839         Py_END_ALLOW_THREADS
3840     }
3841 
3842     return list;
3843 }  /* end of _posix_listdir */
3844 #endif  /* which OS */
3845 
3846 
3847 /*[clinic input]
3848 os.listdir
3849 
3850     path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3851 
3852 Return a list containing the names of the files in the directory.
3853 
3854 path can be specified as either str, bytes, or a path-like object.  If path is bytes,
3855   the filenames returned will also be bytes; in all other circumstances
3856   the filenames returned will be str.
3857 If path is None, uses the path='.'.
3858 On some platforms, path may also be specified as an open file descriptor;\
3859   the file descriptor must refer to a directory.
3860   If this functionality is unavailable, using it raises NotImplementedError.
3861 
3862 The list is in arbitrary order.  It does not include the special
3863 entries '.' and '..' even if they are present in the directory.
3864 
3865 
3866 [clinic start generated code]*/
3867 
3868 static PyObject *
os_listdir_impl(PyObject * module,path_t * path)3869 os_listdir_impl(PyObject *module, path_t *path)
3870 /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
3871 {
3872     if (PySys_Audit("os.listdir", "O",
3873                     path->object ? path->object : Py_None) < 0) {
3874         return NULL;
3875     }
3876 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3877     return _listdir_windows_no_opendir(path, NULL);
3878 #else
3879     return _posix_listdir(path, NULL);
3880 #endif
3881 }
3882 
3883 #ifdef MS_WINDOWS
3884 /* A helper function for abspath on win32 */
3885 /*[clinic input]
3886 os._getfullpathname
3887 
3888     path: path_t
3889     /
3890 
3891 [clinic start generated code]*/
3892 
3893 static PyObject *
os__getfullpathname_impl(PyObject * module,path_t * path)3894 os__getfullpathname_impl(PyObject *module, path_t *path)
3895 /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
3896 {
3897     wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3898     wchar_t *wtemp;
3899     DWORD result;
3900     PyObject *v;
3901 
3902     result = GetFullPathNameW(path->wide,
3903                               Py_ARRAY_LENGTH(woutbuf),
3904                               woutbuf, &wtemp);
3905     if (result > Py_ARRAY_LENGTH(woutbuf)) {
3906         woutbufp = PyMem_New(wchar_t, result);
3907         if (!woutbufp)
3908             return PyErr_NoMemory();
3909         result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
3910     }
3911     if (result) {
3912         v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3913         if (path->narrow)
3914             Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3915     } else
3916         v = win32_error_object("GetFullPathNameW", path->object);
3917     if (woutbufp != woutbuf)
3918         PyMem_Free(woutbufp);
3919     return v;
3920 }
3921 
3922 
3923 /*[clinic input]
3924 os._getfinalpathname
3925 
3926     path: path_t
3927     /
3928 
3929 A helper function for samepath on windows.
3930 [clinic start generated code]*/
3931 
3932 static PyObject *
os__getfinalpathname_impl(PyObject * module,path_t * path)3933 os__getfinalpathname_impl(PyObject *module, path_t *path)
3934 /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
3935 {
3936     HANDLE hFile;
3937     wchar_t buf[MAXPATHLEN], *target_path = buf;
3938     int buf_size = Py_ARRAY_LENGTH(buf);
3939     int result_length;
3940     PyObject *result;
3941 
3942     Py_BEGIN_ALLOW_THREADS
3943     hFile = CreateFileW(
3944         path->wide,
3945         0, /* desired access */
3946         0, /* share mode */
3947         NULL, /* security attributes */
3948         OPEN_EXISTING,
3949         /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3950         FILE_FLAG_BACKUP_SEMANTICS,
3951         NULL);
3952     Py_END_ALLOW_THREADS
3953 
3954     if (hFile == INVALID_HANDLE_VALUE) {
3955         return win32_error_object("CreateFileW", path->object);
3956     }
3957 
3958     /* We have a good handle to the target, use it to determine the
3959        target path name. */
3960     while (1) {
3961         Py_BEGIN_ALLOW_THREADS
3962         result_length = GetFinalPathNameByHandleW(hFile, target_path,
3963                                                   buf_size, VOLUME_NAME_DOS);
3964         Py_END_ALLOW_THREADS
3965 
3966         if (!result_length) {
3967             result = win32_error_object("GetFinalPathNameByHandleW",
3968                                          path->object);
3969             goto cleanup;
3970         }
3971 
3972         if (result_length < buf_size) {
3973             break;
3974         }
3975 
3976         wchar_t *tmp;
3977         tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3978                             result_length * sizeof(*tmp));
3979         if (!tmp) {
3980             result = PyErr_NoMemory();
3981             goto cleanup;
3982         }
3983 
3984         buf_size = result_length;
3985         target_path = tmp;
3986     }
3987 
3988     result = PyUnicode_FromWideChar(target_path, result_length);
3989     if (result && path->narrow) {
3990         Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
3991     }
3992 
3993 cleanup:
3994     if (target_path != buf) {
3995         PyMem_Free(target_path);
3996     }
3997     CloseHandle(hFile);
3998     return result;
3999 }
4000 
4001 
4002 /*[clinic input]
4003 os._getvolumepathname
4004 
4005     path: path_t
4006 
4007 A helper function for ismount on Win32.
4008 [clinic start generated code]*/
4009 
4010 static PyObject *
os__getvolumepathname_impl(PyObject * module,path_t * path)4011 os__getvolumepathname_impl(PyObject *module, path_t *path)
4012 /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
4013 {
4014     PyObject *result;
4015     wchar_t *mountpath=NULL;
4016     size_t buflen;
4017     BOOL ret;
4018 
4019     /* Volume path should be shorter than entire path */
4020     buflen = Py_MAX(path->length, MAX_PATH);
4021 
4022     if (buflen > PY_DWORD_MAX) {
4023         PyErr_SetString(PyExc_OverflowError, "path too long");
4024         return NULL;
4025     }
4026 
4027     mountpath = PyMem_New(wchar_t, buflen);
4028     if (mountpath == NULL)
4029         return PyErr_NoMemory();
4030 
4031     Py_BEGIN_ALLOW_THREADS
4032     ret = GetVolumePathNameW(path->wide, mountpath,
4033                              Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
4034     Py_END_ALLOW_THREADS
4035 
4036     if (!ret) {
4037         result = win32_error_object("_getvolumepathname", path->object);
4038         goto exit;
4039     }
4040     result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
4041     if (path->narrow)
4042         Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
4043 
4044 exit:
4045     PyMem_Free(mountpath);
4046     return result;
4047 }
4048 
4049 #endif /* MS_WINDOWS */
4050 
4051 
4052 /*[clinic input]
4053 os.mkdir
4054 
4055     path : path_t
4056 
4057     mode: int = 0o777
4058 
4059     *
4060 
4061     dir_fd : dir_fd(requires='mkdirat') = None
4062 
4063 # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4064 
4065 Create a directory.
4066 
4067 If dir_fd is not None, it should be a file descriptor open to a directory,
4068   and path should be relative; path will then be relative to that directory.
4069 dir_fd may not be implemented on your platform.
4070   If it is unavailable, using it will raise a NotImplementedError.
4071 
4072 The mode argument is ignored on Windows.
4073 [clinic start generated code]*/
4074 
4075 static PyObject *
os_mkdir_impl(PyObject * module,path_t * path,int mode,int dir_fd)4076 os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4077 /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
4078 {
4079     int result;
4080 
4081     if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
4082                     dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4083         return NULL;
4084     }
4085 
4086 #ifdef MS_WINDOWS
4087     Py_BEGIN_ALLOW_THREADS
4088     result = CreateDirectoryW(path->wide, NULL);
4089     Py_END_ALLOW_THREADS
4090 
4091     if (!result)
4092         return path_error(path);
4093 #else
4094     Py_BEGIN_ALLOW_THREADS
4095 #if HAVE_MKDIRAT
4096     if (dir_fd != DEFAULT_DIR_FD)
4097         result = mkdirat(dir_fd, path->narrow, mode);
4098     else
4099 #endif
4100 #if defined(__WATCOMC__) && !defined(__QNX__)
4101         result = mkdir(path->narrow);
4102 #else
4103         result = mkdir(path->narrow, mode);
4104 #endif
4105     Py_END_ALLOW_THREADS
4106     if (result < 0)
4107         return path_error(path);
4108 #endif /* MS_WINDOWS */
4109     Py_RETURN_NONE;
4110 }
4111 
4112 
4113 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4114 #if defined(HAVE_SYS_RESOURCE_H)
4115 #include <sys/resource.h>
4116 #endif
4117 
4118 
4119 #ifdef HAVE_NICE
4120 /*[clinic input]
4121 os.nice
4122 
4123     increment: int
4124     /
4125 
4126 Add increment to the priority of process and return the new priority.
4127 [clinic start generated code]*/
4128 
4129 static PyObject *
os_nice_impl(PyObject * module,int increment)4130 os_nice_impl(PyObject *module, int increment)
4131 /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
4132 {
4133     int value;
4134 
4135     /* There are two flavours of 'nice': one that returns the new
4136        priority (as required by almost all standards out there) and the
4137        Linux/FreeBSD one, which returns '0' on success and advices
4138        the use of getpriority() to get the new priority.
4139 
4140        If we are of the nice family that returns the new priority, we
4141        need to clear errno before the call, and check if errno is filled
4142        before calling posix_error() on a returnvalue of -1, because the
4143        -1 may be the actual new priority! */
4144 
4145     errno = 0;
4146     value = nice(increment);
4147 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
4148     if (value == 0)
4149         value = getpriority(PRIO_PROCESS, 0);
4150 #endif
4151     if (value == -1 && errno != 0)
4152         /* either nice() or getpriority() returned an error */
4153         return posix_error();
4154     return PyLong_FromLong((long) value);
4155 }
4156 #endif /* HAVE_NICE */
4157 
4158 
4159 #ifdef HAVE_GETPRIORITY
4160 /*[clinic input]
4161 os.getpriority
4162 
4163     which: int
4164     who: int
4165 
4166 Return program scheduling priority.
4167 [clinic start generated code]*/
4168 
4169 static PyObject *
os_getpriority_impl(PyObject * module,int which,int who)4170 os_getpriority_impl(PyObject *module, int which, int who)
4171 /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
4172 {
4173     int retval;
4174 
4175     errno = 0;
4176     retval = getpriority(which, who);
4177     if (errno != 0)
4178         return posix_error();
4179     return PyLong_FromLong((long)retval);
4180 }
4181 #endif /* HAVE_GETPRIORITY */
4182 
4183 
4184 #ifdef HAVE_SETPRIORITY
4185 /*[clinic input]
4186 os.setpriority
4187 
4188     which: int
4189     who: int
4190     priority: int
4191 
4192 Set program scheduling priority.
4193 [clinic start generated code]*/
4194 
4195 static PyObject *
os_setpriority_impl(PyObject * module,int which,int who,int priority)4196 os_setpriority_impl(PyObject *module, int which, int who, int priority)
4197 /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
4198 {
4199     int retval;
4200 
4201     retval = setpriority(which, who, priority);
4202     if (retval == -1)
4203         return posix_error();
4204     Py_RETURN_NONE;
4205 }
4206 #endif /* HAVE_SETPRIORITY */
4207 
4208 
4209 static PyObject *
internal_rename(path_t * src,path_t * dst,int src_dir_fd,int dst_dir_fd,int is_replace)4210 internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace)
4211 {
4212     const char *function_name = is_replace ? "replace" : "rename";
4213     int dir_fd_specified;
4214 
4215 #ifdef MS_WINDOWS
4216     BOOL result;
4217     int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
4218 #else
4219     int result;
4220 #endif
4221 
4222     dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4223                        (dst_dir_fd != DEFAULT_DIR_FD);
4224 #ifndef HAVE_RENAMEAT
4225     if (dir_fd_specified) {
4226         argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
4227         return NULL;
4228     }
4229 #endif
4230 
4231     if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
4232                     src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
4233                     dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
4234         return NULL;
4235     }
4236 
4237 #ifdef MS_WINDOWS
4238     Py_BEGIN_ALLOW_THREADS
4239     result = MoveFileExW(src->wide, dst->wide, flags);
4240     Py_END_ALLOW_THREADS
4241 
4242     if (!result)
4243         return path_error2(src, dst);
4244 
4245 #else
4246     if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4247         PyErr_Format(PyExc_ValueError,
4248                      "%s: src and dst must be the same type", function_name);
4249         return NULL;
4250     }
4251 
4252     Py_BEGIN_ALLOW_THREADS
4253 #ifdef HAVE_RENAMEAT
4254     if (dir_fd_specified)
4255         result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
4256     else
4257 #endif
4258     result = rename(src->narrow, dst->narrow);
4259     Py_END_ALLOW_THREADS
4260 
4261     if (result)
4262         return path_error2(src, dst);
4263 #endif
4264     Py_RETURN_NONE;
4265 }
4266 
4267 
4268 /*[clinic input]
4269 os.rename
4270 
4271     src : path_t
4272     dst : path_t
4273     *
4274     src_dir_fd : dir_fd = None
4275     dst_dir_fd : dir_fd = None
4276 
4277 Rename a file or directory.
4278 
4279 If either src_dir_fd or dst_dir_fd is not None, it should be a file
4280   descriptor open to a directory, and the respective path string (src or dst)
4281   should be relative; the path will then be relative to that directory.
4282 src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4283   If they are unavailable, using them will raise a NotImplementedError.
4284 [clinic start generated code]*/
4285 
4286 static PyObject *
os_rename_impl(PyObject * module,path_t * src,path_t * dst,int src_dir_fd,int dst_dir_fd)4287 os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4288                int dst_dir_fd)
4289 /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
4290 {
4291     return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
4292 }
4293 
4294 
4295 /*[clinic input]
4296 os.replace = os.rename
4297 
4298 Rename a file or directory, overwriting the destination.
4299 
4300 If either src_dir_fd or dst_dir_fd is not None, it should be a file
4301   descriptor open to a directory, and the respective path string (src or dst)
4302   should be relative; the path will then be relative to that directory.
4303 src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4304   If they are unavailable, using them will raise a NotImplementedError.
4305 [clinic start generated code]*/
4306 
4307 static PyObject *
os_replace_impl(PyObject * module,path_t * src,path_t * dst,int src_dir_fd,int dst_dir_fd)4308 os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4309                 int dst_dir_fd)
4310 /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
4311 {
4312     return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4313 }
4314 
4315 
4316 /*[clinic input]
4317 os.rmdir
4318 
4319     path: path_t
4320     *
4321     dir_fd: dir_fd(requires='unlinkat') = None
4322 
4323 Remove a directory.
4324 
4325 If dir_fd is not None, it should be a file descriptor open to a directory,
4326   and path should be relative; path will then be relative to that directory.
4327 dir_fd may not be implemented on your platform.
4328   If it is unavailable, using it will raise a NotImplementedError.
4329 [clinic start generated code]*/
4330 
4331 static PyObject *
os_rmdir_impl(PyObject * module,path_t * path,int dir_fd)4332 os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4333 /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
4334 {
4335     int result;
4336 
4337     if (PySys_Audit("os.rmdir", "Oi", path->object,
4338                     dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4339         return NULL;
4340     }
4341 
4342     Py_BEGIN_ALLOW_THREADS
4343 #ifdef MS_WINDOWS
4344     /* Windows, success=1, UNIX, success=0 */
4345     result = !RemoveDirectoryW(path->wide);
4346 #else
4347 #ifdef HAVE_UNLINKAT
4348     if (dir_fd != DEFAULT_DIR_FD)
4349         result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
4350     else
4351 #endif
4352         result = rmdir(path->narrow);
4353 #endif
4354     Py_END_ALLOW_THREADS
4355 
4356     if (result)
4357         return path_error(path);
4358 
4359     Py_RETURN_NONE;
4360 }
4361 
4362 
4363 #ifdef HAVE_SYSTEM
4364 #ifdef MS_WINDOWS
4365 /*[clinic input]
4366 os.system -> long
4367 
4368     command: Py_UNICODE
4369 
4370 Execute the command in a subshell.
4371 [clinic start generated code]*/
4372 
4373 static long
os_system_impl(PyObject * module,const Py_UNICODE * command)4374 os_system_impl(PyObject *module, const Py_UNICODE *command)
4375 /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
4376 {
4377     long result;
4378 
4379     if (PySys_Audit("os.system", "(u)", command) < 0) {
4380         return -1;
4381     }
4382 
4383     Py_BEGIN_ALLOW_THREADS
4384     _Py_BEGIN_SUPPRESS_IPH
4385     result = _wsystem(command);
4386     _Py_END_SUPPRESS_IPH
4387     Py_END_ALLOW_THREADS
4388     return result;
4389 }
4390 #else /* MS_WINDOWS */
4391 /*[clinic input]
4392 os.system -> long
4393 
4394     command: FSConverter
4395 
4396 Execute the command in a subshell.
4397 [clinic start generated code]*/
4398 
4399 static long
os_system_impl(PyObject * module,PyObject * command)4400 os_system_impl(PyObject *module, PyObject *command)
4401 /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
4402 {
4403     long result;
4404     const char *bytes = PyBytes_AsString(command);
4405 
4406     if (PySys_Audit("os.system", "(O)", command) < 0) {
4407         return -1;
4408     }
4409 
4410     Py_BEGIN_ALLOW_THREADS
4411     result = system(bytes);
4412     Py_END_ALLOW_THREADS
4413     return result;
4414 }
4415 #endif
4416 #endif /* HAVE_SYSTEM */
4417 
4418 
4419 /*[clinic input]
4420 os.umask
4421 
4422     mask: int
4423     /
4424 
4425 Set the current numeric umask and return the previous umask.
4426 [clinic start generated code]*/
4427 
4428 static PyObject *
os_umask_impl(PyObject * module,int mask)4429 os_umask_impl(PyObject *module, int mask)
4430 /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
4431 {
4432     int i = (int)umask(mask);
4433     if (i < 0)
4434         return posix_error();
4435     return PyLong_FromLong((long)i);
4436 }
4437 
4438 #ifdef MS_WINDOWS
4439 
4440 /* override the default DeleteFileW behavior so that directory
4441 symlinks can be removed with this function, the same as with
4442 Unix symlinks */
Py_DeleteFileW(LPCWSTR lpFileName)4443 BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4444 {
4445     WIN32_FILE_ATTRIBUTE_DATA info;
4446     WIN32_FIND_DATAW find_data;
4447     HANDLE find_data_handle;
4448     int is_directory = 0;
4449     int is_link = 0;
4450 
4451     if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4452         is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
4453 
4454         /* Get WIN32_FIND_DATA structure for the path to determine if
4455            it is a symlink */
4456         if(is_directory &&
4457            info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4458             find_data_handle = FindFirstFileW(lpFileName, &find_data);
4459 
4460             if(find_data_handle != INVALID_HANDLE_VALUE) {
4461                 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4462                    IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4463                 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4464                           find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
4465                 FindClose(find_data_handle);
4466             }
4467         }
4468     }
4469 
4470     if (is_directory && is_link)
4471         return RemoveDirectoryW(lpFileName);
4472 
4473     return DeleteFileW(lpFileName);
4474 }
4475 #endif /* MS_WINDOWS */
4476 
4477 
4478 /*[clinic input]
4479 os.unlink
4480 
4481     path: path_t
4482     *
4483     dir_fd: dir_fd(requires='unlinkat')=None
4484 
4485 Remove a file (same as remove()).
4486 
4487 If dir_fd is not None, it should be a file descriptor open to a directory,
4488   and path should be relative; path will then be relative to that directory.
4489 dir_fd may not be implemented on your platform.
4490   If it is unavailable, using it will raise a NotImplementedError.
4491 
4492 [clinic start generated code]*/
4493 
4494 static PyObject *
os_unlink_impl(PyObject * module,path_t * path,int dir_fd)4495 os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4496 /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
4497 {
4498     int result;
4499 
4500     if (PySys_Audit("os.remove", "Oi", path->object,
4501                     dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4502         return NULL;
4503     }
4504 
4505     Py_BEGIN_ALLOW_THREADS
4506     _Py_BEGIN_SUPPRESS_IPH
4507 #ifdef MS_WINDOWS
4508     /* Windows, success=1, UNIX, success=0 */
4509     result = !Py_DeleteFileW(path->wide);
4510 #else
4511 #ifdef HAVE_UNLINKAT
4512     if (dir_fd != DEFAULT_DIR_FD)
4513         result = unlinkat(dir_fd, path->narrow, 0);
4514     else
4515 #endif /* HAVE_UNLINKAT */
4516         result = unlink(path->narrow);
4517 #endif
4518     _Py_END_SUPPRESS_IPH
4519     Py_END_ALLOW_THREADS
4520 
4521     if (result)
4522         return path_error(path);
4523 
4524     Py_RETURN_NONE;
4525 }
4526 
4527 
4528 /*[clinic input]
4529 os.remove = os.unlink
4530 
4531 Remove a file (same as unlink()).
4532 
4533 If dir_fd is not None, it should be a file descriptor open to a directory,
4534   and path should be relative; path will then be relative to that directory.
4535 dir_fd may not be implemented on your platform.
4536   If it is unavailable, using it will raise a NotImplementedError.
4537 [clinic start generated code]*/
4538 
4539 static PyObject *
os_remove_impl(PyObject * module,path_t * path,int dir_fd)4540 os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4541 /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
4542 {
4543     return os_unlink_impl(module, path, dir_fd);
4544 }
4545 
4546 
4547 static PyStructSequence_Field uname_result_fields[] = {
4548     {"sysname",    "operating system name"},
4549     {"nodename",   "name of machine on network (implementation-defined)"},
4550     {"release",    "operating system release"},
4551     {"version",    "operating system version"},
4552     {"machine",    "hardware identifier"},
4553     {NULL}
4554 };
4555 
4556 PyDoc_STRVAR(uname_result__doc__,
4557 "uname_result: Result from os.uname().\n\n\
4558 This object may be accessed either as a tuple of\n\
4559   (sysname, nodename, release, version, machine),\n\
4560 or via the attributes sysname, nodename, release, version, and machine.\n\
4561 \n\
4562 See os.uname for more information.");
4563 
4564 static PyStructSequence_Desc uname_result_desc = {
4565     "uname_result", /* name */
4566     uname_result__doc__, /* doc */
4567     uname_result_fields,
4568     5
4569 };
4570 
4571 static PyTypeObject* UnameResultType;
4572 
4573 
4574 #ifdef HAVE_UNAME
4575 /*[clinic input]
4576 os.uname
4577 
4578 Return an object identifying the current operating system.
4579 
4580 The object behaves like a named tuple with the following fields:
4581   (sysname, nodename, release, version, machine)
4582 
4583 [clinic start generated code]*/
4584 
4585 static PyObject *
os_uname_impl(PyObject * module)4586 os_uname_impl(PyObject *module)
4587 /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
4588 {
4589     struct utsname u;
4590     int res;
4591     PyObject *value;
4592 
4593     Py_BEGIN_ALLOW_THREADS
4594     res = uname(&u);
4595     Py_END_ALLOW_THREADS
4596     if (res < 0)
4597         return posix_error();
4598 
4599     value = PyStructSequence_New(UnameResultType);
4600     if (value == NULL)
4601         return NULL;
4602 
4603 #define SET(i, field) \
4604     { \
4605     PyObject *o = PyUnicode_DecodeFSDefault(field); \
4606     if (!o) { \
4607         Py_DECREF(value); \
4608         return NULL; \
4609     } \
4610     PyStructSequence_SET_ITEM(value, i, o); \
4611     } \
4612 
4613     SET(0, u.sysname);
4614     SET(1, u.nodename);
4615     SET(2, u.release);
4616     SET(3, u.version);
4617     SET(4, u.machine);
4618 
4619 #undef SET
4620 
4621     return value;
4622 }
4623 #endif /* HAVE_UNAME */
4624 
4625 
4626 
4627 typedef struct {
4628     int    now;
4629     time_t atime_s;
4630     long   atime_ns;
4631     time_t mtime_s;
4632     long   mtime_ns;
4633 } utime_t;
4634 
4635 /*
4636  * these macros assume that "ut" is a pointer to a utime_t
4637  * they also intentionally leak the declaration of a pointer named "time"
4638  */
4639 #define UTIME_TO_TIMESPEC \
4640     struct timespec ts[2]; \
4641     struct timespec *time; \
4642     if (ut->now) \
4643         time = NULL; \
4644     else { \
4645         ts[0].tv_sec = ut->atime_s; \
4646         ts[0].tv_nsec = ut->atime_ns; \
4647         ts[1].tv_sec = ut->mtime_s; \
4648         ts[1].tv_nsec = ut->mtime_ns; \
4649         time = ts; \
4650     } \
4651 
4652 #define UTIME_TO_TIMEVAL \
4653     struct timeval tv[2]; \
4654     struct timeval *time; \
4655     if (ut->now) \
4656         time = NULL; \
4657     else { \
4658         tv[0].tv_sec = ut->atime_s; \
4659         tv[0].tv_usec = ut->atime_ns / 1000; \
4660         tv[1].tv_sec = ut->mtime_s; \
4661         tv[1].tv_usec = ut->mtime_ns / 1000; \
4662         time = tv; \
4663     } \
4664 
4665 #define UTIME_TO_UTIMBUF \
4666     struct utimbuf u; \
4667     struct utimbuf *time; \
4668     if (ut->now) \
4669         time = NULL; \
4670     else { \
4671         u.actime = ut->atime_s; \
4672         u.modtime = ut->mtime_s; \
4673         time = &u; \
4674     }
4675 
4676 #define UTIME_TO_TIME_T \
4677     time_t timet[2]; \
4678     time_t *time; \
4679     if (ut->now) \
4680         time = NULL; \
4681     else { \
4682         timet[0] = ut->atime_s; \
4683         timet[1] = ut->mtime_s; \
4684         time = timet; \
4685     } \
4686 
4687 
4688 #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
4689 
4690 static int
utime_dir_fd(utime_t * ut,int dir_fd,const char * path,int follow_symlinks)4691 utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
4692 {
4693 #ifdef HAVE_UTIMENSAT
4694     int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4695     UTIME_TO_TIMESPEC;
4696     return utimensat(dir_fd, path, time, flags);
4697 #elif defined(HAVE_FUTIMESAT)
4698     UTIME_TO_TIMEVAL;
4699     /*
4700      * follow_symlinks will never be false here;
4701      * we only allow !follow_symlinks and dir_fd together
4702      * if we have utimensat()
4703      */
4704     assert(follow_symlinks);
4705     return futimesat(dir_fd, path, time);
4706 #endif
4707 }
4708 
4709     #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4710 #else
4711     #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
4712 #endif
4713 
4714 #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
4715 
4716 static int
utime_fd(utime_t * ut,int fd)4717 utime_fd(utime_t *ut, int fd)
4718 {
4719 #ifdef HAVE_FUTIMENS
4720     UTIME_TO_TIMESPEC;
4721     return futimens(fd, time);
4722 #else
4723     UTIME_TO_TIMEVAL;
4724     return futimes(fd, time);
4725 #endif
4726 }
4727 
4728     #define PATH_UTIME_HAVE_FD 1
4729 #else
4730     #define PATH_UTIME_HAVE_FD 0
4731 #endif
4732 
4733 #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4734 #  define UTIME_HAVE_NOFOLLOW_SYMLINKS
4735 #endif
4736 
4737 #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
4738 
4739 static int
utime_nofollow_symlinks(utime_t * ut,const char * path)4740 utime_nofollow_symlinks(utime_t *ut, const char *path)
4741 {
4742 #ifdef HAVE_UTIMENSAT
4743     UTIME_TO_TIMESPEC;
4744     return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4745 #else
4746     UTIME_TO_TIMEVAL;
4747     return lutimes(path, time);
4748 #endif
4749 }
4750 
4751 #endif
4752 
4753 #ifndef MS_WINDOWS
4754 
4755 static int
utime_default(utime_t * ut,const char * path)4756 utime_default(utime_t *ut, const char *path)
4757 {
4758 #ifdef HAVE_UTIMENSAT
4759     UTIME_TO_TIMESPEC;
4760     return utimensat(DEFAULT_DIR_FD, path, time, 0);
4761 #elif defined(HAVE_UTIMES)
4762     UTIME_TO_TIMEVAL;
4763     return utimes(path, time);
4764 #elif defined(HAVE_UTIME_H)
4765     UTIME_TO_UTIMBUF;
4766     return utime(path, time);
4767 #else
4768     UTIME_TO_TIME_T;
4769     return utime(path, time);
4770 #endif
4771 }
4772 
4773 #endif
4774 
4775 static int
split_py_long_to_s_and_ns(PyObject * py_long,time_t * s,long * ns)4776 split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4777 {
4778     int result = 0;
4779     PyObject *divmod;
4780     divmod = PyNumber_Divmod(py_long, billion);
4781     if (!divmod)
4782         goto exit;
4783     if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4784         PyErr_Format(PyExc_TypeError,
4785                      "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4786                      Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4787         goto exit;
4788     }
4789     *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4790     if ((*s == -1) && PyErr_Occurred())
4791         goto exit;
4792     *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
4793     if ((*ns == -1) && PyErr_Occurred())
4794         goto exit;
4795 
4796     result = 1;
4797 exit:
4798     Py_XDECREF(divmod);
4799     return result;
4800 }
4801 
4802 
4803 /*[clinic input]
4804 os.utime
4805 
4806     path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4807     times: object = None
4808     *
4809     ns: object = NULL
4810     dir_fd: dir_fd(requires='futimensat') = None
4811     follow_symlinks: bool=True
4812 
4813 # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
4814 
4815 Set the access and modified time of path.
4816 
4817 path may always be specified as a string.
4818 On some platforms, path may also be specified as an open file descriptor.
4819   If this functionality is unavailable, using it raises an exception.
4820 
4821 If times is not None, it must be a tuple (atime, mtime);
4822     atime and mtime should be expressed as float seconds since the epoch.
4823 If ns is specified, it must be a tuple (atime_ns, mtime_ns);
4824     atime_ns and mtime_ns should be expressed as integer nanoseconds
4825     since the epoch.
4826 If times is None and ns is unspecified, utime uses the current time.
4827 Specifying tuples for both times and ns is an error.
4828 
4829 If dir_fd is not None, it should be a file descriptor open to a directory,
4830   and path should be relative; path will then be relative to that directory.
4831 If follow_symlinks is False, and the last element of the path is a symbolic
4832   link, utime will modify the symbolic link itself instead of the file the
4833   link points to.
4834 It is an error to use dir_fd or follow_symlinks when specifying path
4835   as an open file descriptor.
4836 dir_fd and follow_symlinks may not be available on your platform.
4837   If they are unavailable, using them will raise a NotImplementedError.
4838 
4839 [clinic start generated code]*/
4840 
4841 static PyObject *
os_utime_impl(PyObject * module,path_t * path,PyObject * times,PyObject * ns,int dir_fd,int follow_symlinks)4842 os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4843               int dir_fd, int follow_symlinks)
4844 /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
4845 {
4846 #ifdef MS_WINDOWS
4847     HANDLE hFile;
4848     FILETIME atime, mtime;
4849 #else
4850     int result;
4851 #endif
4852 
4853     utime_t utime;
4854 
4855     memset(&utime, 0, sizeof(utime_t));
4856 
4857     if (times != Py_None && ns) {
4858         PyErr_SetString(PyExc_ValueError,
4859                      "utime: you may specify either 'times'"
4860                      " or 'ns' but not both");
4861         return NULL;
4862     }
4863 
4864     if (times != Py_None) {
4865         time_t a_sec, m_sec;
4866         long a_nsec, m_nsec;
4867         if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
4868             PyErr_SetString(PyExc_TypeError,
4869                          "utime: 'times' must be either"
4870                          " a tuple of two ints or None");
4871             return NULL;
4872         }
4873         utime.now = 0;
4874         if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
4875                                      &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
4876             _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
4877                                      &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
4878             return NULL;
4879         }
4880         utime.atime_s = a_sec;
4881         utime.atime_ns = a_nsec;
4882         utime.mtime_s = m_sec;
4883         utime.mtime_ns = m_nsec;
4884     }
4885     else if (ns) {
4886         if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
4887             PyErr_SetString(PyExc_TypeError,
4888                          "utime: 'ns' must be a tuple of two ints");
4889             return NULL;
4890         }
4891         utime.now = 0;
4892         if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
4893                                       &utime.atime_s, &utime.atime_ns) ||
4894             !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
4895                                        &utime.mtime_s, &utime.mtime_ns)) {
4896             return NULL;
4897         }
4898     }
4899     else {
4900         /* times and ns are both None/unspecified. use "now". */
4901         utime.now = 1;
4902     }
4903 
4904 #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
4905     if (follow_symlinks_specified("utime", follow_symlinks))
4906         return NULL;
4907 #endif
4908 
4909     if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4910         dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4911         fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
4912         return NULL;
4913 
4914 #if !defined(HAVE_UTIMENSAT)
4915     if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
4916         PyErr_SetString(PyExc_ValueError,
4917                      "utime: cannot use dir_fd and follow_symlinks "
4918                      "together on this platform");
4919         return NULL;
4920     }
4921 #endif
4922 
4923     if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
4924                     dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4925         return NULL;
4926     }
4927 
4928 #ifdef MS_WINDOWS
4929     Py_BEGIN_ALLOW_THREADS
4930     hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4931                         NULL, OPEN_EXISTING,
4932                         FILE_FLAG_BACKUP_SEMANTICS, NULL);
4933     Py_END_ALLOW_THREADS
4934     if (hFile == INVALID_HANDLE_VALUE) {
4935         path_error(path);
4936         return NULL;
4937     }
4938 
4939     if (utime.now) {
4940         GetSystemTimeAsFileTime(&mtime);
4941         atime = mtime;
4942     }
4943     else {
4944         _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4945         _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
4946     }
4947     if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4948         /* Avoid putting the file name into the error here,
4949            as that may confuse the user into believing that
4950            something is wrong with the file, when it also
4951            could be the time stamp that gives a problem. */
4952         PyErr_SetFromWindowsErr(0);
4953         CloseHandle(hFile);
4954         return NULL;
4955     }
4956     CloseHandle(hFile);
4957 #else /* MS_WINDOWS */
4958     Py_BEGIN_ALLOW_THREADS
4959 
4960 #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
4961     if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
4962         result = utime_nofollow_symlinks(&utime, path->narrow);
4963     else
4964 #endif
4965 
4966 #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
4967     if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
4968         result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
4969     else
4970 #endif
4971 
4972 #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
4973     if (path->fd != -1)
4974         result = utime_fd(&utime, path->fd);
4975     else
4976 #endif
4977 
4978     result = utime_default(&utime, path->narrow);
4979 
4980     Py_END_ALLOW_THREADS
4981 
4982     if (result < 0) {
4983         /* see previous comment about not putting filename in error here */
4984         posix_error();
4985         return NULL;
4986     }
4987 
4988 #endif /* MS_WINDOWS */
4989 
4990     Py_RETURN_NONE;
4991 }
4992 
4993 /* Process operations */
4994 
4995 
4996 /*[clinic input]
4997 os._exit
4998 
4999     status: int
5000 
5001 Exit to the system with specified status, without normal exit processing.
5002 [clinic start generated code]*/
5003 
5004 static PyObject *
os__exit_impl(PyObject * module,int status)5005 os__exit_impl(PyObject *module, int status)
5006 /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
5007 {
5008     _exit(status);
5009     return NULL; /* Make gcc -Wall happy */
5010 }
5011 
5012 #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5013 #define EXECV_CHAR wchar_t
5014 #else
5015 #define EXECV_CHAR char
5016 #endif
5017 
5018 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
5019 static void
free_string_array(EXECV_CHAR ** array,Py_ssize_t count)5020 free_string_array(EXECV_CHAR **array, Py_ssize_t count)
5021 {
5022     Py_ssize_t i;
5023     for (i = 0; i < count; i++)
5024         PyMem_Free(array[i]);
5025     PyMem_DEL(array);
5026 }
5027 
5028 static int
fsconvert_strdup(PyObject * o,EXECV_CHAR ** out)5029 fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
5030 {
5031     Py_ssize_t size;
5032     PyObject *ub;
5033     int result = 0;
5034 #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5035     if (!PyUnicode_FSDecoder(o, &ub))
5036         return 0;
5037     *out = PyUnicode_AsWideCharString(ub, &size);
5038     if (*out)
5039         result = 1;
5040 #else
5041     if (!PyUnicode_FSConverter(o, &ub))
5042         return 0;
5043     size = PyBytes_GET_SIZE(ub);
5044     *out = PyMem_Malloc(size + 1);
5045     if (*out) {
5046         memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5047         result = 1;
5048     } else
5049         PyErr_NoMemory();
5050 #endif
5051     Py_DECREF(ub);
5052     return result;
5053 }
5054 #endif
5055 
5056 #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
5057 static EXECV_CHAR**
parse_envlist(PyObject * env,Py_ssize_t * envc_ptr)5058 parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5059 {
5060     Py_ssize_t i, pos, envc;
5061     PyObject *keys=NULL, *vals=NULL;
5062     PyObject *key, *val, *key2, *val2, *keyval;
5063     EXECV_CHAR **envlist;
5064 
5065     i = PyMapping_Size(env);
5066     if (i < 0)
5067         return NULL;
5068     envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
5069     if (envlist == NULL) {
5070         PyErr_NoMemory();
5071         return NULL;
5072     }
5073     envc = 0;
5074     keys = PyMapping_Keys(env);
5075     if (!keys)
5076         goto error;
5077     vals = PyMapping_Values(env);
5078     if (!vals)
5079         goto error;
5080     if (!PyList_Check(keys) || !PyList_Check(vals)) {
5081         PyErr_Format(PyExc_TypeError,
5082                      "env.keys() or env.values() is not a list");
5083         goto error;
5084     }
5085 
5086     for (pos = 0; pos < i; pos++) {
5087         key = PyList_GetItem(keys, pos);
5088         val = PyList_GetItem(vals, pos);
5089         if (!key || !val)
5090             goto error;
5091 
5092 #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5093         if (!PyUnicode_FSDecoder(key, &key2))
5094             goto error;
5095         if (!PyUnicode_FSDecoder(val, &val2)) {
5096             Py_DECREF(key2);
5097             goto error;
5098         }
5099         /* Search from index 1 because on Windows starting '=' is allowed for
5100            defining hidden environment variables. */
5101         if (PyUnicode_GET_LENGTH(key2) == 0 ||
5102             PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5103         {
5104             PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
5105             Py_DECREF(key2);
5106             Py_DECREF(val2);
5107             goto error;
5108         }
5109         keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5110 #else
5111         if (!PyUnicode_FSConverter(key, &key2))
5112             goto error;
5113         if (!PyUnicode_FSConverter(val, &val2)) {
5114             Py_DECREF(key2);
5115             goto error;
5116         }
5117         if (PyBytes_GET_SIZE(key2) == 0 ||
5118             strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5119         {
5120             PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
5121             Py_DECREF(key2);
5122             Py_DECREF(val2);
5123             goto error;
5124         }
5125         keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5126                                              PyBytes_AS_STRING(val2));
5127 #endif
5128         Py_DECREF(key2);
5129         Py_DECREF(val2);
5130         if (!keyval)
5131             goto error;
5132 
5133         if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5134             Py_DECREF(keyval);
5135             goto error;
5136         }
5137 
5138         Py_DECREF(keyval);
5139     }
5140     Py_DECREF(vals);
5141     Py_DECREF(keys);
5142 
5143     envlist[envc] = 0;
5144     *envc_ptr = envc;
5145     return envlist;
5146 
5147 error:
5148     Py_XDECREF(keys);
5149     Py_XDECREF(vals);
5150     free_string_array(envlist, envc);
5151     return NULL;
5152 }
5153 
5154 static EXECV_CHAR**
parse_arglist(PyObject * argv,Py_ssize_t * argc)5155 parse_arglist(PyObject* argv, Py_ssize_t *argc)
5156 {
5157     int i;
5158     EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
5159     if (argvlist == NULL) {
5160         PyErr_NoMemory();
5161         return NULL;
5162     }
5163     for (i = 0; i < *argc; i++) {
5164         PyObject* item = PySequence_ITEM(argv, i);
5165         if (item == NULL)
5166             goto fail;
5167         if (!fsconvert_strdup(item, &argvlist[i])) {
5168             Py_DECREF(item);
5169             goto fail;
5170         }
5171         Py_DECREF(item);
5172     }
5173     argvlist[*argc] = NULL;
5174     return argvlist;
5175 fail:
5176     *argc = i;
5177     free_string_array(argvlist, *argc);
5178     return NULL;
5179 }
5180 
5181 #endif
5182 
5183 
5184 #ifdef HAVE_EXECV
5185 /*[clinic input]
5186 os.execv
5187 
5188     path: path_t
5189         Path of executable file.
5190     argv: object
5191         Tuple or list of strings.
5192     /
5193 
5194 Execute an executable path with arguments, replacing current process.
5195 [clinic start generated code]*/
5196 
5197 static PyObject *
os_execv_impl(PyObject * module,path_t * path,PyObject * argv)5198 os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5199 /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
5200 {
5201     EXECV_CHAR **argvlist;
5202     Py_ssize_t argc;
5203 
5204     /* execv has two arguments: (path, argv), where
5205        argv is a list or tuple of strings. */
5206 
5207     if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5208         PyErr_SetString(PyExc_TypeError,
5209                         "execv() arg 2 must be a tuple or list");
5210         return NULL;
5211     }
5212     argc = PySequence_Size(argv);
5213     if (argc < 1) {
5214         PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
5215         return NULL;
5216     }
5217 
5218     argvlist = parse_arglist(argv, &argc);
5219     if (argvlist == NULL) {
5220         return NULL;
5221     }
5222     if (!argvlist[0][0]) {
5223         PyErr_SetString(PyExc_ValueError,
5224             "execv() arg 2 first element cannot be empty");
5225         free_string_array(argvlist, argc);
5226         return NULL;
5227     }
5228 
5229     if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
5230         free_string_array(argvlist, argc);
5231         return NULL;
5232     }
5233 
5234     _Py_BEGIN_SUPPRESS_IPH
5235 #ifdef HAVE_WEXECV
5236     _wexecv(path->wide, argvlist);
5237 #else
5238     execv(path->narrow, argvlist);
5239 #endif
5240     _Py_END_SUPPRESS_IPH
5241 
5242     /* If we get here it's definitely an error */
5243 
5244     free_string_array(argvlist, argc);
5245     return posix_error();
5246 }
5247 
5248 
5249 /*[clinic input]
5250 os.execve
5251 
5252     path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5253         Path of executable file.
5254     argv: object
5255         Tuple or list of strings.
5256     env: object
5257         Dictionary of strings mapping to strings.
5258 
5259 Execute an executable path with arguments, replacing current process.
5260 [clinic start generated code]*/
5261 
5262 static PyObject *
os_execve_impl(PyObject * module,path_t * path,PyObject * argv,PyObject * env)5263 os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5264 /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
5265 {
5266     EXECV_CHAR **argvlist = NULL;
5267     EXECV_CHAR **envlist;
5268     Py_ssize_t argc, envc;
5269 
5270     /* execve has three arguments: (path, argv, env), where
5271        argv is a list or tuple of strings and env is a dictionary
5272        like posix.environ. */
5273 
5274     if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5275         PyErr_SetString(PyExc_TypeError,
5276                         "execve: argv must be a tuple or list");
5277         goto fail_0;
5278     }
5279     argc = PySequence_Size(argv);
5280     if (argc < 1) {
5281         PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5282         return NULL;
5283     }
5284 
5285     if (!PyMapping_Check(env)) {
5286         PyErr_SetString(PyExc_TypeError,
5287                         "execve: environment must be a mapping object");
5288         goto fail_0;
5289     }
5290 
5291     argvlist = parse_arglist(argv, &argc);
5292     if (argvlist == NULL) {
5293         goto fail_0;
5294     }
5295     if (!argvlist[0][0]) {
5296         PyErr_SetString(PyExc_ValueError,
5297             "execve: argv first element cannot be empty");
5298         goto fail_0;
5299     }
5300 
5301     envlist = parse_envlist(env, &envc);
5302     if (envlist == NULL)
5303         goto fail_0;
5304 
5305     if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
5306         goto fail_1;
5307     }
5308 
5309     _Py_BEGIN_SUPPRESS_IPH
5310 #ifdef HAVE_FEXECVE
5311     if (path->fd > -1)
5312         fexecve(path->fd, argvlist, envlist);
5313     else
5314 #endif
5315 #ifdef HAVE_WEXECV
5316         _wexecve(path->wide, argvlist, envlist);
5317 #else
5318         execve(path->narrow, argvlist, envlist);
5319 #endif
5320     _Py_END_SUPPRESS_IPH
5321 
5322     /* If we get here it's definitely an error */
5323 
5324     posix_path_error(path);
5325   fail_1:
5326     free_string_array(envlist, envc);
5327   fail_0:
5328     if (argvlist)
5329         free_string_array(argvlist, argc);
5330     return NULL;
5331 }
5332 
5333 #endif /* HAVE_EXECV */
5334 
5335 #ifdef HAVE_POSIX_SPAWN
5336 
5337 enum posix_spawn_file_actions_identifier {
5338     POSIX_SPAWN_OPEN,
5339     POSIX_SPAWN_CLOSE,
5340     POSIX_SPAWN_DUP2
5341 };
5342 
5343 #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
5344 static int
5345 convert_sched_param(PyObject *param, struct sched_param *res);
5346 #endif
5347 
5348 static int
parse_posix_spawn_flags(const char * func_name,PyObject * setpgroup,int resetids,int setsid,PyObject * setsigmask,PyObject * setsigdef,PyObject * scheduler,posix_spawnattr_t * attrp)5349 parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5350                         int resetids, int setsid, PyObject *setsigmask,
5351                         PyObject *setsigdef, PyObject *scheduler,
5352                         posix_spawnattr_t *attrp)
5353 {
5354     long all_flags = 0;
5355 
5356     errno = posix_spawnattr_init(attrp);
5357     if (errno) {
5358         posix_error();
5359         return -1;
5360     }
5361 
5362     if (setpgroup) {
5363         pid_t pgid = PyLong_AsPid(setpgroup);
5364         if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5365             goto fail;
5366         }
5367         errno = posix_spawnattr_setpgroup(attrp, pgid);
5368         if (errno) {
5369             posix_error();
5370             goto fail;
5371         }
5372         all_flags |= POSIX_SPAWN_SETPGROUP;
5373     }
5374 
5375     if (resetids) {
5376         all_flags |= POSIX_SPAWN_RESETIDS;
5377     }
5378 
5379     if (setsid) {
5380 #ifdef POSIX_SPAWN_SETSID
5381         all_flags |= POSIX_SPAWN_SETSID;
5382 #elif defined(POSIX_SPAWN_SETSID_NP)
5383         all_flags |= POSIX_SPAWN_SETSID_NP;
5384 #else
5385         argument_unavailable_error(func_name, "setsid");
5386         return -1;
5387 #endif
5388     }
5389 
5390    if (setsigmask) {
5391         sigset_t set;
5392         if (!_Py_Sigset_Converter(setsigmask, &set)) {
5393             goto fail;
5394         }
5395         errno = posix_spawnattr_setsigmask(attrp, &set);
5396         if (errno) {
5397             posix_error();
5398             goto fail;
5399         }
5400         all_flags |= POSIX_SPAWN_SETSIGMASK;
5401     }
5402 
5403     if (setsigdef) {
5404         sigset_t set;
5405         if (!_Py_Sigset_Converter(setsigdef, &set)) {
5406             goto fail;
5407         }
5408         errno = posix_spawnattr_setsigdefault(attrp, &set);
5409         if (errno) {
5410             posix_error();
5411             goto fail;
5412         }
5413         all_flags |= POSIX_SPAWN_SETSIGDEF;
5414     }
5415 
5416     if (scheduler) {
5417 #ifdef POSIX_SPAWN_SETSCHEDULER
5418         PyObject *py_schedpolicy;
5419         struct sched_param schedparam;
5420 
5421         if (!PyArg_ParseTuple(scheduler, "OO&"
5422                         ";A scheduler tuple must have two elements",
5423                         &py_schedpolicy, convert_sched_param, &schedparam)) {
5424             goto fail;
5425         }
5426         if (py_schedpolicy != Py_None) {
5427             int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5428 
5429             if (schedpolicy == -1 && PyErr_Occurred()) {
5430                 goto fail;
5431             }
5432             errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5433             if (errno) {
5434                 posix_error();
5435                 goto fail;
5436             }
5437             all_flags |= POSIX_SPAWN_SETSCHEDULER;
5438         }
5439         errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5440         if (errno) {
5441             posix_error();
5442             goto fail;
5443         }
5444         all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5445 #else
5446         PyErr_SetString(PyExc_NotImplementedError,
5447                 "The scheduler option is not supported in this system.");
5448         goto fail;
5449 #endif
5450     }
5451 
5452     errno = posix_spawnattr_setflags(attrp, all_flags);
5453     if (errno) {
5454         posix_error();
5455         goto fail;
5456     }
5457 
5458     return 0;
5459 
5460 fail:
5461     (void)posix_spawnattr_destroy(attrp);
5462     return -1;
5463 }
5464 
5465 static int
parse_file_actions(PyObject * file_actions,posix_spawn_file_actions_t * file_actionsp,PyObject * temp_buffer)5466 parse_file_actions(PyObject *file_actions,
5467                    posix_spawn_file_actions_t *file_actionsp,
5468                    PyObject *temp_buffer)
5469 {
5470     PyObject *seq;
5471     PyObject *file_action = NULL;
5472     PyObject *tag_obj;
5473 
5474     seq = PySequence_Fast(file_actions,
5475                           "file_actions must be a sequence or None");
5476     if (seq == NULL) {
5477         return -1;
5478     }
5479 
5480     errno = posix_spawn_file_actions_init(file_actionsp);
5481     if (errno) {
5482         posix_error();
5483         Py_DECREF(seq);
5484         return -1;
5485     }
5486 
5487     for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
5488         file_action = PySequence_Fast_GET_ITEM(seq, i);
5489         Py_INCREF(file_action);
5490         if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5491             PyErr_SetString(PyExc_TypeError,
5492                 "Each file_actions element must be a non-empty tuple");
5493             goto fail;
5494         }
5495         long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5496         if (tag == -1 && PyErr_Occurred()) {
5497             goto fail;
5498         }
5499 
5500         /* Populate the file_actions object */
5501         switch (tag) {
5502             case POSIX_SPAWN_OPEN: {
5503                 int fd, oflag;
5504                 PyObject *path;
5505                 unsigned long mode;
5506                 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5507                         ";A open file_action tuple must have 5 elements",
5508                         &tag_obj, &fd, PyUnicode_FSConverter, &path,
5509                         &oflag, &mode))
5510                 {
5511                     goto fail;
5512                 }
5513                 if (PyList_Append(temp_buffer, path)) {
5514                     Py_DECREF(path);
5515                     goto fail;
5516                 }
5517                 errno = posix_spawn_file_actions_addopen(file_actionsp,
5518                         fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
5519                 Py_DECREF(path);
5520                 if (errno) {
5521                     posix_error();
5522                     goto fail;
5523                 }
5524                 break;
5525             }
5526             case POSIX_SPAWN_CLOSE: {
5527                 int fd;
5528                 if (!PyArg_ParseTuple(file_action, "Oi"
5529                         ";A close file_action tuple must have 2 elements",
5530                         &tag_obj, &fd))
5531                 {
5532                     goto fail;
5533                 }
5534                 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5535                 if (errno) {
5536                     posix_error();
5537                     goto fail;
5538                 }
5539                 break;
5540             }
5541             case POSIX_SPAWN_DUP2: {
5542                 int fd1, fd2;
5543                 if (!PyArg_ParseTuple(file_action, "Oii"
5544                         ";A dup2 file_action tuple must have 3 elements",
5545                         &tag_obj, &fd1, &fd2))
5546                 {
5547                     goto fail;
5548                 }
5549                 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5550                                                          fd1, fd2);
5551                 if (errno) {
5552                     posix_error();
5553                     goto fail;
5554                 }
5555                 break;
5556             }
5557             default: {
5558                 PyErr_SetString(PyExc_TypeError,
5559                                 "Unknown file_actions identifier");
5560                 goto fail;
5561             }
5562         }
5563         Py_DECREF(file_action);
5564     }
5565 
5566     Py_DECREF(seq);
5567     return 0;
5568 
5569 fail:
5570     Py_DECREF(seq);
5571     Py_DECREF(file_action);
5572     (void)posix_spawn_file_actions_destroy(file_actionsp);
5573     return -1;
5574 }
5575 
5576 
5577 static PyObject *
py_posix_spawn(int use_posix_spawnp,PyObject * module,path_t * path,PyObject * argv,PyObject * env,PyObject * file_actions,PyObject * setpgroup,int resetids,int setsid,PyObject * setsigmask,PyObject * setsigdef,PyObject * scheduler)5578 py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5579                PyObject *env, PyObject *file_actions,
5580                PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
5581                PyObject *setsigdef, PyObject *scheduler)
5582 {
5583     const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
5584     EXECV_CHAR **argvlist = NULL;
5585     EXECV_CHAR **envlist = NULL;
5586     posix_spawn_file_actions_t file_actions_buf;
5587     posix_spawn_file_actions_t *file_actionsp = NULL;
5588     posix_spawnattr_t attr;
5589     posix_spawnattr_t *attrp = NULL;
5590     Py_ssize_t argc, envc;
5591     PyObject *result = NULL;
5592     PyObject *temp_buffer = NULL;
5593     pid_t pid;
5594     int err_code;
5595 
5596     /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
5597        argv is a list or tuple of strings and env is a dictionary
5598        like posix.environ. */
5599 
5600     if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5601         PyErr_Format(PyExc_TypeError,
5602                      "%s: argv must be a tuple or list", func_name);
5603         goto exit;
5604     }
5605     argc = PySequence_Size(argv);
5606     if (argc < 1) {
5607         PyErr_Format(PyExc_ValueError,
5608                      "%s: argv must not be empty", func_name);
5609         return NULL;
5610     }
5611 
5612     if (!PyMapping_Check(env)) {
5613         PyErr_Format(PyExc_TypeError,
5614                      "%s: environment must be a mapping object", func_name);
5615         goto exit;
5616     }
5617 
5618     argvlist = parse_arglist(argv, &argc);
5619     if (argvlist == NULL) {
5620         goto exit;
5621     }
5622     if (!argvlist[0][0]) {
5623         PyErr_Format(PyExc_ValueError,
5624                      "%s: argv first element cannot be empty", func_name);
5625         goto exit;
5626     }
5627 
5628     envlist = parse_envlist(env, &envc);
5629     if (envlist == NULL) {
5630         goto exit;
5631     }
5632 
5633     if (file_actions != NULL && file_actions != Py_None) {
5634         /* There is a bug in old versions of glibc that makes some of the
5635          * helper functions for manipulating file actions not copy the provided
5636          * buffers. The problem is that posix_spawn_file_actions_addopen does not
5637          * copy the value of path for some old versions of glibc (<2.20).
5638          * The use of temp_buffer here is a workaround that keeps the
5639          * python objects that own the buffers alive until posix_spawn gets called.
5640          * Check https://bugs.python.org/issue33630 and
5641          * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5642         temp_buffer = PyList_New(0);
5643         if (!temp_buffer) {
5644             goto exit;
5645         }
5646         if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
5647             goto exit;
5648         }
5649         file_actionsp = &file_actions_buf;
5650     }
5651 
5652     if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5653                                 setsigmask, setsigdef, scheduler, &attr)) {
5654         goto exit;
5655     }
5656     attrp = &attr;
5657 
5658     if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
5659         goto exit;
5660     }
5661 
5662     _Py_BEGIN_SUPPRESS_IPH
5663 #ifdef HAVE_POSIX_SPAWNP
5664     if (use_posix_spawnp) {
5665         err_code = posix_spawnp(&pid, path->narrow,
5666                                 file_actionsp, attrp, argvlist, envlist);
5667     }
5668     else
5669 #endif /* HAVE_POSIX_SPAWNP */
5670     {
5671         err_code = posix_spawn(&pid, path->narrow,
5672                                file_actionsp, attrp, argvlist, envlist);
5673     }
5674     _Py_END_SUPPRESS_IPH
5675 
5676     if (err_code) {
5677         errno = err_code;
5678         PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
5679         goto exit;
5680     }
5681 #ifdef _Py_MEMORY_SANITIZER
5682     __msan_unpoison(&pid, sizeof(pid));
5683 #endif
5684     result = PyLong_FromPid(pid);
5685 
5686 exit:
5687     if (file_actionsp) {
5688         (void)posix_spawn_file_actions_destroy(file_actionsp);
5689     }
5690     if (attrp) {
5691         (void)posix_spawnattr_destroy(attrp);
5692     }
5693     if (envlist) {
5694         free_string_array(envlist, envc);
5695     }
5696     if (argvlist) {
5697         free_string_array(argvlist, argc);
5698     }
5699     Py_XDECREF(temp_buffer);
5700     return result;
5701 }
5702 
5703 
5704 /*[clinic input]
5705 
5706 os.posix_spawn
5707     path: path_t
5708         Path of executable file.
5709     argv: object
5710         Tuple or list of strings.
5711     env: object
5712         Dictionary of strings mapping to strings.
5713     /
5714     *
5715     file_actions: object(c_default='NULL') = ()
5716         A sequence of file action tuples.
5717     setpgroup: object = NULL
5718         The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5719     resetids: bool(accept={int}) = False
5720         If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5721     setsid: bool(accept={int}) = False
5722         If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
5723     setsigmask: object(c_default='NULL') = ()
5724         The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5725     setsigdef: object(c_default='NULL') = ()
5726         The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5727     scheduler: object = NULL
5728         A tuple with the scheduler policy (optional) and parameters.
5729 
5730 Execute the program specified by path in a new process.
5731 [clinic start generated code]*/
5732 
5733 static PyObject *
os_posix_spawn_impl(PyObject * module,path_t * path,PyObject * argv,PyObject * env,PyObject * file_actions,PyObject * setpgroup,int resetids,int setsid,PyObject * setsigmask,PyObject * setsigdef,PyObject * scheduler)5734 os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5735                     PyObject *env, PyObject *file_actions,
5736                     PyObject *setpgroup, int resetids, int setsid,
5737                     PyObject *setsigmask, PyObject *setsigdef,
5738                     PyObject *scheduler)
5739 /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
5740 {
5741     return py_posix_spawn(0, module, path, argv, env, file_actions,
5742                           setpgroup, resetids, setsid, setsigmask, setsigdef,
5743                           scheduler);
5744 }
5745  #endif /* HAVE_POSIX_SPAWN */
5746 
5747 
5748 
5749 #ifdef HAVE_POSIX_SPAWNP
5750 /*[clinic input]
5751 
5752 os.posix_spawnp
5753     path: path_t
5754         Path of executable file.
5755     argv: object
5756         Tuple or list of strings.
5757     env: object
5758         Dictionary of strings mapping to strings.
5759     /
5760     *
5761     file_actions: object(c_default='NULL') = ()
5762         A sequence of file action tuples.
5763     setpgroup: object = NULL
5764         The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5765     resetids: bool(accept={int}) = False
5766         If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
5767     setsid: bool(accept={int}) = False
5768         If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
5769     setsigmask: object(c_default='NULL') = ()
5770         The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5771     setsigdef: object(c_default='NULL') = ()
5772         The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5773     scheduler: object = NULL
5774         A tuple with the scheduler policy (optional) and parameters.
5775 
5776 Execute the program specified by path in a new process.
5777 [clinic start generated code]*/
5778 
5779 static PyObject *
os_posix_spawnp_impl(PyObject * module,path_t * path,PyObject * argv,PyObject * env,PyObject * file_actions,PyObject * setpgroup,int resetids,int setsid,PyObject * setsigmask,PyObject * setsigdef,PyObject * scheduler)5780 os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5781                      PyObject *env, PyObject *file_actions,
5782                      PyObject *setpgroup, int resetids, int setsid,
5783                      PyObject *setsigmask, PyObject *setsigdef,
5784                      PyObject *scheduler)
5785 /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
5786 {
5787     return py_posix_spawn(1, module, path, argv, env, file_actions,
5788                           setpgroup, resetids, setsid, setsigmask, setsigdef,
5789                           scheduler);
5790 }
5791 #endif /* HAVE_POSIX_SPAWNP */
5792 
5793 #ifdef HAVE_RTPSPAWN
5794 static intptr_t
_rtp_spawn(int mode,const char * rtpFileName,const char * argv[],const char * envp[])5795 _rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5796                const char  *envp[])
5797 {
5798      RTP_ID rtpid;
5799      int status;
5800      pid_t res;
5801      int async_err = 0;
5802 
5803      /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5804         uStackSize=0 cannot be used, the default stack size is too small for
5805         Python. */
5806      if (envp) {
5807          rtpid = rtpSpawn(rtpFileName, argv, envp,
5808                           100, 0x1000000, 0, VX_FP_TASK);
5809      }
5810      else {
5811          rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5812                           100, 0x1000000, 0, VX_FP_TASK);
5813      }
5814      if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5815          do {
5816              res = waitpid((pid_t)rtpid, &status, 0);
5817          } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5818 
5819          if (res < 0)
5820              return RTP_ID_ERROR;
5821          return ((intptr_t)status);
5822      }
5823      return ((intptr_t)rtpid);
5824 }
5825 #endif
5826 
5827 #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
5828 /*[clinic input]
5829 os.spawnv
5830 
5831     mode: int
5832         Mode of process creation.
5833     path: path_t
5834         Path of executable file.
5835     argv: object
5836         Tuple or list of strings.
5837     /
5838 
5839 Execute the program specified by path in a new process.
5840 [clinic start generated code]*/
5841 
5842 static PyObject *
os_spawnv_impl(PyObject * module,int mode,path_t * path,PyObject * argv)5843 os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5844 /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
5845 {
5846     EXECV_CHAR **argvlist;
5847     int i;
5848     Py_ssize_t argc;
5849     intptr_t spawnval;
5850     PyObject *(*getitem)(PyObject *, Py_ssize_t);
5851 
5852     /* spawnv has three arguments: (mode, path, argv), where
5853        argv is a list or tuple of strings. */
5854 
5855     if (PyList_Check(argv)) {
5856         argc = PyList_Size(argv);
5857         getitem = PyList_GetItem;
5858     }
5859     else if (PyTuple_Check(argv)) {
5860         argc = PyTuple_Size(argv);
5861         getitem = PyTuple_GetItem;
5862     }
5863     else {
5864         PyErr_SetString(PyExc_TypeError,
5865                         "spawnv() arg 2 must be a tuple or list");
5866         return NULL;
5867     }
5868     if (argc == 0) {
5869         PyErr_SetString(PyExc_ValueError,
5870             "spawnv() arg 2 cannot be empty");
5871         return NULL;
5872     }
5873 
5874     argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
5875     if (argvlist == NULL) {
5876         return PyErr_NoMemory();
5877     }
5878     for (i = 0; i < argc; i++) {
5879         if (!fsconvert_strdup((*getitem)(argv, i),
5880                               &argvlist[i])) {
5881             free_string_array(argvlist, i);
5882             PyErr_SetString(
5883                 PyExc_TypeError,
5884                 "spawnv() arg 2 must contain only strings");
5885             return NULL;
5886         }
5887         if (i == 0 && !argvlist[0][0]) {
5888             free_string_array(argvlist, i + 1);
5889             PyErr_SetString(
5890                 PyExc_ValueError,
5891                 "spawnv() arg 2 first element cannot be empty");
5892             return NULL;
5893         }
5894     }
5895     argvlist[argc] = NULL;
5896 
5897 #if !defined(HAVE_RTPSPAWN)
5898     if (mode == _OLD_P_OVERLAY)
5899         mode = _P_OVERLAY;
5900 #endif
5901 
5902     if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
5903                     Py_None) < 0) {
5904         free_string_array(argvlist, argc);
5905         return NULL;
5906     }
5907 
5908     Py_BEGIN_ALLOW_THREADS
5909     _Py_BEGIN_SUPPRESS_IPH
5910 #ifdef HAVE_WSPAWNV
5911     spawnval = _wspawnv(mode, path->wide, argvlist);
5912 #elif defined(HAVE_RTPSPAWN)
5913     spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
5914 #else
5915     spawnval = _spawnv(mode, path->narrow, argvlist);
5916 #endif
5917     _Py_END_SUPPRESS_IPH
5918     Py_END_ALLOW_THREADS
5919 
5920     free_string_array(argvlist, argc);
5921 
5922     if (spawnval == -1)
5923         return posix_error();
5924     else
5925         return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
5926 }
5927 
5928 /*[clinic input]
5929 os.spawnve
5930 
5931     mode: int
5932         Mode of process creation.
5933     path: path_t
5934         Path of executable file.
5935     argv: object
5936         Tuple or list of strings.
5937     env: object
5938         Dictionary of strings mapping to strings.
5939     /
5940 
5941 Execute the program specified by path in a new process.
5942 [clinic start generated code]*/
5943 
5944 static PyObject *
os_spawnve_impl(PyObject * module,int mode,path_t * path,PyObject * argv,PyObject * env)5945 os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
5946                 PyObject *env)
5947 /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
5948 {
5949     EXECV_CHAR **argvlist;
5950     EXECV_CHAR **envlist;
5951     PyObject *res = NULL;
5952     Py_ssize_t argc, i, envc;
5953     intptr_t spawnval;
5954     PyObject *(*getitem)(PyObject *, Py_ssize_t);
5955     Py_ssize_t lastarg = 0;
5956 
5957     /* spawnve has four arguments: (mode, path, argv, env), where
5958        argv is a list or tuple of strings and env is a dictionary
5959        like posix.environ. */
5960 
5961     if (PyList_Check(argv)) {
5962         argc = PyList_Size(argv);
5963         getitem = PyList_GetItem;
5964     }
5965     else if (PyTuple_Check(argv)) {
5966         argc = PyTuple_Size(argv);
5967         getitem = PyTuple_GetItem;
5968     }
5969     else {
5970         PyErr_SetString(PyExc_TypeError,
5971                         "spawnve() arg 2 must be a tuple or list");
5972         goto fail_0;
5973     }
5974     if (argc == 0) {
5975         PyErr_SetString(PyExc_ValueError,
5976             "spawnve() arg 2 cannot be empty");
5977         goto fail_0;
5978     }
5979     if (!PyMapping_Check(env)) {
5980         PyErr_SetString(PyExc_TypeError,
5981                         "spawnve() arg 3 must be a mapping object");
5982         goto fail_0;
5983     }
5984 
5985     argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
5986     if (argvlist == NULL) {
5987         PyErr_NoMemory();
5988         goto fail_0;
5989     }
5990     for (i = 0; i < argc; i++) {
5991         if (!fsconvert_strdup((*getitem)(argv, i),
5992                               &argvlist[i]))
5993         {
5994             lastarg = i;
5995             goto fail_1;
5996         }
5997         if (i == 0 && !argvlist[0][0]) {
5998             lastarg = i + 1;
5999             PyErr_SetString(
6000                 PyExc_ValueError,
6001                 "spawnv() arg 2 first element cannot be empty");
6002             goto fail_1;
6003         }
6004     }
6005     lastarg = argc;
6006     argvlist[argc] = NULL;
6007 
6008     envlist = parse_envlist(env, &envc);
6009     if (envlist == NULL)
6010         goto fail_1;
6011 
6012 #if !defined(HAVE_RTPSPAWN)
6013     if (mode == _OLD_P_OVERLAY)
6014         mode = _P_OVERLAY;
6015 #endif
6016 
6017     if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
6018         goto fail_2;
6019     }
6020 
6021     Py_BEGIN_ALLOW_THREADS
6022     _Py_BEGIN_SUPPRESS_IPH
6023 #ifdef HAVE_WSPAWNV
6024     spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
6025 #elif defined(HAVE_RTPSPAWN)
6026     spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6027                            (const char **)envlist);
6028 #else
6029     spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6030 #endif
6031     _Py_END_SUPPRESS_IPH
6032     Py_END_ALLOW_THREADS
6033 
6034     if (spawnval == -1)
6035         (void) posix_error();
6036     else
6037         res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
6038 
6039   fail_2:
6040     while (--envc >= 0)
6041         PyMem_DEL(envlist[envc]);
6042     PyMem_DEL(envlist);
6043   fail_1:
6044     free_string_array(argvlist, lastarg);
6045   fail_0:
6046     return res;
6047 }
6048 
6049 #endif /* HAVE_SPAWNV */
6050 
6051 
6052 #ifdef HAVE_FORK
6053 
6054 /* Helper function to validate arguments.
6055    Returns 0 on success.  non-zero on failure with a TypeError raised.
6056    If obj is non-NULL it must be callable.  */
6057 static int
check_null_or_callable(PyObject * obj,const char * obj_name)6058 check_null_or_callable(PyObject *obj, const char* obj_name)
6059 {
6060     if (obj && !PyCallable_Check(obj)) {
6061         PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
6062                      obj_name, Py_TYPE(obj)->tp_name);
6063         return -1;
6064     }
6065     return 0;
6066 }
6067 
6068 /*[clinic input]
6069 os.register_at_fork
6070 
6071     *
6072     before: object=NULL
6073         A callable to be called in the parent before the fork() syscall.
6074     after_in_child: object=NULL
6075         A callable to be called in the child after fork().
6076     after_in_parent: object=NULL
6077         A callable to be called in the parent after fork().
6078 
6079 Register callables to be called when forking a new process.
6080 
6081 'before' callbacks are called in reverse order.
6082 'after_in_child' and 'after_in_parent' callbacks are called in order.
6083 
6084 [clinic start generated code]*/
6085 
6086 static PyObject *
os_register_at_fork_impl(PyObject * module,PyObject * before,PyObject * after_in_child,PyObject * after_in_parent)6087 os_register_at_fork_impl(PyObject *module, PyObject *before,
6088                          PyObject *after_in_child, PyObject *after_in_parent)
6089 /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
6090 {
6091     PyInterpreterState *interp;
6092 
6093     if (!before && !after_in_child && !after_in_parent) {
6094         PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6095         return NULL;
6096     }
6097     if (check_null_or_callable(before, "before") ||
6098         check_null_or_callable(after_in_child, "after_in_child") ||
6099         check_null_or_callable(after_in_parent, "after_in_parent")) {
6100         return NULL;
6101     }
6102     interp = _PyInterpreterState_Get();
6103 
6104     if (register_at_forker(&interp->before_forkers, before)) {
6105         return NULL;
6106     }
6107     if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
6108         return NULL;
6109     }
6110     if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6111         return NULL;
6112     }
6113     Py_RETURN_NONE;
6114 }
6115 #endif /* HAVE_FORK */
6116 
6117 
6118 #ifdef HAVE_FORK1
6119 /*[clinic input]
6120 os.fork1
6121 
6122 Fork a child process with a single multiplexed (i.e., not bound) thread.
6123 
6124 Return 0 to child process and PID of child to parent process.
6125 [clinic start generated code]*/
6126 
6127 static PyObject *
os_fork1_impl(PyObject * module)6128 os_fork1_impl(PyObject *module)
6129 /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
6130 {
6131     pid_t pid;
6132 
6133     if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6134         PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6135         return NULL;
6136     }
6137     PyOS_BeforeFork();
6138     pid = fork1();
6139     if (pid == 0) {
6140         /* child: this clobbers and resets the import lock. */
6141         PyOS_AfterFork_Child();
6142     } else {
6143         /* parent: release the import lock. */
6144         PyOS_AfterFork_Parent();
6145     }
6146     if (pid == -1)
6147         return posix_error();
6148     return PyLong_FromPid(pid);
6149 }
6150 #endif /* HAVE_FORK1 */
6151 
6152 
6153 #ifdef HAVE_FORK
6154 /*[clinic input]
6155 os.fork
6156 
6157 Fork a child process.
6158 
6159 Return 0 to child process and PID of child to parent process.
6160 [clinic start generated code]*/
6161 
6162 static PyObject *
os_fork_impl(PyObject * module)6163 os_fork_impl(PyObject *module)
6164 /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
6165 {
6166     pid_t pid;
6167 
6168     if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6169         PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6170         return NULL;
6171     }
6172     if (PySys_Audit("os.fork", NULL) < 0) {
6173         return NULL;
6174     }
6175     PyOS_BeforeFork();
6176     pid = fork();
6177     if (pid == 0) {
6178         /* child: this clobbers and resets the import lock. */
6179         PyOS_AfterFork_Child();
6180     } else {
6181         /* parent: release the import lock. */
6182         PyOS_AfterFork_Parent();
6183     }
6184     if (pid == -1)
6185         return posix_error();
6186     return PyLong_FromPid(pid);
6187 }
6188 #endif /* HAVE_FORK */
6189 
6190 
6191 #ifdef HAVE_SCHED_H
6192 #ifdef HAVE_SCHED_GET_PRIORITY_MAX
6193 /*[clinic input]
6194 os.sched_get_priority_max
6195 
6196     policy: int
6197 
6198 Get the maximum scheduling priority for policy.
6199 [clinic start generated code]*/
6200 
6201 static PyObject *
os_sched_get_priority_max_impl(PyObject * module,int policy)6202 os_sched_get_priority_max_impl(PyObject *module, int policy)
6203 /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
6204 {
6205     int max;
6206 
6207     max = sched_get_priority_max(policy);
6208     if (max < 0)
6209         return posix_error();
6210     return PyLong_FromLong(max);
6211 }
6212 
6213 
6214 /*[clinic input]
6215 os.sched_get_priority_min
6216 
6217     policy: int
6218 
6219 Get the minimum scheduling priority for policy.
6220 [clinic start generated code]*/
6221 
6222 static PyObject *
os_sched_get_priority_min_impl(PyObject * module,int policy)6223 os_sched_get_priority_min_impl(PyObject *module, int policy)
6224 /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
6225 {
6226     int min = sched_get_priority_min(policy);
6227     if (min < 0)
6228         return posix_error();
6229     return PyLong_FromLong(min);
6230 }
6231 #endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6232 
6233 
6234 #ifdef HAVE_SCHED_SETSCHEDULER
6235 /*[clinic input]
6236 os.sched_getscheduler
6237     pid: pid_t
6238     /
6239 
6240 Get the scheduling policy for the process identifiedy by pid.
6241 
6242 Passing 0 for pid returns the scheduling policy for the calling process.
6243 [clinic start generated code]*/
6244 
6245 static PyObject *
os_sched_getscheduler_impl(PyObject * module,pid_t pid)6246 os_sched_getscheduler_impl(PyObject *module, pid_t pid)
6247 /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
6248 {
6249     int policy;
6250 
6251     policy = sched_getscheduler(pid);
6252     if (policy < 0)
6253         return posix_error();
6254     return PyLong_FromLong(policy);
6255 }
6256 #endif /* HAVE_SCHED_SETSCHEDULER */
6257 
6258 
6259 #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
6260 /*[clinic input]
6261 class os.sched_param "PyObject *" "SchedParamType"
6262 
6263 @classmethod
6264 os.sched_param.__new__
6265 
6266     sched_priority: object
6267         A scheduling parameter.
6268 
6269 Current has only one field: sched_priority");
6270 [clinic start generated code]*/
6271 
6272 static PyObject *
os_sched_param_impl(PyTypeObject * type,PyObject * sched_priority)6273 os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
6274 /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
6275 {
6276     PyObject *res;
6277 
6278     res = PyStructSequence_New(type);
6279     if (!res)
6280         return NULL;
6281     Py_INCREF(sched_priority);
6282     PyStructSequence_SET_ITEM(res, 0, sched_priority);
6283     return res;
6284 }
6285 
6286 
6287 PyDoc_VAR(os_sched_param__doc__);
6288 
6289 static PyStructSequence_Field sched_param_fields[] = {
6290     {"sched_priority", "the scheduling priority"},
6291     {0}
6292 };
6293 
6294 static PyStructSequence_Desc sched_param_desc = {
6295     "sched_param", /* name */
6296     os_sched_param__doc__, /* doc */
6297     sched_param_fields,
6298     1
6299 };
6300 
6301 static int
convert_sched_param(PyObject * param,struct sched_param * res)6302 convert_sched_param(PyObject *param, struct sched_param *res)
6303 {
6304     long priority;
6305 
6306     if (Py_TYPE(param) != SchedParamType) {
6307         PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6308         return 0;
6309     }
6310     priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6311     if (priority == -1 && PyErr_Occurred())
6312         return 0;
6313     if (priority > INT_MAX || priority < INT_MIN) {
6314         PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6315         return 0;
6316     }
6317     res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6318     return 1;
6319 }
6320 #endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
6321 
6322 
6323 #ifdef HAVE_SCHED_SETSCHEDULER
6324 /*[clinic input]
6325 os.sched_setscheduler
6326 
6327     pid: pid_t
6328     policy: int
6329     param: sched_param
6330     /
6331 
6332 Set the scheduling policy for the process identified by pid.
6333 
6334 If pid is 0, the calling process is changed.
6335 param is an instance of sched_param.
6336 [clinic start generated code]*/
6337 
6338 static PyObject *
os_sched_setscheduler_impl(PyObject * module,pid_t pid,int policy,struct sched_param * param)6339 os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
6340                            struct sched_param *param)
6341 /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
6342 {
6343     /*
6344     ** sched_setscheduler() returns 0 in Linux, but the previous
6345     ** scheduling policy under Solaris/Illumos, and others.
6346     ** On error, -1 is returned in all Operating Systems.
6347     */
6348     if (sched_setscheduler(pid, policy, param) == -1)
6349         return posix_error();
6350     Py_RETURN_NONE;
6351 }
6352 #endif  /* HAVE_SCHED_SETSCHEDULER*/
6353 
6354 
6355 #ifdef HAVE_SCHED_SETPARAM
6356 /*[clinic input]
6357 os.sched_getparam
6358     pid: pid_t
6359     /
6360 
6361 Returns scheduling parameters for the process identified by pid.
6362 
6363 If pid is 0, returns parameters for the calling process.
6364 Return value is an instance of sched_param.
6365 [clinic start generated code]*/
6366 
6367 static PyObject *
os_sched_getparam_impl(PyObject * module,pid_t pid)6368 os_sched_getparam_impl(PyObject *module, pid_t pid)
6369 /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
6370 {
6371     struct sched_param param;
6372     PyObject *result;
6373     PyObject *priority;
6374 
6375     if (sched_getparam(pid, &param))
6376         return posix_error();
6377     result = PyStructSequence_New(SchedParamType);
6378     if (!result)
6379         return NULL;
6380     priority = PyLong_FromLong(param.sched_priority);
6381     if (!priority) {
6382         Py_DECREF(result);
6383         return NULL;
6384     }
6385     PyStructSequence_SET_ITEM(result, 0, priority);
6386     return result;
6387 }
6388 
6389 
6390 /*[clinic input]
6391 os.sched_setparam
6392     pid: pid_t
6393     param: sched_param
6394     /
6395 
6396 Set scheduling parameters for the process identified by pid.
6397 
6398 If pid is 0, sets parameters for the calling process.
6399 param should be an instance of sched_param.
6400 [clinic start generated code]*/
6401 
6402 static PyObject *
os_sched_setparam_impl(PyObject * module,pid_t pid,struct sched_param * param)6403 os_sched_setparam_impl(PyObject *module, pid_t pid,
6404                        struct sched_param *param)
6405 /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
6406 {
6407     if (sched_setparam(pid, param))
6408         return posix_error();
6409     Py_RETURN_NONE;
6410 }
6411 #endif /* HAVE_SCHED_SETPARAM */
6412 
6413 
6414 #ifdef HAVE_SCHED_RR_GET_INTERVAL
6415 /*[clinic input]
6416 os.sched_rr_get_interval -> double
6417     pid: pid_t
6418     /
6419 
6420 Return the round-robin quantum for the process identified by pid, in seconds.
6421 
6422 Value returned is a float.
6423 [clinic start generated code]*/
6424 
6425 static double
os_sched_rr_get_interval_impl(PyObject * module,pid_t pid)6426 os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6427 /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
6428 {
6429     struct timespec interval;
6430     if (sched_rr_get_interval(pid, &interval)) {
6431         posix_error();
6432         return -1.0;
6433     }
6434 #ifdef _Py_MEMORY_SANITIZER
6435     __msan_unpoison(&interval, sizeof(interval));
6436 #endif
6437     return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6438 }
6439 #endif /* HAVE_SCHED_RR_GET_INTERVAL */
6440 
6441 
6442 /*[clinic input]
6443 os.sched_yield
6444 
6445 Voluntarily relinquish the CPU.
6446 [clinic start generated code]*/
6447 
6448 static PyObject *
os_sched_yield_impl(PyObject * module)6449 os_sched_yield_impl(PyObject *module)
6450 /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
6451 {
6452     if (sched_yield())
6453         return posix_error();
6454     Py_RETURN_NONE;
6455 }
6456 
6457 #ifdef HAVE_SCHED_SETAFFINITY
6458 /* The minimum number of CPUs allocated in a cpu_set_t */
6459 static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
6460 
6461 /*[clinic input]
6462 os.sched_setaffinity
6463     pid: pid_t
6464     mask : object
6465     /
6466 
6467 Set the CPU affinity of the process identified by pid to mask.
6468 
6469 mask should be an iterable of integers identifying CPUs.
6470 [clinic start generated code]*/
6471 
6472 static PyObject *
os_sched_setaffinity_impl(PyObject * module,pid_t pid,PyObject * mask)6473 os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6474 /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
6475 {
6476     int ncpus;
6477     size_t setsize;
6478     cpu_set_t *cpu_set = NULL;
6479     PyObject *iterator = NULL, *item;
6480 
6481     iterator = PyObject_GetIter(mask);
6482     if (iterator == NULL)
6483         return NULL;
6484 
6485     ncpus = NCPUS_START;
6486     setsize = CPU_ALLOC_SIZE(ncpus);
6487     cpu_set = CPU_ALLOC(ncpus);
6488     if (cpu_set == NULL) {
6489         PyErr_NoMemory();
6490         goto error;
6491     }
6492     CPU_ZERO_S(setsize, cpu_set);
6493 
6494     while ((item = PyIter_Next(iterator))) {
6495         long cpu;
6496         if (!PyLong_Check(item)) {
6497             PyErr_Format(PyExc_TypeError,
6498                         "expected an iterator of ints, "
6499                         "but iterator yielded %R",
6500                         Py_TYPE(item));
6501             Py_DECREF(item);
6502             goto error;
6503         }
6504         cpu = PyLong_AsLong(item);
6505         Py_DECREF(item);
6506         if (cpu < 0) {
6507             if (!PyErr_Occurred())
6508                 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6509             goto error;
6510         }
6511         if (cpu > INT_MAX - 1) {
6512             PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6513             goto error;
6514         }
6515         if (cpu >= ncpus) {
6516             /* Grow CPU mask to fit the CPU number */
6517             int newncpus = ncpus;
6518             cpu_set_t *newmask;
6519             size_t newsetsize;
6520             while (newncpus <= cpu) {
6521                 if (newncpus > INT_MAX / 2)
6522                     newncpus = cpu + 1;
6523                 else
6524                     newncpus = newncpus * 2;
6525             }
6526             newmask = CPU_ALLOC(newncpus);
6527             if (newmask == NULL) {
6528                 PyErr_NoMemory();
6529                 goto error;
6530             }
6531             newsetsize = CPU_ALLOC_SIZE(newncpus);
6532             CPU_ZERO_S(newsetsize, newmask);
6533             memcpy(newmask, cpu_set, setsize);
6534             CPU_FREE(cpu_set);
6535             setsize = newsetsize;
6536             cpu_set = newmask;
6537             ncpus = newncpus;
6538         }
6539         CPU_SET_S(cpu, setsize, cpu_set);
6540     }
6541     if (PyErr_Occurred()) {
6542         goto error;
6543     }
6544     Py_CLEAR(iterator);
6545 
6546     if (sched_setaffinity(pid, setsize, cpu_set)) {
6547         posix_error();
6548         goto error;
6549     }
6550     CPU_FREE(cpu_set);
6551     Py_RETURN_NONE;
6552 
6553 error:
6554     if (cpu_set)
6555         CPU_FREE(cpu_set);
6556     Py_XDECREF(iterator);
6557     return NULL;
6558 }
6559 
6560 
6561 /*[clinic input]
6562 os.sched_getaffinity
6563     pid: pid_t
6564     /
6565 
6566 Return the affinity of the process identified by pid (or the current process if zero).
6567 
6568 The affinity is returned as a set of CPU identifiers.
6569 [clinic start generated code]*/
6570 
6571 static PyObject *
os_sched_getaffinity_impl(PyObject * module,pid_t pid)6572 os_sched_getaffinity_impl(PyObject *module, pid_t pid)
6573 /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
6574 {
6575     int cpu, ncpus, count;
6576     size_t setsize;
6577     cpu_set_t *mask = NULL;
6578     PyObject *res = NULL;
6579 
6580     ncpus = NCPUS_START;
6581     while (1) {
6582         setsize = CPU_ALLOC_SIZE(ncpus);
6583         mask = CPU_ALLOC(ncpus);
6584         if (mask == NULL)
6585             return PyErr_NoMemory();
6586         if (sched_getaffinity(pid, setsize, mask) == 0)
6587             break;
6588         CPU_FREE(mask);
6589         if (errno != EINVAL)
6590             return posix_error();
6591         if (ncpus > INT_MAX / 2) {
6592             PyErr_SetString(PyExc_OverflowError, "could not allocate "
6593                             "a large enough CPU set");
6594             return NULL;
6595         }
6596         ncpus = ncpus * 2;
6597     }
6598 
6599     res = PySet_New(NULL);
6600     if (res == NULL)
6601         goto error;
6602     for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6603         if (CPU_ISSET_S(cpu, setsize, mask)) {
6604             PyObject *cpu_num = PyLong_FromLong(cpu);
6605             --count;
6606             if (cpu_num == NULL)
6607                 goto error;
6608             if (PySet_Add(res, cpu_num)) {
6609                 Py_DECREF(cpu_num);
6610                 goto error;
6611             }
6612             Py_DECREF(cpu_num);
6613         }
6614     }
6615     CPU_FREE(mask);
6616     return res;
6617 
6618 error:
6619     if (mask)
6620         CPU_FREE(mask);
6621     Py_XDECREF(res);
6622     return NULL;
6623 }
6624 
6625 #endif /* HAVE_SCHED_SETAFFINITY */
6626 
6627 #endif /* HAVE_SCHED_H */
6628 
6629 
6630 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
6631 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6632 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
6633 #define DEV_PTY_FILE "/dev/ptc"
6634 #define HAVE_DEV_PTMX
6635 #else
6636 #define DEV_PTY_FILE "/dev/ptmx"
6637 #endif
6638 
6639 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
6640 #ifdef HAVE_PTY_H
6641 #include <pty.h>
6642 #else
6643 #ifdef HAVE_LIBUTIL_H
6644 #include <libutil.h>
6645 #else
6646 #ifdef HAVE_UTIL_H
6647 #include <util.h>
6648 #endif /* HAVE_UTIL_H */
6649 #endif /* HAVE_LIBUTIL_H */
6650 #endif /* HAVE_PTY_H */
6651 #ifdef HAVE_STROPTS_H
6652 #include <stropts.h>
6653 #endif
6654 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
6655 
6656 
6657 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
6658 /*[clinic input]
6659 os.openpty
6660 
6661 Open a pseudo-terminal.
6662 
6663 Return a tuple of (master_fd, slave_fd) containing open file descriptors
6664 for both the master and slave ends.
6665 [clinic start generated code]*/
6666 
6667 static PyObject *
os_openpty_impl(PyObject * module)6668 os_openpty_impl(PyObject *module)
6669 /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
6670 {
6671     int master_fd = -1, slave_fd = -1;
6672 #ifndef HAVE_OPENPTY
6673     char * slave_name;
6674 #endif
6675 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
6676     PyOS_sighandler_t sig_saved;
6677 #if defined(__sun) && defined(__SVR4)
6678     extern char *ptsname(int fildes);
6679 #endif
6680 #endif
6681 
6682 #ifdef HAVE_OPENPTY
6683     if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
6684         goto posix_error;
6685 
6686     if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6687         goto error;
6688     if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6689         goto error;
6690 
6691 #elif defined(HAVE__GETPTY)
6692     slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6693     if (slave_name == NULL)
6694         goto posix_error;
6695     if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6696         goto error;
6697 
6698     slave_fd = _Py_open(slave_name, O_RDWR);
6699     if (slave_fd < 0)
6700         goto error;
6701 
6702 #else
6703     master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
6704     if (master_fd < 0)
6705         goto posix_error;
6706 
6707     sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
6708 
6709     /* change permission of slave */
6710     if (grantpt(master_fd) < 0) {
6711         PyOS_setsig(SIGCHLD, sig_saved);
6712         goto posix_error;
6713     }
6714 
6715     /* unlock slave */
6716     if (unlockpt(master_fd) < 0) {
6717         PyOS_setsig(SIGCHLD, sig_saved);
6718         goto posix_error;
6719     }
6720 
6721     PyOS_setsig(SIGCHLD, sig_saved);
6722 
6723     slave_name = ptsname(master_fd); /* get name of slave */
6724     if (slave_name == NULL)
6725         goto posix_error;
6726 
6727     slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
6728     if (slave_fd == -1)
6729         goto error;
6730 
6731     if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6732         goto posix_error;
6733 
6734 #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
6735     ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6736     ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
6737 #ifndef __hpux
6738     ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
6739 #endif /* __hpux */
6740 #endif /* HAVE_CYGWIN */
6741 #endif /* HAVE_OPENPTY */
6742 
6743     return Py_BuildValue("(ii)", master_fd, slave_fd);
6744 
6745 posix_error:
6746     posix_error();
6747 error:
6748     if (master_fd != -1)
6749         close(master_fd);
6750     if (slave_fd != -1)
6751         close(slave_fd);
6752     return NULL;
6753 }
6754 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
6755 
6756 
6757 #ifdef HAVE_FORKPTY
6758 /*[clinic input]
6759 os.forkpty
6760 
6761 Fork a new process with a new pseudo-terminal as controlling tty.
6762 
6763 Returns a tuple of (pid, master_fd).
6764 Like fork(), return pid of 0 to the child process,
6765 and pid of child to the parent process.
6766 To both, return fd of newly opened pseudo-terminal.
6767 [clinic start generated code]*/
6768 
6769 static PyObject *
os_forkpty_impl(PyObject * module)6770 os_forkpty_impl(PyObject *module)
6771 /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
6772 {
6773     int master_fd = -1;
6774     pid_t pid;
6775 
6776     if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6777         PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6778         return NULL;
6779     }
6780     if (PySys_Audit("os.forkpty", NULL) < 0) {
6781         return NULL;
6782     }
6783     PyOS_BeforeFork();
6784     pid = forkpty(&master_fd, NULL, NULL, NULL);
6785     if (pid == 0) {
6786         /* child: this clobbers and resets the import lock. */
6787         PyOS_AfterFork_Child();
6788     } else {
6789         /* parent: release the import lock. */
6790         PyOS_AfterFork_Parent();
6791     }
6792     if (pid == -1)
6793         return posix_error();
6794     return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
6795 }
6796 #endif /* HAVE_FORKPTY */
6797 
6798 
6799 #ifdef HAVE_GETEGID
6800 /*[clinic input]
6801 os.getegid
6802 
6803 Return the current process's effective group id.
6804 [clinic start generated code]*/
6805 
6806 static PyObject *
os_getegid_impl(PyObject * module)6807 os_getegid_impl(PyObject *module)
6808 /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
6809 {
6810     return _PyLong_FromGid(getegid());
6811 }
6812 #endif /* HAVE_GETEGID */
6813 
6814 
6815 #ifdef HAVE_GETEUID
6816 /*[clinic input]
6817 os.geteuid
6818 
6819 Return the current process's effective user id.
6820 [clinic start generated code]*/
6821 
6822 static PyObject *
os_geteuid_impl(PyObject * module)6823 os_geteuid_impl(PyObject *module)
6824 /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
6825 {
6826     return _PyLong_FromUid(geteuid());
6827 }
6828 #endif /* HAVE_GETEUID */
6829 
6830 
6831 #ifdef HAVE_GETGID
6832 /*[clinic input]
6833 os.getgid
6834 
6835 Return the current process's group id.
6836 [clinic start generated code]*/
6837 
6838 static PyObject *
os_getgid_impl(PyObject * module)6839 os_getgid_impl(PyObject *module)
6840 /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
6841 {
6842     return _PyLong_FromGid(getgid());
6843 }
6844 #endif /* HAVE_GETGID */
6845 
6846 
6847 #ifdef HAVE_GETPID
6848 /*[clinic input]
6849 os.getpid
6850 
6851 Return the current process id.
6852 [clinic start generated code]*/
6853 
6854 static PyObject *
os_getpid_impl(PyObject * module)6855 os_getpid_impl(PyObject *module)
6856 /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
6857 {
6858     return PyLong_FromPid(getpid());
6859 }
6860 #endif /* HAVE_GETPID */
6861 
6862 #ifdef NGROUPS_MAX
6863 #define MAX_GROUPS NGROUPS_MAX
6864 #else
6865     /* defined to be 16 on Solaris7, so this should be a small number */
6866 #define MAX_GROUPS 64
6867 #endif
6868 
6869 #ifdef HAVE_GETGROUPLIST
6870 
6871 /* AC 3.5: funny apple logic below */
6872 PyDoc_STRVAR(posix_getgrouplist__doc__,
6873 "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6874 Returns a list of groups to which a user belongs.\n\n\
6875     user: username to lookup\n\
6876     group: base group id of the user");
6877 
6878 static PyObject *
posix_getgrouplist(PyObject * self,PyObject * args)6879 posix_getgrouplist(PyObject *self, PyObject *args)
6880 {
6881     const char *user;
6882     int i, ngroups;
6883     PyObject *list;
6884 #ifdef __APPLE__
6885     int *groups, basegid;
6886 #else
6887     gid_t *groups, basegid;
6888 #endif
6889 
6890     /*
6891      * NGROUPS_MAX is defined by POSIX.1 as the maximum
6892      * number of supplimental groups a users can belong to.
6893      * We have to increment it by one because
6894      * getgrouplist() returns both the supplemental groups
6895      * and the primary group, i.e. all of the groups the
6896      * user belongs to.
6897      */
6898     ngroups = 1 + MAX_GROUPS;
6899 
6900 #ifdef __APPLE__
6901     if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
6902         return NULL;
6903 #else
6904     if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6905                           _Py_Gid_Converter, &basegid))
6906         return NULL;
6907 #endif
6908 
6909     while (1) {
6910 #ifdef __APPLE__
6911         groups = PyMem_New(int, ngroups);
6912 #else
6913         groups = PyMem_New(gid_t, ngroups);
6914 #endif
6915         if (groups == NULL) {
6916             return PyErr_NoMemory();
6917         }
6918 
6919         int old_ngroups = ngroups;
6920         if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
6921             /* Success */
6922             break;
6923         }
6924 
6925         /* getgrouplist() fails if the group list is too small */
6926         PyMem_Free(groups);
6927 
6928         if (ngroups > old_ngroups) {
6929             /* If the group list is too small, the glibc implementation of
6930                getgrouplist() sets ngroups to the total number of groups and
6931                returns -1. */
6932         }
6933         else {
6934             /* Double the group list size */
6935             if (ngroups > INT_MAX / 2) {
6936                 return PyErr_NoMemory();
6937             }
6938             ngroups *= 2;
6939         }
6940 
6941         /* Retry getgrouplist() with a larger group list */
6942     }
6943 
6944 #ifdef _Py_MEMORY_SANITIZER
6945     /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6946     __msan_unpoison(&ngroups, sizeof(ngroups));
6947     __msan_unpoison(groups, ngroups*sizeof(*groups));
6948 #endif
6949 
6950     list = PyList_New(ngroups);
6951     if (list == NULL) {
6952         PyMem_Del(groups);
6953         return NULL;
6954     }
6955 
6956     for (i = 0; i < ngroups; i++) {
6957 #ifdef __APPLE__
6958         PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
6959 #else
6960         PyObject *o = _PyLong_FromGid(groups[i]);
6961 #endif
6962         if (o == NULL) {
6963             Py_DECREF(list);
6964             PyMem_Del(groups);
6965             return NULL;
6966         }
6967         PyList_SET_ITEM(list, i, o);
6968     }
6969 
6970     PyMem_Del(groups);
6971 
6972     return list;
6973 }
6974 #endif /* HAVE_GETGROUPLIST */
6975 
6976 
6977 #ifdef HAVE_GETGROUPS
6978 /*[clinic input]
6979 os.getgroups
6980 
6981 Return list of supplemental group IDs for the process.
6982 [clinic start generated code]*/
6983 
6984 static PyObject *
os_getgroups_impl(PyObject * module)6985 os_getgroups_impl(PyObject *module)
6986 /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
6987 {
6988     PyObject *result = NULL;
6989     gid_t grouplist[MAX_GROUPS];
6990 
6991     /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
6992      * This is a helper variable to store the intermediate result when
6993      * that happens.
6994      *
6995      * To keep the code readable the OSX behaviour is unconditional,
6996      * according to the POSIX spec this should be safe on all unix-y
6997      * systems.
6998      */
6999     gid_t* alt_grouplist = grouplist;
7000     int n;
7001 
7002 #ifdef __APPLE__
7003     /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
7004      * there are more groups than can fit in grouplist.  Therefore, on OS X
7005      * always first call getgroups with length 0 to get the actual number
7006      * of groups.
7007      */
7008     n = getgroups(0, NULL);
7009     if (n < 0) {
7010         return posix_error();
7011     } else if (n <= MAX_GROUPS) {
7012         /* groups will fit in existing array */
7013         alt_grouplist = grouplist;
7014     } else {
7015         alt_grouplist = PyMem_New(gid_t, n);
7016         if (alt_grouplist == NULL) {
7017             return PyErr_NoMemory();
7018         }
7019     }
7020 
7021     n = getgroups(n, alt_grouplist);
7022     if (n == -1) {
7023         if (alt_grouplist != grouplist) {
7024             PyMem_Free(alt_grouplist);
7025         }
7026         return posix_error();
7027     }
7028 #else
7029     n = getgroups(MAX_GROUPS, grouplist);
7030     if (n < 0) {
7031         if (errno == EINVAL) {
7032             n = getgroups(0, NULL);
7033             if (n == -1) {
7034                 return posix_error();
7035             }
7036             if (n == 0) {
7037                 /* Avoid malloc(0) */
7038                 alt_grouplist = grouplist;
7039             } else {
7040                 alt_grouplist = PyMem_New(gid_t, n);
7041                 if (alt_grouplist == NULL) {
7042                     return PyErr_NoMemory();
7043                 }
7044                 n = getgroups(n, alt_grouplist);
7045                 if (n == -1) {
7046                     PyMem_Free(alt_grouplist);
7047                     return posix_error();
7048                 }
7049             }
7050         } else {
7051             return posix_error();
7052         }
7053     }
7054 #endif
7055 
7056     result = PyList_New(n);
7057     if (result != NULL) {
7058         int i;
7059         for (i = 0; i < n; ++i) {
7060             PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
7061             if (o == NULL) {
7062                 Py_DECREF(result);
7063                 result = NULL;
7064                 break;
7065             }
7066             PyList_SET_ITEM(result, i, o);
7067         }
7068     }
7069 
7070     if (alt_grouplist != grouplist) {
7071         PyMem_Free(alt_grouplist);
7072     }
7073 
7074     return result;
7075 }
7076 #endif /* HAVE_GETGROUPS */
7077 
7078 #ifdef HAVE_INITGROUPS
7079 PyDoc_STRVAR(posix_initgroups__doc__,
7080 "initgroups(username, gid) -> None\n\n\
7081 Call the system initgroups() to initialize the group access list with all of\n\
7082 the groups of which the specified username is a member, plus the specified\n\
7083 group id.");
7084 
7085 /* AC 3.5: funny apple logic */
7086 static PyObject *
posix_initgroups(PyObject * self,PyObject * args)7087 posix_initgroups(PyObject *self, PyObject *args)
7088 {
7089     PyObject *oname;
7090     const char *username;
7091     int res;
7092 #ifdef __APPLE__
7093     int gid;
7094 #else
7095     gid_t gid;
7096 #endif
7097 
7098 #ifdef __APPLE__
7099     if (!PyArg_ParseTuple(args, "O&i:initgroups",
7100                           PyUnicode_FSConverter, &oname,
7101                           &gid))
7102 #else
7103     if (!PyArg_ParseTuple(args, "O&O&:initgroups",
7104                           PyUnicode_FSConverter, &oname,
7105                           _Py_Gid_Converter, &gid))
7106 #endif
7107         return NULL;
7108     username = PyBytes_AS_STRING(oname);
7109 
7110     res = initgroups(username, gid);
7111     Py_DECREF(oname);
7112     if (res == -1)
7113         return PyErr_SetFromErrno(PyExc_OSError);
7114 
7115     Py_RETURN_NONE;
7116 }
7117 #endif /* HAVE_INITGROUPS */
7118 
7119 
7120 #ifdef HAVE_GETPGID
7121 /*[clinic input]
7122 os.getpgid
7123 
7124     pid: pid_t
7125 
7126 Call the system call getpgid(), and return the result.
7127 [clinic start generated code]*/
7128 
7129 static PyObject *
os_getpgid_impl(PyObject * module,pid_t pid)7130 os_getpgid_impl(PyObject *module, pid_t pid)
7131 /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
7132 {
7133     pid_t pgid = getpgid(pid);
7134     if (pgid < 0)
7135         return posix_error();
7136     return PyLong_FromPid(pgid);
7137 }
7138 #endif /* HAVE_GETPGID */
7139 
7140 
7141 #ifdef HAVE_GETPGRP
7142 /*[clinic input]
7143 os.getpgrp
7144 
7145 Return the current process group id.
7146 [clinic start generated code]*/
7147 
7148 static PyObject *
os_getpgrp_impl(PyObject * module)7149 os_getpgrp_impl(PyObject *module)
7150 /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
7151 {
7152 #ifdef GETPGRP_HAVE_ARG
7153     return PyLong_FromPid(getpgrp(0));
7154 #else /* GETPGRP_HAVE_ARG */
7155     return PyLong_FromPid(getpgrp());
7156 #endif /* GETPGRP_HAVE_ARG */
7157 }
7158 #endif /* HAVE_GETPGRP */
7159 
7160 
7161 #ifdef HAVE_SETPGRP
7162 /*[clinic input]
7163 os.setpgrp
7164 
7165 Make the current process the leader of its process group.
7166 [clinic start generated code]*/
7167 
7168 static PyObject *
os_setpgrp_impl(PyObject * module)7169 os_setpgrp_impl(PyObject *module)
7170 /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
7171 {
7172 #ifdef SETPGRP_HAVE_ARG
7173     if (setpgrp(0, 0) < 0)
7174 #else /* SETPGRP_HAVE_ARG */
7175     if (setpgrp() < 0)
7176 #endif /* SETPGRP_HAVE_ARG */
7177         return posix_error();
7178     Py_RETURN_NONE;
7179 }
7180 #endif /* HAVE_SETPGRP */
7181 
7182 #ifdef HAVE_GETPPID
7183 
7184 #ifdef MS_WINDOWS
7185 #include <tlhelp32.h>
7186 
7187 static PyObject*
win32_getppid()7188 win32_getppid()
7189 {
7190     HANDLE snapshot;
7191     pid_t mypid;
7192     PyObject* result = NULL;
7193     BOOL have_record;
7194     PROCESSENTRY32 pe;
7195 
7196     mypid = getpid(); /* This function never fails */
7197 
7198     snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7199     if (snapshot == INVALID_HANDLE_VALUE)
7200         return PyErr_SetFromWindowsErr(GetLastError());
7201 
7202     pe.dwSize = sizeof(pe);
7203     have_record = Process32First(snapshot, &pe);
7204     while (have_record) {
7205         if (mypid == (pid_t)pe.th32ProcessID) {
7206             /* We could cache the ulong value in a static variable. */
7207             result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7208             break;
7209         }
7210 
7211         have_record = Process32Next(snapshot, &pe);
7212     }
7213 
7214     /* If our loop exits and our pid was not found (result will be NULL)
7215      * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7216      * error anyway, so let's raise it. */
7217     if (!result)
7218         result = PyErr_SetFromWindowsErr(GetLastError());
7219 
7220     CloseHandle(snapshot);
7221 
7222     return result;
7223 }
7224 #endif /*MS_WINDOWS*/
7225 
7226 
7227 /*[clinic input]
7228 os.getppid
7229 
7230 Return the parent's process id.
7231 
7232 If the parent process has already exited, Windows machines will still
7233 return its id; others systems will return the id of the 'init' process (1).
7234 [clinic start generated code]*/
7235 
7236 static PyObject *
os_getppid_impl(PyObject * module)7237 os_getppid_impl(PyObject *module)
7238 /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
7239 {
7240 #ifdef MS_WINDOWS
7241     return win32_getppid();
7242 #else
7243     return PyLong_FromPid(getppid());
7244 #endif
7245 }
7246 #endif /* HAVE_GETPPID */
7247 
7248 
7249 #ifdef HAVE_GETLOGIN
7250 /*[clinic input]
7251 os.getlogin
7252 
7253 Return the actual login name.
7254 [clinic start generated code]*/
7255 
7256 static PyObject *
os_getlogin_impl(PyObject * module)7257 os_getlogin_impl(PyObject *module)
7258 /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
7259 {
7260     PyObject *result = NULL;
7261 #ifdef MS_WINDOWS
7262     wchar_t user_name[UNLEN + 1];
7263     DWORD num_chars = Py_ARRAY_LENGTH(user_name);
7264 
7265     if (GetUserNameW(user_name, &num_chars)) {
7266         /* num_chars is the number of unicode chars plus null terminator */
7267         result = PyUnicode_FromWideChar(user_name, num_chars - 1);
7268     }
7269     else
7270         result = PyErr_SetFromWindowsErr(GetLastError());
7271 #else
7272     char *name;
7273     int old_errno = errno;
7274 
7275     errno = 0;
7276     name = getlogin();
7277     if (name == NULL) {
7278         if (errno)
7279             posix_error();
7280         else
7281             PyErr_SetString(PyExc_OSError, "unable to determine login name");
7282     }
7283     else
7284         result = PyUnicode_DecodeFSDefault(name);
7285     errno = old_errno;
7286 #endif
7287     return result;
7288 }
7289 #endif /* HAVE_GETLOGIN */
7290 
7291 
7292 #ifdef HAVE_GETUID
7293 /*[clinic input]
7294 os.getuid
7295 
7296 Return the current process's user id.
7297 [clinic start generated code]*/
7298 
7299 static PyObject *
os_getuid_impl(PyObject * module)7300 os_getuid_impl(PyObject *module)
7301 /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
7302 {
7303     return _PyLong_FromUid(getuid());
7304 }
7305 #endif /* HAVE_GETUID */
7306 
7307 
7308 #ifdef MS_WINDOWS
7309 #define HAVE_KILL
7310 #endif /* MS_WINDOWS */
7311 
7312 #ifdef HAVE_KILL
7313 /*[clinic input]
7314 os.kill
7315 
7316     pid: pid_t
7317     signal: Py_ssize_t
7318     /
7319 
7320 Kill a process with a signal.
7321 [clinic start generated code]*/
7322 
7323 static PyObject *
os_kill_impl(PyObject * module,pid_t pid,Py_ssize_t signal)7324 os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7325 /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
7326 {
7327     if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
7328         return NULL;
7329     }
7330 #ifndef MS_WINDOWS
7331     if (kill(pid, (int)signal) == -1)
7332         return posix_error();
7333     Py_RETURN_NONE;
7334 #else /* !MS_WINDOWS */
7335     PyObject *result;
7336     DWORD sig = (DWORD)signal;
7337     DWORD err;
7338     HANDLE handle;
7339 
7340     /* Console processes which share a common console can be sent CTRL+C or
7341        CTRL+BREAK events, provided they handle said events. */
7342     if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
7343         if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
7344             err = GetLastError();
7345             PyErr_SetFromWindowsErr(err);
7346         }
7347         else
7348             Py_RETURN_NONE;
7349     }
7350 
7351     /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7352        attempt to open and terminate the process. */
7353     handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
7354     if (handle == NULL) {
7355         err = GetLastError();
7356         return PyErr_SetFromWindowsErr(err);
7357     }
7358 
7359     if (TerminateProcess(handle, sig) == 0) {
7360         err = GetLastError();
7361         result = PyErr_SetFromWindowsErr(err);
7362     } else {
7363         Py_INCREF(Py_None);
7364         result = Py_None;
7365     }
7366 
7367     CloseHandle(handle);
7368     return result;
7369 #endif /* !MS_WINDOWS */
7370 }
7371 #endif /* HAVE_KILL */
7372 
7373 
7374 #ifdef HAVE_KILLPG
7375 /*[clinic input]
7376 os.killpg
7377 
7378     pgid: pid_t
7379     signal: int
7380     /
7381 
7382 Kill a process group with a signal.
7383 [clinic start generated code]*/
7384 
7385 static PyObject *
os_killpg_impl(PyObject * module,pid_t pgid,int signal)7386 os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7387 /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
7388 {
7389     if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
7390         return NULL;
7391     }
7392     /* XXX some man pages make the `pgid` parameter an int, others
7393        a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7394        take the same type. Moreover, pid_t is always at least as wide as
7395        int (else compilation of this module fails), which is safe. */
7396     if (killpg(pgid, signal) == -1)
7397         return posix_error();
7398     Py_RETURN_NONE;
7399 }
7400 #endif /* HAVE_KILLPG */
7401 
7402 
7403 #ifdef HAVE_PLOCK
7404 #ifdef HAVE_SYS_LOCK_H
7405 #include <sys/lock.h>
7406 #endif
7407 
7408 /*[clinic input]
7409 os.plock
7410     op: int
7411     /
7412 
7413 Lock program segments into memory.");
7414 [clinic start generated code]*/
7415 
7416 static PyObject *
os_plock_impl(PyObject * module,int op)7417 os_plock_impl(PyObject *module, int op)
7418 /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
7419 {
7420     if (plock(op) == -1)
7421         return posix_error();
7422     Py_RETURN_NONE;
7423 }
7424 #endif /* HAVE_PLOCK */
7425 
7426 
7427 #ifdef HAVE_SETUID
7428 /*[clinic input]
7429 os.setuid
7430 
7431     uid: uid_t
7432     /
7433 
7434 Set the current process's user id.
7435 [clinic start generated code]*/
7436 
7437 static PyObject *
os_setuid_impl(PyObject * module,uid_t uid)7438 os_setuid_impl(PyObject *module, uid_t uid)
7439 /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
7440 {
7441     if (setuid(uid) < 0)
7442         return posix_error();
7443     Py_RETURN_NONE;
7444 }
7445 #endif /* HAVE_SETUID */
7446 
7447 
7448 #ifdef HAVE_SETEUID
7449 /*[clinic input]
7450 os.seteuid
7451 
7452     euid: uid_t
7453     /
7454 
7455 Set the current process's effective user id.
7456 [clinic start generated code]*/
7457 
7458 static PyObject *
os_seteuid_impl(PyObject * module,uid_t euid)7459 os_seteuid_impl(PyObject *module, uid_t euid)
7460 /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
7461 {
7462     if (seteuid(euid) < 0)
7463         return posix_error();
7464     Py_RETURN_NONE;
7465 }
7466 #endif /* HAVE_SETEUID */
7467 
7468 
7469 #ifdef HAVE_SETEGID
7470 /*[clinic input]
7471 os.setegid
7472 
7473     egid: gid_t
7474     /
7475 
7476 Set the current process's effective group id.
7477 [clinic start generated code]*/
7478 
7479 static PyObject *
os_setegid_impl(PyObject * module,gid_t egid)7480 os_setegid_impl(PyObject *module, gid_t egid)
7481 /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
7482 {
7483     if (setegid(egid) < 0)
7484         return posix_error();
7485     Py_RETURN_NONE;
7486 }
7487 #endif /* HAVE_SETEGID */
7488 
7489 
7490 #ifdef HAVE_SETREUID
7491 /*[clinic input]
7492 os.setreuid
7493 
7494     ruid: uid_t
7495     euid: uid_t
7496     /
7497 
7498 Set the current process's real and effective user ids.
7499 [clinic start generated code]*/
7500 
7501 static PyObject *
os_setreuid_impl(PyObject * module,uid_t ruid,uid_t euid)7502 os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7503 /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
7504 {
7505     if (setreuid(ruid, euid) < 0) {
7506         return posix_error();
7507     } else {
7508         Py_RETURN_NONE;
7509     }
7510 }
7511 #endif /* HAVE_SETREUID */
7512 
7513 
7514 #ifdef HAVE_SETREGID
7515 /*[clinic input]
7516 os.setregid
7517 
7518     rgid: gid_t
7519     egid: gid_t
7520     /
7521 
7522 Set the current process's real and effective group ids.
7523 [clinic start generated code]*/
7524 
7525 static PyObject *
os_setregid_impl(PyObject * module,gid_t rgid,gid_t egid)7526 os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7527 /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
7528 {
7529     if (setregid(rgid, egid) < 0)
7530         return posix_error();
7531     Py_RETURN_NONE;
7532 }
7533 #endif /* HAVE_SETREGID */
7534 
7535 
7536 #ifdef HAVE_SETGID
7537 /*[clinic input]
7538 os.setgid
7539     gid: gid_t
7540     /
7541 
7542 Set the current process's group id.
7543 [clinic start generated code]*/
7544 
7545 static PyObject *
os_setgid_impl(PyObject * module,gid_t gid)7546 os_setgid_impl(PyObject *module, gid_t gid)
7547 /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
7548 {
7549     if (setgid(gid) < 0)
7550         return posix_error();
7551     Py_RETURN_NONE;
7552 }
7553 #endif /* HAVE_SETGID */
7554 
7555 
7556 #ifdef HAVE_SETGROUPS
7557 /*[clinic input]
7558 os.setgroups
7559 
7560     groups: object
7561     /
7562 
7563 Set the groups of the current process to list.
7564 [clinic start generated code]*/
7565 
7566 static PyObject *
os_setgroups(PyObject * module,PyObject * groups)7567 os_setgroups(PyObject *module, PyObject *groups)
7568 /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
7569 {
7570     Py_ssize_t i, len;
7571     gid_t grouplist[MAX_GROUPS];
7572 
7573     if (!PySequence_Check(groups)) {
7574         PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7575         return NULL;
7576     }
7577     len = PySequence_Size(groups);
7578     if (len < 0) {
7579         return NULL;
7580     }
7581     if (len > MAX_GROUPS) {
7582         PyErr_SetString(PyExc_ValueError, "too many groups");
7583         return NULL;
7584     }
7585     for(i = 0; i < len; i++) {
7586         PyObject *elem;
7587         elem = PySequence_GetItem(groups, i);
7588         if (!elem)
7589             return NULL;
7590         if (!PyLong_Check(elem)) {
7591             PyErr_SetString(PyExc_TypeError,
7592                             "groups must be integers");
7593             Py_DECREF(elem);
7594             return NULL;
7595         } else {
7596             if (!_Py_Gid_Converter(elem, &grouplist[i])) {
7597                 Py_DECREF(elem);
7598                 return NULL;
7599             }
7600         }
7601         Py_DECREF(elem);
7602     }
7603 
7604     if (setgroups(len, grouplist) < 0)
7605         return posix_error();
7606     Py_RETURN_NONE;
7607 }
7608 #endif /* HAVE_SETGROUPS */
7609 
7610 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7611 static PyObject *
wait_helper(pid_t pid,int status,struct rusage * ru)7612 wait_helper(pid_t pid, int status, struct rusage *ru)
7613 {
7614     PyObject *result;
7615     static PyObject *struct_rusage;
7616     _Py_IDENTIFIER(struct_rusage);
7617 
7618     if (pid == -1)
7619         return posix_error();
7620 
7621     if (struct_rusage == NULL) {
7622         PyObject *m = PyImport_ImportModuleNoBlock("resource");
7623         if (m == NULL)
7624             return NULL;
7625         struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
7626         Py_DECREF(m);
7627         if (struct_rusage == NULL)
7628             return NULL;
7629     }
7630 
7631     /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7632     result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7633     if (!result)
7634         return NULL;
7635 
7636 #ifndef doubletime
7637 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7638 #endif
7639 
7640     PyStructSequence_SET_ITEM(result, 0,
7641                               PyFloat_FromDouble(doubletime(ru->ru_utime)));
7642     PyStructSequence_SET_ITEM(result, 1,
7643                               PyFloat_FromDouble(doubletime(ru->ru_stime)));
7644 #define SET_INT(result, index, value)\
7645         PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7646     SET_INT(result, 2, ru->ru_maxrss);
7647     SET_INT(result, 3, ru->ru_ixrss);
7648     SET_INT(result, 4, ru->ru_idrss);
7649     SET_INT(result, 5, ru->ru_isrss);
7650     SET_INT(result, 6, ru->ru_minflt);
7651     SET_INT(result, 7, ru->ru_majflt);
7652     SET_INT(result, 8, ru->ru_nswap);
7653     SET_INT(result, 9, ru->ru_inblock);
7654     SET_INT(result, 10, ru->ru_oublock);
7655     SET_INT(result, 11, ru->ru_msgsnd);
7656     SET_INT(result, 12, ru->ru_msgrcv);
7657     SET_INT(result, 13, ru->ru_nsignals);
7658     SET_INT(result, 14, ru->ru_nvcsw);
7659     SET_INT(result, 15, ru->ru_nivcsw);
7660 #undef SET_INT
7661 
7662     if (PyErr_Occurred()) {
7663         Py_DECREF(result);
7664         return NULL;
7665     }
7666 
7667     return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
7668 }
7669 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7670 
7671 
7672 #ifdef HAVE_WAIT3
7673 /*[clinic input]
7674 os.wait3
7675 
7676     options: int
7677 Wait for completion of a child process.
7678 
7679 Returns a tuple of information about the child process:
7680   (pid, status, rusage)
7681 [clinic start generated code]*/
7682 
7683 static PyObject *
os_wait3_impl(PyObject * module,int options)7684 os_wait3_impl(PyObject *module, int options)
7685 /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
7686 {
7687     pid_t pid;
7688     struct rusage ru;
7689     int async_err = 0;
7690     WAIT_TYPE status;
7691     WAIT_STATUS_INT(status) = 0;
7692 
7693     do {
7694         Py_BEGIN_ALLOW_THREADS
7695         pid = wait3(&status, options, &ru);
7696         Py_END_ALLOW_THREADS
7697     } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7698     if (pid < 0)
7699         return (!async_err) ? posix_error() : NULL;
7700 
7701     return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
7702 }
7703 #endif /* HAVE_WAIT3 */
7704 
7705 
7706 #ifdef HAVE_WAIT4
7707 /*[clinic input]
7708 
7709 os.wait4
7710 
7711     pid: pid_t
7712     options: int
7713 
7714 Wait for completion of a specific child process.
7715 
7716 Returns a tuple of information about the child process:
7717   (pid, status, rusage)
7718 [clinic start generated code]*/
7719 
7720 static PyObject *
os_wait4_impl(PyObject * module,pid_t pid,int options)7721 os_wait4_impl(PyObject *module, pid_t pid, int options)
7722 /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
7723 {
7724     pid_t res;
7725     struct rusage ru;
7726     int async_err = 0;
7727     WAIT_TYPE status;
7728     WAIT_STATUS_INT(status) = 0;
7729 
7730     do {
7731         Py_BEGIN_ALLOW_THREADS
7732         res = wait4(pid, &status, options, &ru);
7733         Py_END_ALLOW_THREADS
7734     } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7735     if (res < 0)
7736         return (!async_err) ? posix_error() : NULL;
7737 
7738     return wait_helper(res, WAIT_STATUS_INT(status), &ru);
7739 }
7740 #endif /* HAVE_WAIT4 */
7741 
7742 
7743 #if defined(HAVE_WAITID) && !defined(__APPLE__)
7744 /*[clinic input]
7745 os.waitid
7746 
7747     idtype: idtype_t
7748         Must be one of be P_PID, P_PGID or P_ALL.
7749     id: id_t
7750         The id to wait on.
7751     options: int
7752         Constructed from the ORing of one or more of WEXITED, WSTOPPED
7753         or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7754     /
7755 
7756 Returns the result of waiting for a process or processes.
7757 
7758 Returns either waitid_result or None if WNOHANG is specified and there are
7759 no children in a waitable state.
7760 [clinic start generated code]*/
7761 
7762 static PyObject *
os_waitid_impl(PyObject * module,idtype_t idtype,id_t id,int options)7763 os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7764 /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
7765 {
7766     PyObject *result;
7767     int res;
7768     int async_err = 0;
7769     siginfo_t si;
7770     si.si_pid = 0;
7771 
7772     do {
7773         Py_BEGIN_ALLOW_THREADS
7774         res = waitid(idtype, id, &si, options);
7775         Py_END_ALLOW_THREADS
7776     } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7777     if (res < 0)
7778         return (!async_err) ? posix_error() : NULL;
7779 
7780     if (si.si_pid == 0)
7781         Py_RETURN_NONE;
7782 
7783     result = PyStructSequence_New(WaitidResultType);
7784     if (!result)
7785         return NULL;
7786 
7787     PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
7788     PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
7789     PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7790     PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7791     PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7792     if (PyErr_Occurred()) {
7793         Py_DECREF(result);
7794         return NULL;
7795     }
7796 
7797     return result;
7798 }
7799 #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
7800 
7801 
7802 #if defined(HAVE_WAITPID)
7803 /*[clinic input]
7804 os.waitpid
7805     pid: pid_t
7806     options: int
7807     /
7808 
7809 Wait for completion of a given child process.
7810 
7811 Returns a tuple of information regarding the child process:
7812     (pid, status)
7813 
7814 The options argument is ignored on Windows.
7815 [clinic start generated code]*/
7816 
7817 static PyObject *
os_waitpid_impl(PyObject * module,pid_t pid,int options)7818 os_waitpid_impl(PyObject *module, pid_t pid, int options)
7819 /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
7820 {
7821     pid_t res;
7822     int async_err = 0;
7823     WAIT_TYPE status;
7824     WAIT_STATUS_INT(status) = 0;
7825 
7826     do {
7827         Py_BEGIN_ALLOW_THREADS
7828         res = waitpid(pid, &status, options);
7829         Py_END_ALLOW_THREADS
7830     } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7831     if (res < 0)
7832         return (!async_err) ? posix_error() : NULL;
7833 
7834     return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
7835 }
7836 #elif defined(HAVE_CWAIT)
7837 /* MS C has a variant of waitpid() that's usable for most purposes. */
7838 /*[clinic input]
7839 os.waitpid
7840     pid: intptr_t
7841     options: int
7842     /
7843 
7844 Wait for completion of a given process.
7845 
7846 Returns a tuple of information regarding the process:
7847     (pid, status << 8)
7848 
7849 The options argument is ignored on Windows.
7850 [clinic start generated code]*/
7851 
7852 static PyObject *
os_waitpid_impl(PyObject * module,intptr_t pid,int options)7853 os_waitpid_impl(PyObject *module, intptr_t pid, int options)
7854 /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
7855 {
7856     int status;
7857     intptr_t res;
7858     int async_err = 0;
7859 
7860     do {
7861         Py_BEGIN_ALLOW_THREADS
7862         _Py_BEGIN_SUPPRESS_IPH
7863         res = _cwait(&status, pid, options);
7864         _Py_END_SUPPRESS_IPH
7865         Py_END_ALLOW_THREADS
7866     } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7867     if (res < 0)
7868         return (!async_err) ? posix_error() : NULL;
7869 
7870     unsigned long long ustatus = (unsigned int)status;
7871 
7872     /* shift the status left a byte so this is more like the POSIX waitpid */
7873     return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8);
7874 }
7875 #endif
7876 
7877 
7878 #ifdef HAVE_WAIT
7879 /*[clinic input]
7880 os.wait
7881 
7882 Wait for completion of a child process.
7883 
7884 Returns a tuple of information about the child process:
7885     (pid, status)
7886 [clinic start generated code]*/
7887 
7888 static PyObject *
os_wait_impl(PyObject * module)7889 os_wait_impl(PyObject *module)
7890 /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
7891 {
7892     pid_t pid;
7893     int async_err = 0;
7894     WAIT_TYPE status;
7895     WAIT_STATUS_INT(status) = 0;
7896 
7897     do {
7898         Py_BEGIN_ALLOW_THREADS
7899         pid = wait(&status);
7900         Py_END_ALLOW_THREADS
7901     } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7902     if (pid < 0)
7903         return (!async_err) ? posix_error() : NULL;
7904 
7905     return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
7906 }
7907 #endif /* HAVE_WAIT */
7908 
7909 
7910 #if defined(HAVE_READLINK) || defined(MS_WINDOWS)
7911 /*[clinic input]
7912 os.readlink
7913 
7914     path: path_t
7915     *
7916     dir_fd: dir_fd(requires='readlinkat') = None
7917 
7918 Return a string representing the path to which the symbolic link points.
7919 
7920 If dir_fd is not None, it should be a file descriptor open to a directory,
7921 and path should be relative; path will then be relative to that directory.
7922 
7923 dir_fd may not be implemented on your platform.  If it is unavailable,
7924 using it will raise a NotImplementedError.
7925 [clinic start generated code]*/
7926 
7927 static PyObject *
os_readlink_impl(PyObject * module,path_t * path,int dir_fd)7928 os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7929 /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
7930 {
7931 #if defined(HAVE_READLINK)
7932     char buffer[MAXPATHLEN+1];
7933     ssize_t length;
7934 
7935     Py_BEGIN_ALLOW_THREADS
7936 #ifdef HAVE_READLINKAT
7937     if (dir_fd != DEFAULT_DIR_FD)
7938         length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7939     else
7940 #endif
7941         length = readlink(path->narrow, buffer, MAXPATHLEN);
7942     Py_END_ALLOW_THREADS
7943 
7944     if (length < 0) {
7945         return path_error(path);
7946     }
7947     buffer[length] = '\0';
7948 
7949     if (PyUnicode_Check(path->object))
7950         return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7951     else
7952         return PyBytes_FromStringAndSize(buffer, length);
7953 #elif defined(MS_WINDOWS)
7954     DWORD n_bytes_returned;
7955     DWORD io_result = 0;
7956     HANDLE reparse_point_handle;
7957     char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7958     _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
7959     PyObject *result = NULL;
7960 
7961     /* First get a handle to the reparse point */
7962     Py_BEGIN_ALLOW_THREADS
7963     reparse_point_handle = CreateFileW(
7964         path->wide,
7965         0,
7966         0,
7967         0,
7968         OPEN_EXISTING,
7969         FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7970         0);
7971     if (reparse_point_handle != INVALID_HANDLE_VALUE) {
7972         /* New call DeviceIoControl to read the reparse point */
7973         io_result = DeviceIoControl(
7974             reparse_point_handle,
7975             FSCTL_GET_REPARSE_POINT,
7976             0, 0, /* in buffer */
7977             target_buffer, sizeof(target_buffer),
7978             &n_bytes_returned,
7979             0 /* we're not using OVERLAPPED_IO */
7980             );
7981         CloseHandle(reparse_point_handle);
7982     }
7983     Py_END_ALLOW_THREADS
7984 
7985     if (io_result == 0) {
7986         return path_error(path);
7987     }
7988 
7989     wchar_t *name = NULL;
7990     Py_ssize_t nameLen = 0;
7991     if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
7992     {
7993         name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7994                            rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
7995         nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
7996     }
7997     else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
7998     {
7999         name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
8000                            rdb->MountPointReparseBuffer.SubstituteNameOffset);
8001         nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
8002     }
8003     else
8004     {
8005         PyErr_SetString(PyExc_ValueError, "not a symbolic link");
8006     }
8007     if (name) {
8008         if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
8009             /* Our buffer is mutable, so this is okay */
8010             name[1] = L'\\';
8011         }
8012         result = PyUnicode_FromWideChar(name, nameLen);
8013         if (result && path->narrow) {
8014             Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
8015         }
8016     }
8017     return result;
8018 #endif
8019 }
8020 #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
8021 
8022 #if defined(MS_WINDOWS)
8023 
8024 /* Remove the last portion of the path - return 0 on success */
8025 static int
_dirnameW(WCHAR * path)8026 _dirnameW(WCHAR *path)
8027 {
8028     WCHAR *ptr;
8029     size_t length = wcsnlen_s(path, MAX_PATH);
8030     if (length == MAX_PATH) {
8031         return -1;
8032     }
8033 
8034     /* walk the path from the end until a backslash is encountered */
8035     for(ptr = path + length; ptr != path; ptr--) {
8036         if (*ptr == L'\\' || *ptr == L'/') {
8037             break;
8038         }
8039     }
8040     *ptr = 0;
8041     return 0;
8042 }
8043 
8044 #endif
8045 
8046 #ifdef HAVE_SYMLINK
8047 
8048 #if defined(MS_WINDOWS)
8049 
8050 /* Is this path absolute? */
8051 static int
_is_absW(const WCHAR * path)8052 _is_absW(const WCHAR *path)
8053 {
8054     return path[0] == L'\\' || path[0] == L'/' ||
8055         (path[0] && path[1] == L':');
8056 }
8057 
8058 /* join root and rest with a backslash - return 0 on success */
8059 static int
_joinW(WCHAR * dest_path,const WCHAR * root,const WCHAR * rest)8060 _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8061 {
8062     if (_is_absW(rest)) {
8063         return wcscpy_s(dest_path, MAX_PATH, rest);
8064     }
8065 
8066     if (wcscpy_s(dest_path, MAX_PATH, root)) {
8067         return -1;
8068     }
8069 
8070     if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8071         return -1;
8072     }
8073 
8074     return wcscat_s(dest_path, MAX_PATH, rest);
8075 }
8076 
8077 /* Return True if the path at src relative to dest is a directory */
8078 static int
_check_dirW(LPCWSTR src,LPCWSTR dest)8079 _check_dirW(LPCWSTR src, LPCWSTR dest)
8080 {
8081     WIN32_FILE_ATTRIBUTE_DATA src_info;
8082     WCHAR dest_parent[MAX_PATH];
8083     WCHAR src_resolved[MAX_PATH] = L"";
8084 
8085     /* dest_parent = os.path.dirname(dest) */
8086     if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8087         _dirnameW(dest_parent)) {
8088         return 0;
8089     }
8090     /* src_resolved = os.path.join(dest_parent, src) */
8091     if (_joinW(src_resolved, dest_parent, src)) {
8092         return 0;
8093     }
8094     return (
8095         GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8096         && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8097     );
8098 }
8099 #endif
8100 
8101 
8102 /*[clinic input]
8103 os.symlink
8104     src: path_t
8105     dst: path_t
8106     target_is_directory: bool = False
8107     *
8108     dir_fd: dir_fd(requires='symlinkat')=None
8109 
8110 # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8111 
8112 Create a symbolic link pointing to src named dst.
8113 
8114 target_is_directory is required on Windows if the target is to be
8115   interpreted as a directory.  (On Windows, symlink requires
8116   Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8117   target_is_directory is ignored on non-Windows platforms.
8118 
8119 If dir_fd is not None, it should be a file descriptor open to a directory,
8120   and path should be relative; path will then be relative to that directory.
8121 dir_fd may not be implemented on your platform.
8122   If it is unavailable, using it will raise a NotImplementedError.
8123 
8124 [clinic start generated code]*/
8125 
8126 static PyObject *
os_symlink_impl(PyObject * module,path_t * src,path_t * dst,int target_is_directory,int dir_fd)8127 os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
8128                 int target_is_directory, int dir_fd)
8129 /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
8130 {
8131 #ifdef MS_WINDOWS
8132     DWORD result;
8133     DWORD flags = 0;
8134 
8135     /* Assumed true, set to false if detected to not be available. */
8136     static int windows_has_symlink_unprivileged_flag = TRUE;
8137 #else
8138     int result;
8139 #endif
8140 
8141     if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
8142                     dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
8143         return NULL;
8144     }
8145 
8146 #ifdef MS_WINDOWS
8147 
8148     if (windows_has_symlink_unprivileged_flag) {
8149         /* Allow non-admin symlinks if system allows it. */
8150         flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8151     }
8152 
8153     Py_BEGIN_ALLOW_THREADS
8154     _Py_BEGIN_SUPPRESS_IPH
8155     /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8156     if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8157         flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8158     }
8159 
8160     result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8161     _Py_END_SUPPRESS_IPH
8162     Py_END_ALLOW_THREADS
8163 
8164     if (windows_has_symlink_unprivileged_flag && !result &&
8165         ERROR_INVALID_PARAMETER == GetLastError()) {
8166 
8167         Py_BEGIN_ALLOW_THREADS
8168         _Py_BEGIN_SUPPRESS_IPH
8169         /* This error might be caused by
8170         SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8171         Try again, and update windows_has_symlink_unprivileged_flag if we
8172         are successful this time.
8173 
8174         NOTE: There is a risk of a race condition here if there are other
8175         conditions than the flag causing ERROR_INVALID_PARAMETER, and
8176         another process (or thread) changes that condition in between our
8177         calls to CreateSymbolicLink.
8178         */
8179         flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8180         result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8181         _Py_END_SUPPRESS_IPH
8182         Py_END_ALLOW_THREADS
8183 
8184         if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8185             windows_has_symlink_unprivileged_flag = FALSE;
8186         }
8187     }
8188 
8189     if (!result)
8190         return path_error2(src, dst);
8191 
8192 #else
8193 
8194     if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8195         PyErr_SetString(PyExc_ValueError,
8196             "symlink: src and dst must be the same type");
8197         return NULL;
8198     }
8199 
8200     Py_BEGIN_ALLOW_THREADS
8201 #if HAVE_SYMLINKAT
8202     if (dir_fd != DEFAULT_DIR_FD)
8203         result = symlinkat(src->narrow, dir_fd, dst->narrow);
8204     else
8205 #endif
8206         result = symlink(src->narrow, dst->narrow);
8207     Py_END_ALLOW_THREADS
8208 
8209     if (result)
8210         return path_error2(src, dst);
8211 #endif
8212 
8213     Py_RETURN_NONE;
8214 }
8215 #endif /* HAVE_SYMLINK */
8216 
8217 
8218 
8219 
8220 static PyStructSequence_Field times_result_fields[] = {
8221     {"user",    "user time"},
8222     {"system",   "system time"},
8223     {"children_user",    "user time of children"},
8224     {"children_system",    "system time of children"},
8225     {"elapsed",    "elapsed time since an arbitrary point in the past"},
8226     {NULL}
8227 };
8228 
8229 PyDoc_STRVAR(times_result__doc__,
8230 "times_result: Result from os.times().\n\n\
8231 This object may be accessed either as a tuple of\n\
8232   (user, system, children_user, children_system, elapsed),\n\
8233 or via the attributes user, system, children_user, children_system,\n\
8234 and elapsed.\n\
8235 \n\
8236 See os.times for more information.");
8237 
8238 static PyStructSequence_Desc times_result_desc = {
8239     "times_result", /* name */
8240     times_result__doc__, /* doc */
8241     times_result_fields,
8242     5
8243 };
8244 
8245 static PyTypeObject* TimesResultType;
8246 
8247 #ifdef MS_WINDOWS
8248 #define HAVE_TIMES  /* mandatory, for the method table */
8249 #endif
8250 
8251 #ifdef HAVE_TIMES
8252 
8253 static PyObject *
build_times_result(double user,double system,double children_user,double children_system,double elapsed)8254 build_times_result(double user, double system,
8255     double children_user, double children_system,
8256     double elapsed)
8257 {
8258     PyObject *value = PyStructSequence_New(TimesResultType);
8259     if (value == NULL)
8260         return NULL;
8261 
8262 #define SET(i, field) \
8263     { \
8264     PyObject *o = PyFloat_FromDouble(field); \
8265     if (!o) { \
8266         Py_DECREF(value); \
8267         return NULL; \
8268     } \
8269     PyStructSequence_SET_ITEM(value, i, o); \
8270     } \
8271 
8272     SET(0, user);
8273     SET(1, system);
8274     SET(2, children_user);
8275     SET(3, children_system);
8276     SET(4, elapsed);
8277 
8278 #undef SET
8279 
8280     return value;
8281 }
8282 
8283 
8284 #ifndef MS_WINDOWS
8285 #define NEED_TICKS_PER_SECOND
8286 static long ticks_per_second = -1;
8287 #endif /* MS_WINDOWS */
8288 
8289 /*[clinic input]
8290 os.times
8291 
8292 Return a collection containing process timing information.
8293 
8294 The object returned behaves like a named tuple with these fields:
8295   (utime, stime, cutime, cstime, elapsed_time)
8296 All fields are floating point numbers.
8297 [clinic start generated code]*/
8298 
8299 static PyObject *
os_times_impl(PyObject * module)8300 os_times_impl(PyObject *module)
8301 /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
8302 #ifdef MS_WINDOWS
8303 {
8304     FILETIME create, exit, kernel, user;
8305     HANDLE hProc;
8306     hProc = GetCurrentProcess();
8307     GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8308     /* The fields of a FILETIME structure are the hi and lo part
8309        of a 64-bit value expressed in 100 nanosecond units.
8310        1e7 is one second in such units; 1e-7 the inverse.
8311        429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8312     */
8313     return build_times_result(
8314         (double)(user.dwHighDateTime*429.4967296 +
8315                  user.dwLowDateTime*1e-7),
8316         (double)(kernel.dwHighDateTime*429.4967296 +
8317                  kernel.dwLowDateTime*1e-7),
8318         (double)0,
8319         (double)0,
8320         (double)0);
8321 }
8322 #else /* MS_WINDOWS */
8323 {
8324 
8325 
8326     struct tms t;
8327     clock_t c;
8328     errno = 0;
8329     c = times(&t);
8330     if (c == (clock_t) -1)
8331         return posix_error();
8332     return build_times_result(
8333                          (double)t.tms_utime / ticks_per_second,
8334                          (double)t.tms_stime / ticks_per_second,
8335                          (double)t.tms_cutime / ticks_per_second,
8336                          (double)t.tms_cstime / ticks_per_second,
8337                          (double)c / ticks_per_second);
8338 }
8339 #endif /* MS_WINDOWS */
8340 #endif /* HAVE_TIMES */
8341 
8342 
8343 #ifdef HAVE_GETSID
8344 /*[clinic input]
8345 os.getsid
8346 
8347     pid: pid_t
8348     /
8349 
8350 Call the system call getsid(pid) and return the result.
8351 [clinic start generated code]*/
8352 
8353 static PyObject *
os_getsid_impl(PyObject * module,pid_t pid)8354 os_getsid_impl(PyObject *module, pid_t pid)
8355 /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
8356 {
8357     int sid;
8358     sid = getsid(pid);
8359     if (sid < 0)
8360         return posix_error();
8361     return PyLong_FromLong((long)sid);
8362 }
8363 #endif /* HAVE_GETSID */
8364 
8365 
8366 #ifdef HAVE_SETSID
8367 /*[clinic input]
8368 os.setsid
8369 
8370 Call the system call setsid().
8371 [clinic start generated code]*/
8372 
8373 static PyObject *
os_setsid_impl(PyObject * module)8374 os_setsid_impl(PyObject *module)
8375 /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
8376 {
8377     if (setsid() < 0)
8378         return posix_error();
8379     Py_RETURN_NONE;
8380 }
8381 #endif /* HAVE_SETSID */
8382 
8383 
8384 #ifdef HAVE_SETPGID
8385 /*[clinic input]
8386 os.setpgid
8387 
8388     pid: pid_t
8389     pgrp: pid_t
8390     /
8391 
8392 Call the system call setpgid(pid, pgrp).
8393 [clinic start generated code]*/
8394 
8395 static PyObject *
os_setpgid_impl(PyObject * module,pid_t pid,pid_t pgrp)8396 os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8397 /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
8398 {
8399     if (setpgid(pid, pgrp) < 0)
8400         return posix_error();
8401     Py_RETURN_NONE;
8402 }
8403 #endif /* HAVE_SETPGID */
8404 
8405 
8406 #ifdef HAVE_TCGETPGRP
8407 /*[clinic input]
8408 os.tcgetpgrp
8409 
8410     fd: int
8411     /
8412 
8413 Return the process group associated with the terminal specified by fd.
8414 [clinic start generated code]*/
8415 
8416 static PyObject *
os_tcgetpgrp_impl(PyObject * module,int fd)8417 os_tcgetpgrp_impl(PyObject *module, int fd)
8418 /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
8419 {
8420     pid_t pgid = tcgetpgrp(fd);
8421     if (pgid < 0)
8422         return posix_error();
8423     return PyLong_FromPid(pgid);
8424 }
8425 #endif /* HAVE_TCGETPGRP */
8426 
8427 
8428 #ifdef HAVE_TCSETPGRP
8429 /*[clinic input]
8430 os.tcsetpgrp
8431 
8432     fd: int
8433     pgid: pid_t
8434     /
8435 
8436 Set the process group associated with the terminal specified by fd.
8437 [clinic start generated code]*/
8438 
8439 static PyObject *
os_tcsetpgrp_impl(PyObject * module,int fd,pid_t pgid)8440 os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8441 /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
8442 {
8443     if (tcsetpgrp(fd, pgid) < 0)
8444         return posix_error();
8445     Py_RETURN_NONE;
8446 }
8447 #endif /* HAVE_TCSETPGRP */
8448 
8449 /* Functions acting on file descriptors */
8450 
8451 #ifdef O_CLOEXEC
8452 extern int _Py_open_cloexec_works;
8453 #endif
8454 
8455 
8456 /*[clinic input]
8457 os.open -> int
8458     path: path_t
8459     flags: int
8460     mode: int = 0o777
8461     *
8462     dir_fd: dir_fd(requires='openat') = None
8463 
8464 # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8465 
8466 Open a file for low level IO.  Returns a file descriptor (integer).
8467 
8468 If dir_fd is not None, it should be a file descriptor open to a directory,
8469   and path should be relative; path will then be relative to that directory.
8470 dir_fd may not be implemented on your platform.
8471   If it is unavailable, using it will raise a NotImplementedError.
8472 [clinic start generated code]*/
8473 
8474 static int
os_open_impl(PyObject * module,path_t * path,int flags,int mode,int dir_fd)8475 os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8476 /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
8477 {
8478     int fd;
8479     int async_err = 0;
8480 
8481 #ifdef O_CLOEXEC
8482     int *atomic_flag_works = &_Py_open_cloexec_works;
8483 #elif !defined(MS_WINDOWS)
8484     int *atomic_flag_works = NULL;
8485 #endif
8486 
8487 #ifdef MS_WINDOWS
8488     flags |= O_NOINHERIT;
8489 #elif defined(O_CLOEXEC)
8490     flags |= O_CLOEXEC;
8491 #endif
8492 
8493     if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8494         return -1;
8495     }
8496 
8497     _Py_BEGIN_SUPPRESS_IPH
8498     do {
8499         Py_BEGIN_ALLOW_THREADS
8500 #ifdef MS_WINDOWS
8501         fd = _wopen(path->wide, flags, mode);
8502 #else
8503 #ifdef HAVE_OPENAT
8504         if (dir_fd != DEFAULT_DIR_FD)
8505             fd = openat(dir_fd, path->narrow, flags, mode);
8506         else
8507 #endif /* HAVE_OPENAT */
8508             fd = open(path->narrow, flags, mode);
8509 #endif /* !MS_WINDOWS */
8510         Py_END_ALLOW_THREADS
8511     } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8512     _Py_END_SUPPRESS_IPH
8513 
8514     if (fd < 0) {
8515         if (!async_err)
8516             PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
8517         return -1;
8518     }
8519 
8520 #ifndef MS_WINDOWS
8521     if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8522         close(fd);
8523         return -1;
8524     }
8525 #endif
8526 
8527     return fd;
8528 }
8529 
8530 
8531 /*[clinic input]
8532 os.close
8533 
8534     fd: int
8535 
8536 Close a file descriptor.
8537 [clinic start generated code]*/
8538 
8539 static PyObject *
os_close_impl(PyObject * module,int fd)8540 os_close_impl(PyObject *module, int fd)
8541 /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
8542 {
8543     int res;
8544     /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8545      * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8546      * for more details.
8547      */
8548     Py_BEGIN_ALLOW_THREADS
8549     _Py_BEGIN_SUPPRESS_IPH
8550     res = close(fd);
8551     _Py_END_SUPPRESS_IPH
8552     Py_END_ALLOW_THREADS
8553     if (res < 0)
8554         return posix_error();
8555     Py_RETURN_NONE;
8556 }
8557 
8558 
8559 #ifdef HAVE_FDWALK
8560 static int
_fdwalk_close_func(void * lohi,int fd)8561 _fdwalk_close_func(void *lohi, int fd)
8562 {
8563     int lo = ((int *)lohi)[0];
8564     int hi = ((int *)lohi)[1];
8565 
8566     if (fd >= hi)
8567         return 1;
8568     else if (fd >= lo)
8569         close(fd);
8570     return 0;
8571 }
8572 #endif /* HAVE_FDWALK */
8573 
8574 /*[clinic input]
8575 os.closerange
8576 
8577     fd_low: int
8578     fd_high: int
8579     /
8580 
8581 Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8582 [clinic start generated code]*/
8583 
8584 static PyObject *
os_closerange_impl(PyObject * module,int fd_low,int fd_high)8585 os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8586 /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
8587 {
8588 #ifdef HAVE_FDWALK
8589     int lohi[2];
8590 #else
8591     int i;
8592 #endif
8593     Py_BEGIN_ALLOW_THREADS
8594     _Py_BEGIN_SUPPRESS_IPH
8595 #ifdef HAVE_FDWALK
8596     lohi[0] = Py_MAX(fd_low, 0);
8597     lohi[1] = fd_high;
8598     fdwalk(_fdwalk_close_func, lohi);
8599 #else
8600     for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
8601         close(i);
8602 #endif
8603     _Py_END_SUPPRESS_IPH
8604     Py_END_ALLOW_THREADS
8605     Py_RETURN_NONE;
8606 }
8607 
8608 
8609 /*[clinic input]
8610 os.dup -> int
8611 
8612     fd: int
8613     /
8614 
8615 Return a duplicate of a file descriptor.
8616 [clinic start generated code]*/
8617 
8618 static int
os_dup_impl(PyObject * module,int fd)8619 os_dup_impl(PyObject *module, int fd)
8620 /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
8621 {
8622     return _Py_dup(fd);
8623 }
8624 
8625 
8626 /*[clinic input]
8627 os.dup2 -> int
8628     fd: int
8629     fd2: int
8630     inheritable: bool=True
8631 
8632 Duplicate file descriptor.
8633 [clinic start generated code]*/
8634 
8635 static int
os_dup2_impl(PyObject * module,int fd,int fd2,int inheritable)8636 os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
8637 /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
8638 {
8639     int res = 0;
8640 #if defined(HAVE_DUP3) && \
8641     !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8642     /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
8643     static int dup3_works = -1;
8644 #endif
8645 
8646     if (fd < 0 || fd2 < 0) {
8647         posix_error();
8648         return -1;
8649     }
8650 
8651     /* dup2() can fail with EINTR if the target FD is already open, because it
8652      * then has to be closed. See os_close_impl() for why we don't handle EINTR
8653      * upon close(), and therefore below.
8654      */
8655 #ifdef MS_WINDOWS
8656     Py_BEGIN_ALLOW_THREADS
8657     _Py_BEGIN_SUPPRESS_IPH
8658     res = dup2(fd, fd2);
8659     _Py_END_SUPPRESS_IPH
8660     Py_END_ALLOW_THREADS
8661     if (res < 0) {
8662         posix_error();
8663         return -1;
8664     }
8665     res = fd2; // msvcrt dup2 returns 0 on success.
8666 
8667     /* Character files like console cannot be make non-inheritable */
8668     if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8669         close(fd2);
8670         return -1;
8671     }
8672 
8673 #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8674     Py_BEGIN_ALLOW_THREADS
8675     if (!inheritable)
8676         res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8677     else
8678         res = dup2(fd, fd2);
8679     Py_END_ALLOW_THREADS
8680     if (res < 0) {
8681         posix_error();
8682         return -1;
8683     }
8684 
8685 #else
8686 
8687 #ifdef HAVE_DUP3
8688     if (!inheritable && dup3_works != 0) {
8689         Py_BEGIN_ALLOW_THREADS
8690         res = dup3(fd, fd2, O_CLOEXEC);
8691         Py_END_ALLOW_THREADS
8692         if (res < 0) {
8693             if (dup3_works == -1)
8694                 dup3_works = (errno != ENOSYS);
8695             if (dup3_works) {
8696                 posix_error();
8697                 return -1;
8698             }
8699         }
8700     }
8701 
8702     if (inheritable || dup3_works == 0)
8703     {
8704 #endif
8705         Py_BEGIN_ALLOW_THREADS
8706         res = dup2(fd, fd2);
8707         Py_END_ALLOW_THREADS
8708         if (res < 0) {
8709             posix_error();
8710             return -1;
8711         }
8712 
8713         if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8714             close(fd2);
8715             return -1;
8716         }
8717 #ifdef HAVE_DUP3
8718     }
8719 #endif
8720 
8721 #endif
8722 
8723     return res;
8724 }
8725 
8726 
8727 #ifdef HAVE_LOCKF
8728 /*[clinic input]
8729 os.lockf
8730 
8731     fd: int
8732         An open file descriptor.
8733     command: int
8734         One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8735     length: Py_off_t
8736         The number of bytes to lock, starting at the current position.
8737     /
8738 
8739 Apply, test or remove a POSIX lock on an open file descriptor.
8740 
8741 [clinic start generated code]*/
8742 
8743 static PyObject *
os_lockf_impl(PyObject * module,int fd,int command,Py_off_t length)8744 os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8745 /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
8746 {
8747     int res;
8748 
8749     if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
8750         return NULL;
8751     }
8752 
8753     Py_BEGIN_ALLOW_THREADS
8754     res = lockf(fd, command, length);
8755     Py_END_ALLOW_THREADS
8756 
8757     if (res < 0)
8758         return posix_error();
8759 
8760     Py_RETURN_NONE;
8761 }
8762 #endif /* HAVE_LOCKF */
8763 
8764 
8765 /*[clinic input]
8766 os.lseek -> Py_off_t
8767 
8768     fd: int
8769     position: Py_off_t
8770     how: int
8771     /
8772 
8773 Set the position of a file descriptor.  Return the new position.
8774 
8775 Return the new cursor position in number of bytes
8776 relative to the beginning of the file.
8777 [clinic start generated code]*/
8778 
8779 static Py_off_t
os_lseek_impl(PyObject * module,int fd,Py_off_t position,int how)8780 os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8781 /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
8782 {
8783     Py_off_t result;
8784 
8785 #ifdef SEEK_SET
8786     /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8787     switch (how) {
8788         case 0: how = SEEK_SET; break;
8789         case 1: how = SEEK_CUR; break;
8790         case 2: how = SEEK_END; break;
8791     }
8792 #endif /* SEEK_END */
8793 
8794     Py_BEGIN_ALLOW_THREADS
8795     _Py_BEGIN_SUPPRESS_IPH
8796 #ifdef MS_WINDOWS
8797     result = _lseeki64(fd, position, how);
8798 #else
8799     result = lseek(fd, position, how);
8800 #endif
8801     _Py_END_SUPPRESS_IPH
8802     Py_END_ALLOW_THREADS
8803     if (result < 0)
8804         posix_error();
8805 
8806     return result;
8807 }
8808 
8809 
8810 /*[clinic input]
8811 os.read
8812     fd: int
8813     length: Py_ssize_t
8814     /
8815 
8816 Read from a file descriptor.  Returns a bytes object.
8817 [clinic start generated code]*/
8818 
8819 static PyObject *
os_read_impl(PyObject * module,int fd,Py_ssize_t length)8820 os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8821 /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
8822 {
8823     Py_ssize_t n;
8824     PyObject *buffer;
8825 
8826     if (length < 0) {
8827         errno = EINVAL;
8828         return posix_error();
8829     }
8830 
8831     length = Py_MIN(length, _PY_READ_MAX);
8832 
8833     buffer = PyBytes_FromStringAndSize((char *)NULL, length);
8834     if (buffer == NULL)
8835         return NULL;
8836 
8837     n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8838     if (n == -1) {
8839         Py_DECREF(buffer);
8840         return NULL;
8841     }
8842 
8843     if (n != length)
8844         _PyBytes_Resize(&buffer, n);
8845 
8846     return buffer;
8847 }
8848 
8849 #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
8850                                 || defined(__APPLE__))) \
8851     || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8852     || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8853 static int
iov_setup(struct iovec ** iov,Py_buffer ** buf,PyObject * seq,Py_ssize_t cnt,int type)8854 iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
8855 {
8856     Py_ssize_t i, j;
8857 
8858     *iov = PyMem_New(struct iovec, cnt);
8859     if (*iov == NULL) {
8860         PyErr_NoMemory();
8861         return -1;
8862     }
8863 
8864     *buf = PyMem_New(Py_buffer, cnt);
8865     if (*buf == NULL) {
8866         PyMem_Del(*iov);
8867         PyErr_NoMemory();
8868         return -1;
8869     }
8870 
8871     for (i = 0; i < cnt; i++) {
8872         PyObject *item = PySequence_GetItem(seq, i);
8873         if (item == NULL)
8874             goto fail;
8875         if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8876             Py_DECREF(item);
8877             goto fail;
8878         }
8879         Py_DECREF(item);
8880         (*iov)[i].iov_base = (*buf)[i].buf;
8881         (*iov)[i].iov_len = (*buf)[i].len;
8882     }
8883     return 0;
8884 
8885 fail:
8886     PyMem_Del(*iov);
8887     for (j = 0; j < i; j++) {
8888         PyBuffer_Release(&(*buf)[j]);
8889     }
8890     PyMem_Del(*buf);
8891     return -1;
8892 }
8893 
8894 static void
iov_cleanup(struct iovec * iov,Py_buffer * buf,int cnt)8895 iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8896 {
8897     int i;
8898     PyMem_Del(iov);
8899     for (i = 0; i < cnt; i++) {
8900         PyBuffer_Release(&buf[i]);
8901     }
8902     PyMem_Del(buf);
8903 }
8904 #endif
8905 
8906 
8907 #ifdef HAVE_READV
8908 /*[clinic input]
8909 os.readv -> Py_ssize_t
8910 
8911     fd: int
8912     buffers: object
8913     /
8914 
8915 Read from a file descriptor fd into an iterable of buffers.
8916 
8917 The buffers should be mutable buffers accepting bytes.
8918 readv will transfer data into each buffer until it is full
8919 and then move on to the next buffer in the sequence to hold
8920 the rest of the data.
8921 
8922 readv returns the total number of bytes read,
8923 which may be less than the total capacity of all the buffers.
8924 [clinic start generated code]*/
8925 
8926 static Py_ssize_t
os_readv_impl(PyObject * module,int fd,PyObject * buffers)8927 os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8928 /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
8929 {
8930     Py_ssize_t cnt, n;
8931     int async_err = 0;
8932     struct iovec *iov;
8933     Py_buffer *buf;
8934 
8935     if (!PySequence_Check(buffers)) {
8936         PyErr_SetString(PyExc_TypeError,
8937             "readv() arg 2 must be a sequence");
8938         return -1;
8939     }
8940 
8941     cnt = PySequence_Size(buffers);
8942     if (cnt < 0)
8943         return -1;
8944 
8945     if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8946         return -1;
8947 
8948     do {
8949         Py_BEGIN_ALLOW_THREADS
8950         n = readv(fd, iov, cnt);
8951         Py_END_ALLOW_THREADS
8952     } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8953 
8954     iov_cleanup(iov, buf, cnt);
8955     if (n < 0) {
8956         if (!async_err)
8957             posix_error();
8958         return -1;
8959     }
8960 
8961     return n;
8962 }
8963 #endif /* HAVE_READV */
8964 
8965 
8966 #ifdef HAVE_PREAD
8967 /*[clinic input]
8968 # TODO length should be size_t!  but Python doesn't support parsing size_t yet.
8969 os.pread
8970 
8971     fd: int
8972     length: int
8973     offset: Py_off_t
8974     /
8975 
8976 Read a number of bytes from a file descriptor starting at a particular offset.
8977 
8978 Read length bytes from file descriptor fd, starting at offset bytes from
8979 the beginning of the file.  The file offset remains unchanged.
8980 [clinic start generated code]*/
8981 
8982 static PyObject *
os_pread_impl(PyObject * module,int fd,int length,Py_off_t offset)8983 os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8984 /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
8985 {
8986     Py_ssize_t n;
8987     int async_err = 0;
8988     PyObject *buffer;
8989 
8990     if (length < 0) {
8991         errno = EINVAL;
8992         return posix_error();
8993     }
8994     buffer = PyBytes_FromStringAndSize((char *)NULL, length);
8995     if (buffer == NULL)
8996         return NULL;
8997 
8998     do {
8999         Py_BEGIN_ALLOW_THREADS
9000         _Py_BEGIN_SUPPRESS_IPH
9001         n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
9002         _Py_END_SUPPRESS_IPH
9003         Py_END_ALLOW_THREADS
9004     } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9005 
9006     if (n < 0) {
9007         Py_DECREF(buffer);
9008         return (!async_err) ? posix_error() : NULL;
9009     }
9010     if (n != length)
9011         _PyBytes_Resize(&buffer, n);
9012     return buffer;
9013 }
9014 #endif /* HAVE_PREAD */
9015 
9016 #if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
9017 /*[clinic input]
9018 os.preadv -> Py_ssize_t
9019 
9020     fd: int
9021     buffers: object
9022     offset: Py_off_t
9023     flags: int = 0
9024     /
9025 
9026 Reads from a file descriptor into a number of mutable bytes-like objects.
9027 
9028 Combines the functionality of readv() and pread(). As readv(), it will
9029 transfer data into each buffer until it is full and then move on to the next
9030 buffer in the sequence to hold the rest of the data. Its fourth argument,
9031 specifies the file offset at which the input operation is to be performed. It
9032 will return the total number of bytes read (which can be less than the total
9033 capacity of all the objects).
9034 
9035 The flags argument contains a bitwise OR of zero or more of the following flags:
9036 
9037 - RWF_HIPRI
9038 - RWF_NOWAIT
9039 
9040 Using non-zero flags requires Linux 4.6 or newer.
9041 [clinic start generated code]*/
9042 
9043 static Py_ssize_t
os_preadv_impl(PyObject * module,int fd,PyObject * buffers,Py_off_t offset,int flags)9044 os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9045                int flags)
9046 /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9047 {
9048     Py_ssize_t cnt, n;
9049     int async_err = 0;
9050     struct iovec *iov;
9051     Py_buffer *buf;
9052 
9053     if (!PySequence_Check(buffers)) {
9054         PyErr_SetString(PyExc_TypeError,
9055             "preadv2() arg 2 must be a sequence");
9056         return -1;
9057     }
9058 
9059     cnt = PySequence_Size(buffers);
9060     if (cnt < 0) {
9061         return -1;
9062     }
9063 
9064 #ifndef HAVE_PREADV2
9065     if(flags != 0) {
9066         argument_unavailable_error("preadv2", "flags");
9067         return -1;
9068     }
9069 #endif
9070 
9071     if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9072         return -1;
9073     }
9074 #ifdef HAVE_PREADV2
9075     do {
9076         Py_BEGIN_ALLOW_THREADS
9077         _Py_BEGIN_SUPPRESS_IPH
9078         n = preadv2(fd, iov, cnt, offset, flags);
9079         _Py_END_SUPPRESS_IPH
9080         Py_END_ALLOW_THREADS
9081     } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9082 #else
9083     do {
9084         Py_BEGIN_ALLOW_THREADS
9085         _Py_BEGIN_SUPPRESS_IPH
9086         n = preadv(fd, iov, cnt, offset);
9087         _Py_END_SUPPRESS_IPH
9088         Py_END_ALLOW_THREADS
9089     } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9090 #endif
9091 
9092     iov_cleanup(iov, buf, cnt);
9093     if (n < 0) {
9094         if (!async_err) {
9095             posix_error();
9096         }
9097         return -1;
9098     }
9099 
9100     return n;
9101 }
9102 #endif /* HAVE_PREADV */
9103 
9104 
9105 /*[clinic input]
9106 os.write -> Py_ssize_t
9107 
9108     fd: int
9109     data: Py_buffer
9110     /
9111 
9112 Write a bytes object to a file descriptor.
9113 [clinic start generated code]*/
9114 
9115 static Py_ssize_t
os_write_impl(PyObject * module,int fd,Py_buffer * data)9116 os_write_impl(PyObject *module, int fd, Py_buffer *data)
9117 /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
9118 {
9119     return _Py_write(fd, data->buf, data->len);
9120 }
9121 
9122 #ifdef HAVE_SENDFILE
9123 PyDoc_STRVAR(posix_sendfile__doc__,
9124 "sendfile(out, in, offset, count) -> byteswritten\n\
9125 sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
9126             -> byteswritten\n\
9127 Copy count bytes from file descriptor in to file descriptor out.");
9128 
9129 /* AC 3.5: don't bother converting, has optional group*/
9130 static PyObject *
posix_sendfile(PyObject * self,PyObject * args,PyObject * kwdict)9131 posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
9132 {
9133     int in, out;
9134     Py_ssize_t ret;
9135     int async_err = 0;
9136     off_t offset;
9137 
9138 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9139 #ifndef __APPLE__
9140     Py_ssize_t len;
9141 #endif
9142     PyObject *headers = NULL, *trailers = NULL;
9143     Py_buffer *hbuf, *tbuf;
9144     off_t sbytes;
9145     struct sf_hdtr sf;
9146     int flags = 0;
9147     /* Beware that "in" clashes with Python's own "in" operator keyword */
9148     static char *keywords[] = {"out", "in",
9149                                 "offset", "count",
9150                                 "headers", "trailers", "flags", NULL};
9151 
9152     sf.headers = NULL;
9153     sf.trailers = NULL;
9154 
9155 #ifdef __APPLE__
9156     if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
9157         keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
9158 #else
9159     if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
9160         keywords, &out, &in, Py_off_t_converter, &offset, &len,
9161 #endif
9162                 &headers, &trailers, &flags))
9163             return NULL;
9164     if (headers != NULL) {
9165         if (!PySequence_Check(headers)) {
9166             PyErr_SetString(PyExc_TypeError,
9167                 "sendfile() headers must be a sequence");
9168             return NULL;
9169         } else {
9170             Py_ssize_t i = PySequence_Size(headers);
9171             if (i < 0)
9172                 return NULL;
9173             if (i > INT_MAX) {
9174                 PyErr_SetString(PyExc_OverflowError,
9175                     "sendfile() header is too large");
9176                 return NULL;
9177             }
9178             if (i > 0) {
9179                 sf.hdr_cnt = (int)i;
9180                 if (iov_setup(&(sf.headers), &hbuf,
9181                               headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
9182                     return NULL;
9183 #ifdef __APPLE__
9184                 for (i = 0; i < sf.hdr_cnt; i++) {
9185                     Py_ssize_t blen = sf.headers[i].iov_len;
9186 # define OFF_T_MAX 0x7fffffffffffffff
9187                     if (sbytes >= OFF_T_MAX - blen) {
9188                         PyErr_SetString(PyExc_OverflowError,
9189                             "sendfile() header is too large");
9190                         return NULL;
9191                     }
9192                     sbytes += blen;
9193                 }
9194 #endif
9195             }
9196         }
9197     }
9198     if (trailers != NULL) {
9199         if (!PySequence_Check(trailers)) {
9200             PyErr_SetString(PyExc_TypeError,
9201                 "sendfile() trailers must be a sequence");
9202             return NULL;
9203         } else {
9204             Py_ssize_t i = PySequence_Size(trailers);
9205             if (i < 0)
9206                 return NULL;
9207             if (i > INT_MAX) {
9208                 PyErr_SetString(PyExc_OverflowError,
9209                     "sendfile() trailer is too large");
9210                 return NULL;
9211             }
9212             if (i > 0) {
9213                 sf.trl_cnt = (int)i;
9214                 if (iov_setup(&(sf.trailers), &tbuf,
9215                               trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
9216                     return NULL;
9217             }
9218         }
9219     }
9220 
9221     _Py_BEGIN_SUPPRESS_IPH
9222     do {
9223         Py_BEGIN_ALLOW_THREADS
9224 #ifdef __APPLE__
9225         ret = sendfile(in, out, offset, &sbytes, &sf, flags);
9226 #else
9227         ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
9228 #endif
9229         Py_END_ALLOW_THREADS
9230     } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9231     _Py_END_SUPPRESS_IPH
9232 
9233     if (sf.headers != NULL)
9234         iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9235     if (sf.trailers != NULL)
9236         iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9237 
9238     if (ret < 0) {
9239         if ((errno == EAGAIN) || (errno == EBUSY)) {
9240             if (sbytes != 0) {
9241                 // some data has been sent
9242                 goto done;
9243             }
9244             else {
9245                 // no data has been sent; upper application is supposed
9246                 // to retry on EAGAIN or EBUSY
9247                 return posix_error();
9248             }
9249         }
9250         return (!async_err) ? posix_error() : NULL;
9251     }
9252     goto done;
9253 
9254 done:
9255     #if !defined(HAVE_LARGEFILE_SUPPORT)
9256         return Py_BuildValue("l", sbytes);
9257     #else
9258         return Py_BuildValue("L", sbytes);
9259     #endif
9260 
9261 #else
9262     Py_ssize_t count;
9263     PyObject *offobj;
9264     static char *keywords[] = {"out", "in",
9265                                 "offset", "count", NULL};
9266     if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9267             keywords, &out, &in, &offobj, &count))
9268         return NULL;
9269 #ifdef __linux__
9270     if (offobj == Py_None) {
9271         do {
9272             Py_BEGIN_ALLOW_THREADS
9273             ret = sendfile(out, in, NULL, count);
9274             Py_END_ALLOW_THREADS
9275         } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9276         if (ret < 0)
9277             return (!async_err) ? posix_error() : NULL;
9278         return Py_BuildValue("n", ret);
9279     }
9280 #endif
9281     if (!Py_off_t_converter(offobj, &offset))
9282         return NULL;
9283 
9284     do {
9285         Py_BEGIN_ALLOW_THREADS
9286         ret = sendfile(out, in, &offset, count);
9287         Py_END_ALLOW_THREADS
9288     } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9289     if (ret < 0)
9290         return (!async_err) ? posix_error() : NULL;
9291     return Py_BuildValue("n", ret);
9292 #endif
9293 }
9294 #endif /* HAVE_SENDFILE */
9295 
9296 
9297 #if defined(__APPLE__)
9298 /*[clinic input]
9299 os._fcopyfile
9300 
9301     infd: int
9302     outfd: int
9303     flags: int
9304     /
9305 
9306 Efficiently copy content or metadata of 2 regular file descriptors (macOS).
9307 [clinic start generated code]*/
9308 
9309 static PyObject *
os__fcopyfile_impl(PyObject * module,int infd,int outfd,int flags)9310 os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
9311 /*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
9312 {
9313     int ret;
9314 
9315     Py_BEGIN_ALLOW_THREADS
9316     ret = fcopyfile(infd, outfd, NULL, flags);
9317     Py_END_ALLOW_THREADS
9318     if (ret < 0)
9319         return posix_error();
9320     Py_RETURN_NONE;
9321 }
9322 #endif
9323 
9324 
9325 /*[clinic input]
9326 os.fstat
9327 
9328     fd : int
9329 
9330 Perform a stat system call on the given file descriptor.
9331 
9332 Like stat(), but for an open file descriptor.
9333 Equivalent to os.stat(fd).
9334 [clinic start generated code]*/
9335 
9336 static PyObject *
os_fstat_impl(PyObject * module,int fd)9337 os_fstat_impl(PyObject *module, int fd)
9338 /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
9339 {
9340     STRUCT_STAT st;
9341     int res;
9342     int async_err = 0;
9343 
9344     do {
9345         Py_BEGIN_ALLOW_THREADS
9346         res = FSTAT(fd, &st);
9347         Py_END_ALLOW_THREADS
9348     } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9349     if (res != 0) {
9350 #ifdef MS_WINDOWS
9351         return PyErr_SetFromWindowsErr(0);
9352 #else
9353         return (!async_err) ? posix_error() : NULL;
9354 #endif
9355     }
9356 
9357     return _pystat_fromstructstat(&st);
9358 }
9359 
9360 
9361 /*[clinic input]
9362 os.isatty -> bool
9363     fd: int
9364     /
9365 
9366 Return True if the fd is connected to a terminal.
9367 
9368 Return True if the file descriptor is an open file descriptor
9369 connected to the slave end of a terminal.
9370 [clinic start generated code]*/
9371 
9372 static int
os_isatty_impl(PyObject * module,int fd)9373 os_isatty_impl(PyObject *module, int fd)
9374 /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
9375 {
9376     int return_value;
9377     _Py_BEGIN_SUPPRESS_IPH
9378     return_value = isatty(fd);
9379     _Py_END_SUPPRESS_IPH
9380     return return_value;
9381 }
9382 
9383 
9384 #ifdef HAVE_PIPE
9385 /*[clinic input]
9386 os.pipe
9387 
9388 Create a pipe.
9389 
9390 Returns a tuple of two file descriptors:
9391   (read_fd, write_fd)
9392 [clinic start generated code]*/
9393 
9394 static PyObject *
os_pipe_impl(PyObject * module)9395 os_pipe_impl(PyObject *module)
9396 /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
9397 {
9398     int fds[2];
9399 #ifdef MS_WINDOWS
9400     HANDLE read, write;
9401     SECURITY_ATTRIBUTES attr;
9402     BOOL ok;
9403 #else
9404     int res;
9405 #endif
9406 
9407 #ifdef MS_WINDOWS
9408     attr.nLength = sizeof(attr);
9409     attr.lpSecurityDescriptor = NULL;
9410     attr.bInheritHandle = FALSE;
9411 
9412     Py_BEGIN_ALLOW_THREADS
9413     _Py_BEGIN_SUPPRESS_IPH
9414     ok = CreatePipe(&read, &write, &attr, 0);
9415     if (ok) {
9416         fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9417         fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
9418         if (fds[0] == -1 || fds[1] == -1) {
9419             CloseHandle(read);
9420             CloseHandle(write);
9421             ok = 0;
9422         }
9423     }
9424     _Py_END_SUPPRESS_IPH
9425     Py_END_ALLOW_THREADS
9426 
9427     if (!ok)
9428         return PyErr_SetFromWindowsErr(0);
9429 #else
9430 
9431 #ifdef HAVE_PIPE2
9432     Py_BEGIN_ALLOW_THREADS
9433     res = pipe2(fds, O_CLOEXEC);
9434     Py_END_ALLOW_THREADS
9435 
9436     if (res != 0 && errno == ENOSYS)
9437     {
9438 #endif
9439         Py_BEGIN_ALLOW_THREADS
9440         res = pipe(fds);
9441         Py_END_ALLOW_THREADS
9442 
9443         if (res == 0) {
9444             if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9445                 close(fds[0]);
9446                 close(fds[1]);
9447                 return NULL;
9448             }
9449             if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9450                 close(fds[0]);
9451                 close(fds[1]);
9452                 return NULL;
9453             }
9454         }
9455 #ifdef HAVE_PIPE2
9456     }
9457 #endif
9458 
9459     if (res != 0)
9460         return PyErr_SetFromErrno(PyExc_OSError);
9461 #endif /* !MS_WINDOWS */
9462     return Py_BuildValue("(ii)", fds[0], fds[1]);
9463 }
9464 #endif  /* HAVE_PIPE */
9465 
9466 
9467 #ifdef HAVE_PIPE2
9468 /*[clinic input]
9469 os.pipe2
9470 
9471     flags: int
9472     /
9473 
9474 Create a pipe with flags set atomically.
9475 
9476 Returns a tuple of two file descriptors:
9477   (read_fd, write_fd)
9478 
9479 flags can be constructed by ORing together one or more of these values:
9480 O_NONBLOCK, O_CLOEXEC.
9481 [clinic start generated code]*/
9482 
9483 static PyObject *
os_pipe2_impl(PyObject * module,int flags)9484 os_pipe2_impl(PyObject *module, int flags)
9485 /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
9486 {
9487     int fds[2];
9488     int res;
9489 
9490     res = pipe2(fds, flags);
9491     if (res != 0)
9492         return posix_error();
9493     return Py_BuildValue("(ii)", fds[0], fds[1]);
9494 }
9495 #endif /* HAVE_PIPE2 */
9496 
9497 
9498 #ifdef HAVE_WRITEV
9499 /*[clinic input]
9500 os.writev -> Py_ssize_t
9501     fd: int
9502     buffers: object
9503     /
9504 
9505 Iterate over buffers, and write the contents of each to a file descriptor.
9506 
9507 Returns the total number of bytes written.
9508 buffers must be a sequence of bytes-like objects.
9509 [clinic start generated code]*/
9510 
9511 static Py_ssize_t
os_writev_impl(PyObject * module,int fd,PyObject * buffers)9512 os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9513 /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
9514 {
9515     Py_ssize_t cnt;
9516     Py_ssize_t result;
9517     int async_err = 0;
9518     struct iovec *iov;
9519     Py_buffer *buf;
9520 
9521     if (!PySequence_Check(buffers)) {
9522         PyErr_SetString(PyExc_TypeError,
9523             "writev() arg 2 must be a sequence");
9524         return -1;
9525     }
9526     cnt = PySequence_Size(buffers);
9527     if (cnt < 0)
9528         return -1;
9529 
9530     if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9531         return -1;
9532     }
9533 
9534     do {
9535         Py_BEGIN_ALLOW_THREADS
9536         result = writev(fd, iov, cnt);
9537         Py_END_ALLOW_THREADS
9538     } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9539 
9540     iov_cleanup(iov, buf, cnt);
9541     if (result < 0 && !async_err)
9542         posix_error();
9543 
9544     return result;
9545 }
9546 #endif /* HAVE_WRITEV */
9547 
9548 
9549 #ifdef HAVE_PWRITE
9550 /*[clinic input]
9551 os.pwrite -> Py_ssize_t
9552 
9553     fd: int
9554     buffer: Py_buffer
9555     offset: Py_off_t
9556     /
9557 
9558 Write bytes to a file descriptor starting at a particular offset.
9559 
9560 Write buffer to fd, starting at offset bytes from the beginning of
9561 the file.  Returns the number of bytes writte.  Does not change the
9562 current file offset.
9563 [clinic start generated code]*/
9564 
9565 static Py_ssize_t
os_pwrite_impl(PyObject * module,int fd,Py_buffer * buffer,Py_off_t offset)9566 os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9567 /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
9568 {
9569     Py_ssize_t size;
9570     int async_err = 0;
9571 
9572     do {
9573         Py_BEGIN_ALLOW_THREADS
9574         _Py_BEGIN_SUPPRESS_IPH
9575         size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
9576         _Py_END_SUPPRESS_IPH
9577         Py_END_ALLOW_THREADS
9578     } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9579 
9580     if (size < 0 && !async_err)
9581         posix_error();
9582     return size;
9583 }
9584 #endif /* HAVE_PWRITE */
9585 
9586 #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9587 /*[clinic input]
9588 os.pwritev -> Py_ssize_t
9589 
9590     fd: int
9591     buffers: object
9592     offset: Py_off_t
9593     flags: int = 0
9594     /
9595 
9596 Writes the contents of bytes-like objects to a file descriptor at a given offset.
9597 
9598 Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9599 of bytes-like objects. Buffers are processed in array order. Entire contents of first
9600 buffer is written before proceeding to second, and so on. The operating system may
9601 set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9602 This function writes the contents of each object to the file descriptor and returns
9603 the total number of bytes written.
9604 
9605 The flags argument contains a bitwise OR of zero or more of the following flags:
9606 
9607 - RWF_DSYNC
9608 - RWF_SYNC
9609 
9610 Using non-zero flags requires Linux 4.7 or newer.
9611 [clinic start generated code]*/
9612 
9613 static Py_ssize_t
os_pwritev_impl(PyObject * module,int fd,PyObject * buffers,Py_off_t offset,int flags)9614 os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9615                 int flags)
9616 /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9617 {
9618     Py_ssize_t cnt;
9619     Py_ssize_t result;
9620     int async_err = 0;
9621     struct iovec *iov;
9622     Py_buffer *buf;
9623 
9624     if (!PySequence_Check(buffers)) {
9625         PyErr_SetString(PyExc_TypeError,
9626             "pwritev() arg 2 must be a sequence");
9627         return -1;
9628     }
9629 
9630     cnt = PySequence_Size(buffers);
9631     if (cnt < 0) {
9632         return -1;
9633     }
9634 
9635 #ifndef HAVE_PWRITEV2
9636     if(flags != 0) {
9637         argument_unavailable_error("pwritev2", "flags");
9638         return -1;
9639     }
9640 #endif
9641 
9642     if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9643         return -1;
9644     }
9645 #ifdef HAVE_PWRITEV2
9646     do {
9647         Py_BEGIN_ALLOW_THREADS
9648         _Py_BEGIN_SUPPRESS_IPH
9649         result = pwritev2(fd, iov, cnt, offset, flags);
9650         _Py_END_SUPPRESS_IPH
9651         Py_END_ALLOW_THREADS
9652     } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9653 #else
9654     do {
9655         Py_BEGIN_ALLOW_THREADS
9656         _Py_BEGIN_SUPPRESS_IPH
9657         result = pwritev(fd, iov, cnt, offset);
9658         _Py_END_SUPPRESS_IPH
9659         Py_END_ALLOW_THREADS
9660     } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9661 #endif
9662 
9663     iov_cleanup(iov, buf, cnt);
9664     if (result < 0) {
9665         if (!async_err) {
9666             posix_error();
9667         }
9668         return -1;
9669     }
9670 
9671     return result;
9672 }
9673 #endif /* HAVE_PWRITEV */
9674 
9675 #ifdef HAVE_COPY_FILE_RANGE
9676 /*[clinic input]
9677 
9678 os.copy_file_range
9679     src: int
9680         Source file descriptor.
9681     dst: int
9682         Destination file descriptor.
9683     count: Py_ssize_t
9684         Number of bytes to copy.
9685     offset_src: object = None
9686         Starting offset in src.
9687     offset_dst: object = None
9688         Starting offset in dst.
9689 
9690 Copy count bytes from one file descriptor to another.
9691 
9692 If offset_src is None, then src is read from the current position;
9693 respectively for offset_dst.
9694 [clinic start generated code]*/
9695 
9696 static PyObject *
os_copy_file_range_impl(PyObject * module,int src,int dst,Py_ssize_t count,PyObject * offset_src,PyObject * offset_dst)9697 os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9698                         PyObject *offset_src, PyObject *offset_dst)
9699 /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9700 {
9701     off_t offset_src_val, offset_dst_val;
9702     off_t *p_offset_src = NULL;
9703     off_t *p_offset_dst = NULL;
9704     Py_ssize_t ret;
9705     int async_err = 0;
9706     /* The flags argument is provided to allow
9707      * for future extensions and currently must be to 0. */
9708     int flags = 0;
9709 
9710 
9711     if (count < 0) {
9712         PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9713         return NULL;
9714     }
9715 
9716     if (offset_src != Py_None) {
9717         if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9718             return NULL;
9719         }
9720         p_offset_src = &offset_src_val;
9721     }
9722 
9723     if (offset_dst != Py_None) {
9724         if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9725             return NULL;
9726         }
9727         p_offset_dst = &offset_dst_val;
9728     }
9729 
9730     do {
9731         Py_BEGIN_ALLOW_THREADS
9732         ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9733         Py_END_ALLOW_THREADS
9734     } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9735 
9736     if (ret < 0) {
9737         return (!async_err) ? posix_error() : NULL;
9738     }
9739 
9740     return PyLong_FromSsize_t(ret);
9741 }
9742 #endif /* HAVE_COPY_FILE_RANGE*/
9743 
9744 #ifdef HAVE_MKFIFO
9745 /*[clinic input]
9746 os.mkfifo
9747 
9748     path: path_t
9749     mode: int=0o666
9750     *
9751     dir_fd: dir_fd(requires='mkfifoat')=None
9752 
9753 Create a "fifo" (a POSIX named pipe).
9754 
9755 If dir_fd is not None, it should be a file descriptor open to a directory,
9756   and path should be relative; path will then be relative to that directory.
9757 dir_fd may not be implemented on your platform.
9758   If it is unavailable, using it will raise a NotImplementedError.
9759 [clinic start generated code]*/
9760 
9761 static PyObject *
os_mkfifo_impl(PyObject * module,path_t * path,int mode,int dir_fd)9762 os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9763 /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
9764 {
9765     int result;
9766     int async_err = 0;
9767 
9768     do {
9769         Py_BEGIN_ALLOW_THREADS
9770 #ifdef HAVE_MKFIFOAT
9771         if (dir_fd != DEFAULT_DIR_FD)
9772             result = mkfifoat(dir_fd, path->narrow, mode);
9773         else
9774 #endif
9775             result = mkfifo(path->narrow, mode);
9776         Py_END_ALLOW_THREADS
9777     } while (result != 0 && errno == EINTR &&
9778              !(async_err = PyErr_CheckSignals()));
9779     if (result != 0)
9780         return (!async_err) ? posix_error() : NULL;
9781 
9782     Py_RETURN_NONE;
9783 }
9784 #endif /* HAVE_MKFIFO */
9785 
9786 
9787 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9788 /*[clinic input]
9789 os.mknod
9790 
9791     path: path_t
9792     mode: int=0o600
9793     device: dev_t=0
9794     *
9795     dir_fd: dir_fd(requires='mknodat')=None
9796 
9797 Create a node in the file system.
9798 
9799 Create a node in the file system (file, device special file or named pipe)
9800 at path.  mode specifies both the permissions to use and the
9801 type of node to be created, being combined (bitwise OR) with one of
9802 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO.  If S_IFCHR or S_IFBLK is set on mode,
9803 device defines the newly created device special file (probably using
9804 os.makedev()).  Otherwise device is ignored.
9805 
9806 If dir_fd is not None, it should be a file descriptor open to a directory,
9807   and path should be relative; path will then be relative to that directory.
9808 dir_fd may not be implemented on your platform.
9809   If it is unavailable, using it will raise a NotImplementedError.
9810 [clinic start generated code]*/
9811 
9812 static PyObject *
os_mknod_impl(PyObject * module,path_t * path,int mode,dev_t device,int dir_fd)9813 os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
9814               int dir_fd)
9815 /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
9816 {
9817     int result;
9818     int async_err = 0;
9819 
9820     do {
9821         Py_BEGIN_ALLOW_THREADS
9822 #ifdef HAVE_MKNODAT
9823         if (dir_fd != DEFAULT_DIR_FD)
9824             result = mknodat(dir_fd, path->narrow, mode, device);
9825         else
9826 #endif
9827             result = mknod(path->narrow, mode, device);
9828         Py_END_ALLOW_THREADS
9829     } while (result != 0 && errno == EINTR &&
9830              !(async_err = PyErr_CheckSignals()));
9831     if (result != 0)
9832         return (!async_err) ? posix_error() : NULL;
9833 
9834     Py_RETURN_NONE;
9835 }
9836 #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9837 
9838 
9839 #ifdef HAVE_DEVICE_MACROS
9840 /*[clinic input]
9841 os.major -> unsigned_int
9842 
9843     device: dev_t
9844     /
9845 
9846 Extracts a device major number from a raw device number.
9847 [clinic start generated code]*/
9848 
9849 static unsigned int
os_major_impl(PyObject * module,dev_t device)9850 os_major_impl(PyObject *module, dev_t device)
9851 /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
9852 {
9853     return major(device);
9854 }
9855 
9856 
9857 /*[clinic input]
9858 os.minor -> unsigned_int
9859 
9860     device: dev_t
9861     /
9862 
9863 Extracts a device minor number from a raw device number.
9864 [clinic start generated code]*/
9865 
9866 static unsigned int
os_minor_impl(PyObject * module,dev_t device)9867 os_minor_impl(PyObject *module, dev_t device)
9868 /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
9869 {
9870     return minor(device);
9871 }
9872 
9873 
9874 /*[clinic input]
9875 os.makedev -> dev_t
9876 
9877     major: int
9878     minor: int
9879     /
9880 
9881 Composes a raw device number from the major and minor device numbers.
9882 [clinic start generated code]*/
9883 
9884 static dev_t
os_makedev_impl(PyObject * module,int major,int minor)9885 os_makedev_impl(PyObject *module, int major, int minor)
9886 /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
9887 {
9888     return makedev(major, minor);
9889 }
9890 #endif /* HAVE_DEVICE_MACROS */
9891 
9892 
9893 #if defined HAVE_FTRUNCATE || defined MS_WINDOWS
9894 /*[clinic input]
9895 os.ftruncate
9896 
9897     fd: int
9898     length: Py_off_t
9899     /
9900 
9901 Truncate a file, specified by file descriptor, to a specific length.
9902 [clinic start generated code]*/
9903 
9904 static PyObject *
os_ftruncate_impl(PyObject * module,int fd,Py_off_t length)9905 os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9906 /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
9907 {
9908     int result;
9909     int async_err = 0;
9910 
9911     if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9912         return NULL;
9913     }
9914 
9915     do {
9916         Py_BEGIN_ALLOW_THREADS
9917         _Py_BEGIN_SUPPRESS_IPH
9918 #ifdef MS_WINDOWS
9919         result = _chsize_s(fd, length);
9920 #else
9921         result = ftruncate(fd, length);
9922 #endif
9923         _Py_END_SUPPRESS_IPH
9924         Py_END_ALLOW_THREADS
9925     } while (result != 0 && errno == EINTR &&
9926              !(async_err = PyErr_CheckSignals()));
9927     if (result != 0)
9928         return (!async_err) ? posix_error() : NULL;
9929     Py_RETURN_NONE;
9930 }
9931 #endif /* HAVE_FTRUNCATE || MS_WINDOWS */
9932 
9933 
9934 #if defined HAVE_TRUNCATE || defined MS_WINDOWS
9935 /*[clinic input]
9936 os.truncate
9937     path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9938     length: Py_off_t
9939 
9940 Truncate a file, specified by path, to a specific length.
9941 
9942 On some platforms, path may also be specified as an open file descriptor.
9943   If this functionality is unavailable, using it raises an exception.
9944 [clinic start generated code]*/
9945 
9946 static PyObject *
os_truncate_impl(PyObject * module,path_t * path,Py_off_t length)9947 os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9948 /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
9949 {
9950     int result;
9951 #ifdef MS_WINDOWS
9952     int fd;
9953 #endif
9954 
9955     if (path->fd != -1)
9956         return os_ftruncate_impl(module, path->fd, length);
9957 
9958     if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9959         return NULL;
9960     }
9961 
9962     Py_BEGIN_ALLOW_THREADS
9963     _Py_BEGIN_SUPPRESS_IPH
9964 #ifdef MS_WINDOWS
9965     fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
9966     if (fd < 0)
9967         result = -1;
9968     else {
9969         result = _chsize_s(fd, length);
9970         close(fd);
9971         if (result < 0)
9972             errno = result;
9973     }
9974 #else
9975     result = truncate(path->narrow, length);
9976 #endif
9977     _Py_END_SUPPRESS_IPH
9978     Py_END_ALLOW_THREADS
9979     if (result < 0)
9980         return posix_path_error(path);
9981 
9982     Py_RETURN_NONE;
9983 }
9984 #endif /* HAVE_TRUNCATE || MS_WINDOWS */
9985 
9986 
9987 /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9988    and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9989    defined, which is the case in Python on AIX. AIX bug report:
9990    http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9991 #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9992 #  define POSIX_FADVISE_AIX_BUG
9993 #endif
9994 
9995 
9996 #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
9997 /*[clinic input]
9998 os.posix_fallocate
9999 
10000     fd: int
10001     offset: Py_off_t
10002     length: Py_off_t
10003     /
10004 
10005 Ensure a file has allocated at least a particular number of bytes on disk.
10006 
10007 Ensure that the file specified by fd encompasses a range of bytes
10008 starting at offset bytes from the beginning and continuing for length bytes.
10009 [clinic start generated code]*/
10010 
10011 static PyObject *
os_posix_fallocate_impl(PyObject * module,int fd,Py_off_t offset,Py_off_t length)10012 os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
10013                         Py_off_t length)
10014 /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
10015 {
10016     int result;
10017     int async_err = 0;
10018 
10019     do {
10020         Py_BEGIN_ALLOW_THREADS
10021         result = posix_fallocate(fd, offset, length);
10022         Py_END_ALLOW_THREADS
10023     } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10024 
10025     if (result == 0)
10026         Py_RETURN_NONE;
10027 
10028     if (async_err)
10029         return NULL;
10030 
10031     errno = result;
10032     return posix_error();
10033 }
10034 #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
10035 
10036 
10037 #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
10038 /*[clinic input]
10039 os.posix_fadvise
10040 
10041     fd: int
10042     offset: Py_off_t
10043     length: Py_off_t
10044     advice: int
10045     /
10046 
10047 Announce an intention to access data in a specific pattern.
10048 
10049 Announce an intention to access data in a specific pattern, thus allowing
10050 the kernel to make optimizations.
10051 The advice applies to the region of the file specified by fd starting at
10052 offset and continuing for length bytes.
10053 advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10054 POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10055 POSIX_FADV_DONTNEED.
10056 [clinic start generated code]*/
10057 
10058 static PyObject *
os_posix_fadvise_impl(PyObject * module,int fd,Py_off_t offset,Py_off_t length,int advice)10059 os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
10060                       Py_off_t length, int advice)
10061 /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
10062 {
10063     int result;
10064     int async_err = 0;
10065 
10066     do {
10067         Py_BEGIN_ALLOW_THREADS
10068         result = posix_fadvise(fd, offset, length, advice);
10069         Py_END_ALLOW_THREADS
10070     } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10071 
10072     if (result == 0)
10073         Py_RETURN_NONE;
10074 
10075     if (async_err)
10076         return NULL;
10077 
10078     errno = result;
10079     return posix_error();
10080 }
10081 #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
10082 
10083 #ifdef HAVE_PUTENV
10084 
10085 /* Save putenv() parameters as values here, so we can collect them when they
10086  * get re-set with another call for the same key. */
10087 static PyObject *posix_putenv_garbage;
10088 
10089 static void
posix_putenv_garbage_setitem(PyObject * name,PyObject * value)10090 posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
10091 {
10092     /* Install the first arg and newstr in posix_putenv_garbage;
10093      * this will cause previous value to be collected.  This has to
10094      * happen after the real putenv() call because the old value
10095      * was still accessible until then. */
10096     if (PyDict_SetItem(posix_putenv_garbage, name, value))
10097         /* really not much we can do; just leak */
10098         PyErr_Clear();
10099     else
10100         Py_DECREF(value);
10101 }
10102 
10103 
10104 #ifdef MS_WINDOWS
10105 /*[clinic input]
10106 os.putenv
10107 
10108     name: unicode
10109     value: unicode
10110     /
10111 
10112 Change or add an environment variable.
10113 [clinic start generated code]*/
10114 
10115 static PyObject *
os_putenv_impl(PyObject * module,PyObject * name,PyObject * value)10116 os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10117 /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
10118 {
10119     const wchar_t *env;
10120     Py_ssize_t size;
10121 
10122     /* Search from index 1 because on Windows starting '=' is allowed for
10123        defining hidden environment variables. */
10124     if (PyUnicode_GET_LENGTH(name) == 0 ||
10125         PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10126     {
10127         PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10128         return NULL;
10129     }
10130     PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
10131     if (unicode == NULL) {
10132         return NULL;
10133     }
10134 
10135     env = PyUnicode_AsUnicodeAndSize(unicode, &size);
10136     if (env == NULL)
10137         goto error;
10138     if (size > _MAX_ENV) {
10139         PyErr_Format(PyExc_ValueError,
10140                      "the environment variable is longer than %u characters",
10141                      _MAX_ENV);
10142         goto error;
10143     }
10144     if (wcslen(env) != (size_t)size) {
10145         PyErr_SetString(PyExc_ValueError, "embedded null character");
10146         goto error;
10147     }
10148 
10149     if (_wputenv(env)) {
10150         posix_error();
10151         goto error;
10152     }
10153 
10154     posix_putenv_garbage_setitem(name, unicode);
10155     Py_RETURN_NONE;
10156 
10157 error:
10158     Py_DECREF(unicode);
10159     return NULL;
10160 }
10161 #else /* MS_WINDOWS */
10162 /*[clinic input]
10163 os.putenv
10164 
10165     name: FSConverter
10166     value: FSConverter
10167     /
10168 
10169 Change or add an environment variable.
10170 [clinic start generated code]*/
10171 
10172 static PyObject *
os_putenv_impl(PyObject * module,PyObject * name,PyObject * value)10173 os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10174 /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
10175 {
10176     PyObject *bytes = NULL;
10177     char *env;
10178     const char *name_string = PyBytes_AS_STRING(name);
10179     const char *value_string = PyBytes_AS_STRING(value);
10180 
10181     if (strchr(name_string, '=') != NULL) {
10182         PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10183         return NULL;
10184     }
10185     if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10186         return NULL;
10187     }
10188     bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
10189     if (bytes == NULL) {
10190         return NULL;
10191     }
10192 
10193     env = PyBytes_AS_STRING(bytes);
10194     if (putenv(env)) {
10195         Py_DECREF(bytes);
10196         return posix_error();
10197     }
10198 
10199     posix_putenv_garbage_setitem(name, bytes);
10200     Py_RETURN_NONE;
10201 }
10202 #endif /* MS_WINDOWS */
10203 #endif /* HAVE_PUTENV */
10204 
10205 
10206 #ifdef HAVE_UNSETENV
10207 /*[clinic input]
10208 os.unsetenv
10209     name: FSConverter
10210     /
10211 
10212 Delete an environment variable.
10213 [clinic start generated code]*/
10214 
10215 static PyObject *
os_unsetenv_impl(PyObject * module,PyObject * name)10216 os_unsetenv_impl(PyObject *module, PyObject *name)
10217 /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
10218 {
10219 #ifndef HAVE_BROKEN_UNSETENV
10220     int err;
10221 #endif
10222 
10223     if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10224         return NULL;
10225     }
10226 
10227 #ifdef HAVE_BROKEN_UNSETENV
10228     unsetenv(PyBytes_AS_STRING(name));
10229 #else
10230     err = unsetenv(PyBytes_AS_STRING(name));
10231     if (err)
10232         return posix_error();
10233 #endif
10234 
10235     /* Remove the key from posix_putenv_garbage;
10236      * this will cause it to be collected.  This has to
10237      * happen after the real unsetenv() call because the
10238      * old value was still accessible until then.
10239      */
10240     if (PyDict_DelItem(posix_putenv_garbage, name)) {
10241         /* really not much we can do; just leak */
10242         if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10243             return NULL;
10244         }
10245         PyErr_Clear();
10246     }
10247     Py_RETURN_NONE;
10248 }
10249 #endif /* HAVE_UNSETENV */
10250 
10251 
10252 /*[clinic input]
10253 os.strerror
10254 
10255     code: int
10256     /
10257 
10258 Translate an error code to a message string.
10259 [clinic start generated code]*/
10260 
10261 static PyObject *
os_strerror_impl(PyObject * module,int code)10262 os_strerror_impl(PyObject *module, int code)
10263 /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
10264 {
10265     char *message = strerror(code);
10266     if (message == NULL) {
10267         PyErr_SetString(PyExc_ValueError,
10268                         "strerror() argument out of range");
10269         return NULL;
10270     }
10271     return PyUnicode_DecodeLocale(message, "surrogateescape");
10272 }
10273 
10274 
10275 #ifdef HAVE_SYS_WAIT_H
10276 #ifdef WCOREDUMP
10277 /*[clinic input]
10278 os.WCOREDUMP -> bool
10279 
10280     status: int
10281     /
10282 
10283 Return True if the process returning status was dumped to a core file.
10284 [clinic start generated code]*/
10285 
10286 static int
os_WCOREDUMP_impl(PyObject * module,int status)10287 os_WCOREDUMP_impl(PyObject *module, int status)
10288 /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
10289 {
10290     WAIT_TYPE wait_status;
10291     WAIT_STATUS_INT(wait_status) = status;
10292     return WCOREDUMP(wait_status);
10293 }
10294 #endif /* WCOREDUMP */
10295 
10296 
10297 #ifdef WIFCONTINUED
10298 /*[clinic input]
10299 os.WIFCONTINUED -> bool
10300 
10301     status: int
10302 
10303 Return True if a particular process was continued from a job control stop.
10304 
10305 Return True if the process returning status was continued from a
10306 job control stop.
10307 [clinic start generated code]*/
10308 
10309 static int
os_WIFCONTINUED_impl(PyObject * module,int status)10310 os_WIFCONTINUED_impl(PyObject *module, int status)
10311 /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
10312 {
10313     WAIT_TYPE wait_status;
10314     WAIT_STATUS_INT(wait_status) = status;
10315     return WIFCONTINUED(wait_status);
10316 }
10317 #endif /* WIFCONTINUED */
10318 
10319 
10320 #ifdef WIFSTOPPED
10321 /*[clinic input]
10322 os.WIFSTOPPED -> bool
10323 
10324     status: int
10325 
10326 Return True if the process returning status was stopped.
10327 [clinic start generated code]*/
10328 
10329 static int
os_WIFSTOPPED_impl(PyObject * module,int status)10330 os_WIFSTOPPED_impl(PyObject *module, int status)
10331 /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
10332 {
10333     WAIT_TYPE wait_status;
10334     WAIT_STATUS_INT(wait_status) = status;
10335     return WIFSTOPPED(wait_status);
10336 }
10337 #endif /* WIFSTOPPED */
10338 
10339 
10340 #ifdef WIFSIGNALED
10341 /*[clinic input]
10342 os.WIFSIGNALED -> bool
10343 
10344     status: int
10345 
10346 Return True if the process returning status was terminated by a signal.
10347 [clinic start generated code]*/
10348 
10349 static int
os_WIFSIGNALED_impl(PyObject * module,int status)10350 os_WIFSIGNALED_impl(PyObject *module, int status)
10351 /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
10352 {
10353     WAIT_TYPE wait_status;
10354     WAIT_STATUS_INT(wait_status) = status;
10355     return WIFSIGNALED(wait_status);
10356 }
10357 #endif /* WIFSIGNALED */
10358 
10359 
10360 #ifdef WIFEXITED
10361 /*[clinic input]
10362 os.WIFEXITED -> bool
10363 
10364     status: int
10365 
10366 Return True if the process returning status exited via the exit() system call.
10367 [clinic start generated code]*/
10368 
10369 static int
os_WIFEXITED_impl(PyObject * module,int status)10370 os_WIFEXITED_impl(PyObject *module, int status)
10371 /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
10372 {
10373     WAIT_TYPE wait_status;
10374     WAIT_STATUS_INT(wait_status) = status;
10375     return WIFEXITED(wait_status);
10376 }
10377 #endif /* WIFEXITED */
10378 
10379 
10380 #ifdef WEXITSTATUS
10381 /*[clinic input]
10382 os.WEXITSTATUS -> int
10383 
10384     status: int
10385 
10386 Return the process return code from status.
10387 [clinic start generated code]*/
10388 
10389 static int
os_WEXITSTATUS_impl(PyObject * module,int status)10390 os_WEXITSTATUS_impl(PyObject *module, int status)
10391 /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
10392 {
10393     WAIT_TYPE wait_status;
10394     WAIT_STATUS_INT(wait_status) = status;
10395     return WEXITSTATUS(wait_status);
10396 }
10397 #endif /* WEXITSTATUS */
10398 
10399 
10400 #ifdef WTERMSIG
10401 /*[clinic input]
10402 os.WTERMSIG -> int
10403 
10404     status: int
10405 
10406 Return the signal that terminated the process that provided the status value.
10407 [clinic start generated code]*/
10408 
10409 static int
os_WTERMSIG_impl(PyObject * module,int status)10410 os_WTERMSIG_impl(PyObject *module, int status)
10411 /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
10412 {
10413     WAIT_TYPE wait_status;
10414     WAIT_STATUS_INT(wait_status) = status;
10415     return WTERMSIG(wait_status);
10416 }
10417 #endif /* WTERMSIG */
10418 
10419 
10420 #ifdef WSTOPSIG
10421 /*[clinic input]
10422 os.WSTOPSIG -> int
10423 
10424     status: int
10425 
10426 Return the signal that stopped the process that provided the status value.
10427 [clinic start generated code]*/
10428 
10429 static int
os_WSTOPSIG_impl(PyObject * module,int status)10430 os_WSTOPSIG_impl(PyObject *module, int status)
10431 /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
10432 {
10433     WAIT_TYPE wait_status;
10434     WAIT_STATUS_INT(wait_status) = status;
10435     return WSTOPSIG(wait_status);
10436 }
10437 #endif /* WSTOPSIG */
10438 #endif /* HAVE_SYS_WAIT_H */
10439 
10440 
10441 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
10442 #ifdef _SCO_DS
10443 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10444    needed definitions in sys/statvfs.h */
10445 #define _SVID3
10446 #endif
10447 #include <sys/statvfs.h>
10448 
10449 static PyObject*
_pystatvfs_fromstructstatvfs(struct statvfs st)10450 _pystatvfs_fromstructstatvfs(struct statvfs st) {
10451     PyObject *v = PyStructSequence_New(StatVFSResultType);
10452     if (v == NULL)
10453         return NULL;
10454 
10455 #if !defined(HAVE_LARGEFILE_SUPPORT)
10456     PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10457     PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10458     PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10459     PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10460     PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10461     PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10462     PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10463     PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10464     PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10465     PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
10466 #else
10467     PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10468     PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10469     PyStructSequence_SET_ITEM(v, 2,
10470                               PyLong_FromLongLong((long long) st.f_blocks));
10471     PyStructSequence_SET_ITEM(v, 3,
10472                               PyLong_FromLongLong((long long) st.f_bfree));
10473     PyStructSequence_SET_ITEM(v, 4,
10474                               PyLong_FromLongLong((long long) st.f_bavail));
10475     PyStructSequence_SET_ITEM(v, 5,
10476                               PyLong_FromLongLong((long long) st.f_files));
10477     PyStructSequence_SET_ITEM(v, 6,
10478                               PyLong_FromLongLong((long long) st.f_ffree));
10479     PyStructSequence_SET_ITEM(v, 7,
10480                               PyLong_FromLongLong((long long) st.f_favail));
10481     PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10482     PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
10483 #endif
10484 /* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10485  * (issue #32390). */
10486 #if defined(_AIX) && defined(_ALL_SOURCE)
10487     PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10488 #else
10489     PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
10490 #endif
10491     if (PyErr_Occurred()) {
10492         Py_DECREF(v);
10493         return NULL;
10494     }
10495 
10496     return v;
10497 }
10498 
10499 
10500 /*[clinic input]
10501 os.fstatvfs
10502     fd: int
10503     /
10504 
10505 Perform an fstatvfs system call on the given fd.
10506 
10507 Equivalent to statvfs(fd).
10508 [clinic start generated code]*/
10509 
10510 static PyObject *
os_fstatvfs_impl(PyObject * module,int fd)10511 os_fstatvfs_impl(PyObject *module, int fd)
10512 /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
10513 {
10514     int result;
10515     int async_err = 0;
10516     struct statvfs st;
10517 
10518     do {
10519         Py_BEGIN_ALLOW_THREADS
10520         result = fstatvfs(fd, &st);
10521         Py_END_ALLOW_THREADS
10522     } while (result != 0 && errno == EINTR &&
10523              !(async_err = PyErr_CheckSignals()));
10524     if (result != 0)
10525         return (!async_err) ? posix_error() : NULL;
10526 
10527     return _pystatvfs_fromstructstatvfs(st);
10528 }
10529 #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
10530 
10531 
10532 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
10533 #include <sys/statvfs.h>
10534 /*[clinic input]
10535 os.statvfs
10536 
10537     path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10538 
10539 Perform a statvfs system call on the given path.
10540 
10541 path may always be specified as a string.
10542 On some platforms, path may also be specified as an open file descriptor.
10543   If this functionality is unavailable, using it raises an exception.
10544 [clinic start generated code]*/
10545 
10546 static PyObject *
os_statvfs_impl(PyObject * module,path_t * path)10547 os_statvfs_impl(PyObject *module, path_t *path)
10548 /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
10549 {
10550     int result;
10551     struct statvfs st;
10552 
10553     Py_BEGIN_ALLOW_THREADS
10554 #ifdef HAVE_FSTATVFS
10555     if (path->fd != -1) {
10556 #ifdef __APPLE__
10557         /* handle weak-linking on Mac OS X 10.3 */
10558         if (fstatvfs == NULL) {
10559             fd_specified("statvfs", path->fd);
10560             return NULL;
10561         }
10562 #endif
10563         result = fstatvfs(path->fd, &st);
10564     }
10565     else
10566 #endif
10567         result = statvfs(path->narrow, &st);
10568     Py_END_ALLOW_THREADS
10569 
10570     if (result) {
10571         return path_error(path);
10572     }
10573 
10574     return _pystatvfs_fromstructstatvfs(st);
10575 }
10576 #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10577 
10578 
10579 #ifdef MS_WINDOWS
10580 /*[clinic input]
10581 os._getdiskusage
10582 
10583     path: path_t
10584 
10585 Return disk usage statistics about the given path as a (total, free) tuple.
10586 [clinic start generated code]*/
10587 
10588 static PyObject *
os__getdiskusage_impl(PyObject * module,path_t * path)10589 os__getdiskusage_impl(PyObject *module, path_t *path)
10590 /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
10591 {
10592     BOOL retval;
10593     ULARGE_INTEGER _, total, free;
10594     DWORD err = 0;
10595 
10596     Py_BEGIN_ALLOW_THREADS
10597     retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
10598     Py_END_ALLOW_THREADS
10599     if (retval == 0) {
10600         if (GetLastError() == ERROR_DIRECTORY) {
10601             wchar_t *dir_path = NULL;
10602 
10603             dir_path = PyMem_New(wchar_t, path->length + 1);
10604             if (dir_path == NULL) {
10605                 return PyErr_NoMemory();
10606             }
10607 
10608             wcscpy_s(dir_path, path->length + 1, path->wide);
10609 
10610             if (_dirnameW(dir_path) != -1) {
10611                 Py_BEGIN_ALLOW_THREADS
10612                 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10613                 Py_END_ALLOW_THREADS
10614             }
10615             /* Record the last error in case it's modified by PyMem_Free. */
10616             err = GetLastError();
10617             PyMem_Free(dir_path);
10618             if (retval) {
10619                 goto success;
10620             }
10621         }
10622         return PyErr_SetFromWindowsErr(err);
10623     }
10624 
10625 success:
10626     return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10627 }
10628 #endif /* MS_WINDOWS */
10629 
10630 
10631 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10632  * It maps strings representing configuration variable names to
10633  * integer values, allowing those functions to be called with the
10634  * magic names instead of polluting the module's namespace with tons of
10635  * rarely-used constants.  There are three separate tables that use
10636  * these definitions.
10637  *
10638  * This code is always included, even if none of the interfaces that
10639  * need it are included.  The #if hackery needed to avoid it would be
10640  * sufficiently pervasive that it's not worth the loss of readability.
10641  */
10642 struct constdef {
10643     const char *name;
10644     int value;
10645 };
10646 
10647 static int
conv_confname(PyObject * arg,int * valuep,struct constdef * table,size_t tablesize)10648 conv_confname(PyObject *arg, int *valuep, struct constdef *table,
10649               size_t tablesize)
10650 {
10651     if (PyLong_Check(arg)) {
10652         int value = _PyLong_AsInt(arg);
10653         if (value == -1 && PyErr_Occurred())
10654             return 0;
10655         *valuep = value;
10656         return 1;
10657     }
10658     else {
10659         /* look up the value in the table using a binary search */
10660         size_t lo = 0;
10661         size_t mid;
10662         size_t hi = tablesize;
10663         int cmp;
10664         const char *confname;
10665         if (!PyUnicode_Check(arg)) {
10666             PyErr_SetString(PyExc_TypeError,
10667                 "configuration names must be strings or integers");
10668             return 0;
10669         }
10670         confname = PyUnicode_AsUTF8(arg);
10671         if (confname == NULL)
10672             return 0;
10673         while (lo < hi) {
10674             mid = (lo + hi) / 2;
10675             cmp = strcmp(confname, table[mid].name);
10676             if (cmp < 0)
10677                 hi = mid;
10678             else if (cmp > 0)
10679                 lo = mid + 1;
10680             else {
10681                 *valuep = table[mid].value;
10682                 return 1;
10683             }
10684         }
10685         PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10686         return 0;
10687     }
10688 }
10689 
10690 
10691 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10692 static struct constdef  posix_constants_pathconf[] = {
10693 #ifdef _PC_ABI_AIO_XFER_MAX
10694     {"PC_ABI_AIO_XFER_MAX",     _PC_ABI_AIO_XFER_MAX},
10695 #endif
10696 #ifdef _PC_ABI_ASYNC_IO
10697     {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
10698 #endif
10699 #ifdef _PC_ASYNC_IO
10700     {"PC_ASYNC_IO",     _PC_ASYNC_IO},
10701 #endif
10702 #ifdef _PC_CHOWN_RESTRICTED
10703     {"PC_CHOWN_RESTRICTED",     _PC_CHOWN_RESTRICTED},
10704 #endif
10705 #ifdef _PC_FILESIZEBITS
10706     {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
10707 #endif
10708 #ifdef _PC_LAST
10709     {"PC_LAST", _PC_LAST},
10710 #endif
10711 #ifdef _PC_LINK_MAX
10712     {"PC_LINK_MAX",     _PC_LINK_MAX},
10713 #endif
10714 #ifdef _PC_MAX_CANON
10715     {"PC_MAX_CANON",    _PC_MAX_CANON},
10716 #endif
10717 #ifdef _PC_MAX_INPUT
10718     {"PC_MAX_INPUT",    _PC_MAX_INPUT},
10719 #endif
10720 #ifdef _PC_NAME_MAX
10721     {"PC_NAME_MAX",     _PC_NAME_MAX},
10722 #endif
10723 #ifdef _PC_NO_TRUNC
10724     {"PC_NO_TRUNC",     _PC_NO_TRUNC},
10725 #endif
10726 #ifdef _PC_PATH_MAX
10727     {"PC_PATH_MAX",     _PC_PATH_MAX},
10728 #endif
10729 #ifdef _PC_PIPE_BUF
10730     {"PC_PIPE_BUF",     _PC_PIPE_BUF},
10731 #endif
10732 #ifdef _PC_PRIO_IO
10733     {"PC_PRIO_IO",      _PC_PRIO_IO},
10734 #endif
10735 #ifdef _PC_SOCK_MAXBUF
10736     {"PC_SOCK_MAXBUF",  _PC_SOCK_MAXBUF},
10737 #endif
10738 #ifdef _PC_SYNC_IO
10739     {"PC_SYNC_IO",      _PC_SYNC_IO},
10740 #endif
10741 #ifdef _PC_VDISABLE
10742     {"PC_VDISABLE",     _PC_VDISABLE},
10743 #endif
10744 #ifdef _PC_ACL_ENABLED
10745     {"PC_ACL_ENABLED",  _PC_ACL_ENABLED},
10746 #endif
10747 #ifdef _PC_MIN_HOLE_SIZE
10748     {"PC_MIN_HOLE_SIZE",    _PC_MIN_HOLE_SIZE},
10749 #endif
10750 #ifdef _PC_ALLOC_SIZE_MIN
10751     {"PC_ALLOC_SIZE_MIN",   _PC_ALLOC_SIZE_MIN},
10752 #endif
10753 #ifdef _PC_REC_INCR_XFER_SIZE
10754     {"PC_REC_INCR_XFER_SIZE",   _PC_REC_INCR_XFER_SIZE},
10755 #endif
10756 #ifdef _PC_REC_MAX_XFER_SIZE
10757     {"PC_REC_MAX_XFER_SIZE",    _PC_REC_MAX_XFER_SIZE},
10758 #endif
10759 #ifdef _PC_REC_MIN_XFER_SIZE
10760     {"PC_REC_MIN_XFER_SIZE",    _PC_REC_MIN_XFER_SIZE},
10761 #endif
10762 #ifdef _PC_REC_XFER_ALIGN
10763     {"PC_REC_XFER_ALIGN",   _PC_REC_XFER_ALIGN},
10764 #endif
10765 #ifdef _PC_SYMLINK_MAX
10766     {"PC_SYMLINK_MAX",  _PC_SYMLINK_MAX},
10767 #endif
10768 #ifdef _PC_XATTR_ENABLED
10769     {"PC_XATTR_ENABLED",    _PC_XATTR_ENABLED},
10770 #endif
10771 #ifdef _PC_XATTR_EXISTS
10772     {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10773 #endif
10774 #ifdef _PC_TIMESTAMP_RESOLUTION
10775     {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10776 #endif
10777 };
10778 
10779 static int
conv_path_confname(PyObject * arg,int * valuep)10780 conv_path_confname(PyObject *arg, int *valuep)
10781 {
10782     return conv_confname(arg, valuep, posix_constants_pathconf,
10783                          sizeof(posix_constants_pathconf)
10784                            / sizeof(struct constdef));
10785 }
10786 #endif
10787 
10788 
10789 #ifdef HAVE_FPATHCONF
10790 /*[clinic input]
10791 os.fpathconf -> long
10792 
10793     fd: int
10794     name: path_confname
10795     /
10796 
10797 Return the configuration limit name for the file descriptor fd.
10798 
10799 If there is no limit, return -1.
10800 [clinic start generated code]*/
10801 
10802 static long
os_fpathconf_impl(PyObject * module,int fd,int name)10803 os_fpathconf_impl(PyObject *module, int fd, int name)
10804 /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
10805 {
10806     long limit;
10807 
10808     errno = 0;
10809     limit = fpathconf(fd, name);
10810     if (limit == -1 && errno != 0)
10811         posix_error();
10812 
10813     return limit;
10814 }
10815 #endif /* HAVE_FPATHCONF */
10816 
10817 
10818 #ifdef HAVE_PATHCONF
10819 /*[clinic input]
10820 os.pathconf -> long
10821     path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10822     name: path_confname
10823 
10824 Return the configuration limit name for the file or directory path.
10825 
10826 If there is no limit, return -1.
10827 On some platforms, path may also be specified as an open file descriptor.
10828   If this functionality is unavailable, using it raises an exception.
10829 [clinic start generated code]*/
10830 
10831 static long
os_pathconf_impl(PyObject * module,path_t * path,int name)10832 os_pathconf_impl(PyObject *module, path_t *path, int name)
10833 /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
10834 {
10835     long limit;
10836 
10837     errno = 0;
10838 #ifdef HAVE_FPATHCONF
10839     if (path->fd != -1)
10840         limit = fpathconf(path->fd, name);
10841     else
10842 #endif
10843         limit = pathconf(path->narrow, name);
10844     if (limit == -1 && errno != 0) {
10845         if (errno == EINVAL)
10846             /* could be a path or name problem */
10847             posix_error();
10848         else
10849             path_error(path);
10850     }
10851 
10852     return limit;
10853 }
10854 #endif /* HAVE_PATHCONF */
10855 
10856 #ifdef HAVE_CONFSTR
10857 static struct constdef posix_constants_confstr[] = {
10858 #ifdef _CS_ARCHITECTURE
10859     {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
10860 #endif
10861 #ifdef _CS_GNU_LIBC_VERSION
10862     {"CS_GNU_LIBC_VERSION",     _CS_GNU_LIBC_VERSION},
10863 #endif
10864 #ifdef _CS_GNU_LIBPTHREAD_VERSION
10865     {"CS_GNU_LIBPTHREAD_VERSION",       _CS_GNU_LIBPTHREAD_VERSION},
10866 #endif
10867 #ifdef _CS_HOSTNAME
10868     {"CS_HOSTNAME",     _CS_HOSTNAME},
10869 #endif
10870 #ifdef _CS_HW_PROVIDER
10871     {"CS_HW_PROVIDER",  _CS_HW_PROVIDER},
10872 #endif
10873 #ifdef _CS_HW_SERIAL
10874     {"CS_HW_SERIAL",    _CS_HW_SERIAL},
10875 #endif
10876 #ifdef _CS_INITTAB_NAME
10877     {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
10878 #endif
10879 #ifdef _CS_LFS64_CFLAGS
10880     {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
10881 #endif
10882 #ifdef _CS_LFS64_LDFLAGS
10883     {"CS_LFS64_LDFLAGS",        _CS_LFS64_LDFLAGS},
10884 #endif
10885 #ifdef _CS_LFS64_LIBS
10886     {"CS_LFS64_LIBS",   _CS_LFS64_LIBS},
10887 #endif
10888 #ifdef _CS_LFS64_LINTFLAGS
10889     {"CS_LFS64_LINTFLAGS",      _CS_LFS64_LINTFLAGS},
10890 #endif
10891 #ifdef _CS_LFS_CFLAGS
10892     {"CS_LFS_CFLAGS",   _CS_LFS_CFLAGS},
10893 #endif
10894 #ifdef _CS_LFS_LDFLAGS
10895     {"CS_LFS_LDFLAGS",  _CS_LFS_LDFLAGS},
10896 #endif
10897 #ifdef _CS_LFS_LIBS
10898     {"CS_LFS_LIBS",     _CS_LFS_LIBS},
10899 #endif
10900 #ifdef _CS_LFS_LINTFLAGS
10901     {"CS_LFS_LINTFLAGS",        _CS_LFS_LINTFLAGS},
10902 #endif
10903 #ifdef _CS_MACHINE
10904     {"CS_MACHINE",      _CS_MACHINE},
10905 #endif
10906 #ifdef _CS_PATH
10907     {"CS_PATH", _CS_PATH},
10908 #endif
10909 #ifdef _CS_RELEASE
10910     {"CS_RELEASE",      _CS_RELEASE},
10911 #endif
10912 #ifdef _CS_SRPC_DOMAIN
10913     {"CS_SRPC_DOMAIN",  _CS_SRPC_DOMAIN},
10914 #endif
10915 #ifdef _CS_SYSNAME
10916     {"CS_SYSNAME",      _CS_SYSNAME},
10917 #endif
10918 #ifdef _CS_VERSION
10919     {"CS_VERSION",      _CS_VERSION},
10920 #endif
10921 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
10922     {"CS_XBS5_ILP32_OFF32_CFLAGS",      _CS_XBS5_ILP32_OFF32_CFLAGS},
10923 #endif
10924 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
10925     {"CS_XBS5_ILP32_OFF32_LDFLAGS",     _CS_XBS5_ILP32_OFF32_LDFLAGS},
10926 #endif
10927 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
10928     {"CS_XBS5_ILP32_OFF32_LIBS",        _CS_XBS5_ILP32_OFF32_LIBS},
10929 #endif
10930 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
10931     {"CS_XBS5_ILP32_OFF32_LINTFLAGS",   _CS_XBS5_ILP32_OFF32_LINTFLAGS},
10932 #endif
10933 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
10934     {"CS_XBS5_ILP32_OFFBIG_CFLAGS",     _CS_XBS5_ILP32_OFFBIG_CFLAGS},
10935 #endif
10936 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
10937     {"CS_XBS5_ILP32_OFFBIG_LDFLAGS",    _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
10938 #endif
10939 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
10940     {"CS_XBS5_ILP32_OFFBIG_LIBS",       _CS_XBS5_ILP32_OFFBIG_LIBS},
10941 #endif
10942 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
10943     {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS",  _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
10944 #endif
10945 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
10946     {"CS_XBS5_LP64_OFF64_CFLAGS",       _CS_XBS5_LP64_OFF64_CFLAGS},
10947 #endif
10948 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
10949     {"CS_XBS5_LP64_OFF64_LDFLAGS",      _CS_XBS5_LP64_OFF64_LDFLAGS},
10950 #endif
10951 #ifdef _CS_XBS5_LP64_OFF64_LIBS
10952     {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
10953 #endif
10954 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
10955     {"CS_XBS5_LP64_OFF64_LINTFLAGS",    _CS_XBS5_LP64_OFF64_LINTFLAGS},
10956 #endif
10957 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
10958     {"CS_XBS5_LPBIG_OFFBIG_CFLAGS",     _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
10959 #endif
10960 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
10961     {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS",    _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
10962 #endif
10963 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
10964     {"CS_XBS5_LPBIG_OFFBIG_LIBS",       _CS_XBS5_LPBIG_OFFBIG_LIBS},
10965 #endif
10966 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
10967     {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS",  _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
10968 #endif
10969 #ifdef _MIPS_CS_AVAIL_PROCESSORS
10970     {"MIPS_CS_AVAIL_PROCESSORS",        _MIPS_CS_AVAIL_PROCESSORS},
10971 #endif
10972 #ifdef _MIPS_CS_BASE
10973     {"MIPS_CS_BASE",    _MIPS_CS_BASE},
10974 #endif
10975 #ifdef _MIPS_CS_HOSTID
10976     {"MIPS_CS_HOSTID",  _MIPS_CS_HOSTID},
10977 #endif
10978 #ifdef _MIPS_CS_HW_NAME
10979     {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
10980 #endif
10981 #ifdef _MIPS_CS_NUM_PROCESSORS
10982     {"MIPS_CS_NUM_PROCESSORS",  _MIPS_CS_NUM_PROCESSORS},
10983 #endif
10984 #ifdef _MIPS_CS_OSREL_MAJ
10985     {"MIPS_CS_OSREL_MAJ",       _MIPS_CS_OSREL_MAJ},
10986 #endif
10987 #ifdef _MIPS_CS_OSREL_MIN
10988     {"MIPS_CS_OSREL_MIN",       _MIPS_CS_OSREL_MIN},
10989 #endif
10990 #ifdef _MIPS_CS_OSREL_PATCH
10991     {"MIPS_CS_OSREL_PATCH",     _MIPS_CS_OSREL_PATCH},
10992 #endif
10993 #ifdef _MIPS_CS_OS_NAME
10994     {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
10995 #endif
10996 #ifdef _MIPS_CS_OS_PROVIDER
10997     {"MIPS_CS_OS_PROVIDER",     _MIPS_CS_OS_PROVIDER},
10998 #endif
10999 #ifdef _MIPS_CS_PROCESSORS
11000     {"MIPS_CS_PROCESSORS",      _MIPS_CS_PROCESSORS},
11001 #endif
11002 #ifdef _MIPS_CS_SERIAL
11003     {"MIPS_CS_SERIAL",  _MIPS_CS_SERIAL},
11004 #endif
11005 #ifdef _MIPS_CS_VENDOR
11006     {"MIPS_CS_VENDOR",  _MIPS_CS_VENDOR},
11007 #endif
11008 };
11009 
11010 static int
conv_confstr_confname(PyObject * arg,int * valuep)11011 conv_confstr_confname(PyObject *arg, int *valuep)
11012 {
11013     return conv_confname(arg, valuep, posix_constants_confstr,
11014                          sizeof(posix_constants_confstr)
11015                            / sizeof(struct constdef));
11016 }
11017 
11018 
11019 /*[clinic input]
11020 os.confstr
11021 
11022     name: confstr_confname
11023     /
11024 
11025 Return a string-valued system configuration variable.
11026 [clinic start generated code]*/
11027 
11028 static PyObject *
os_confstr_impl(PyObject * module,int name)11029 os_confstr_impl(PyObject *module, int name)
11030 /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
11031 {
11032     PyObject *result = NULL;
11033     char buffer[255];
11034     size_t len;
11035 
11036     errno = 0;
11037     len = confstr(name, buffer, sizeof(buffer));
11038     if (len == 0) {
11039         if (errno) {
11040             posix_error();
11041             return NULL;
11042         }
11043         else {
11044             Py_RETURN_NONE;
11045         }
11046     }
11047 
11048     if (len >= sizeof(buffer)) {
11049         size_t len2;
11050         char *buf = PyMem_Malloc(len);
11051         if (buf == NULL)
11052             return PyErr_NoMemory();
11053         len2 = confstr(name, buf, len);
11054         assert(len == len2);
11055         result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
11056         PyMem_Free(buf);
11057     }
11058     else
11059         result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
11060     return result;
11061 }
11062 #endif /* HAVE_CONFSTR */
11063 
11064 
11065 #ifdef HAVE_SYSCONF
11066 static struct constdef posix_constants_sysconf[] = {
11067 #ifdef _SC_2_CHAR_TERM
11068     {"SC_2_CHAR_TERM",  _SC_2_CHAR_TERM},
11069 #endif
11070 #ifdef _SC_2_C_BIND
11071     {"SC_2_C_BIND",     _SC_2_C_BIND},
11072 #endif
11073 #ifdef _SC_2_C_DEV
11074     {"SC_2_C_DEV",      _SC_2_C_DEV},
11075 #endif
11076 #ifdef _SC_2_C_VERSION
11077     {"SC_2_C_VERSION",  _SC_2_C_VERSION},
11078 #endif
11079 #ifdef _SC_2_FORT_DEV
11080     {"SC_2_FORT_DEV",   _SC_2_FORT_DEV},
11081 #endif
11082 #ifdef _SC_2_FORT_RUN
11083     {"SC_2_FORT_RUN",   _SC_2_FORT_RUN},
11084 #endif
11085 #ifdef _SC_2_LOCALEDEF
11086     {"SC_2_LOCALEDEF",  _SC_2_LOCALEDEF},
11087 #endif
11088 #ifdef _SC_2_SW_DEV
11089     {"SC_2_SW_DEV",     _SC_2_SW_DEV},
11090 #endif
11091 #ifdef _SC_2_UPE
11092     {"SC_2_UPE",        _SC_2_UPE},
11093 #endif
11094 #ifdef _SC_2_VERSION
11095     {"SC_2_VERSION",    _SC_2_VERSION},
11096 #endif
11097 #ifdef _SC_ABI_ASYNCHRONOUS_IO
11098     {"SC_ABI_ASYNCHRONOUS_IO",  _SC_ABI_ASYNCHRONOUS_IO},
11099 #endif
11100 #ifdef _SC_ACL
11101     {"SC_ACL",  _SC_ACL},
11102 #endif
11103 #ifdef _SC_AIO_LISTIO_MAX
11104     {"SC_AIO_LISTIO_MAX",       _SC_AIO_LISTIO_MAX},
11105 #endif
11106 #ifdef _SC_AIO_MAX
11107     {"SC_AIO_MAX",      _SC_AIO_MAX},
11108 #endif
11109 #ifdef _SC_AIO_PRIO_DELTA_MAX
11110     {"SC_AIO_PRIO_DELTA_MAX",   _SC_AIO_PRIO_DELTA_MAX},
11111 #endif
11112 #ifdef _SC_ARG_MAX
11113     {"SC_ARG_MAX",      _SC_ARG_MAX},
11114 #endif
11115 #ifdef _SC_ASYNCHRONOUS_IO
11116     {"SC_ASYNCHRONOUS_IO",      _SC_ASYNCHRONOUS_IO},
11117 #endif
11118 #ifdef _SC_ATEXIT_MAX
11119     {"SC_ATEXIT_MAX",   _SC_ATEXIT_MAX},
11120 #endif
11121 #ifdef _SC_AUDIT
11122     {"SC_AUDIT",        _SC_AUDIT},
11123 #endif
11124 #ifdef _SC_AVPHYS_PAGES
11125     {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
11126 #endif
11127 #ifdef _SC_BC_BASE_MAX
11128     {"SC_BC_BASE_MAX",  _SC_BC_BASE_MAX},
11129 #endif
11130 #ifdef _SC_BC_DIM_MAX
11131     {"SC_BC_DIM_MAX",   _SC_BC_DIM_MAX},
11132 #endif
11133 #ifdef _SC_BC_SCALE_MAX
11134     {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
11135 #endif
11136 #ifdef _SC_BC_STRING_MAX
11137     {"SC_BC_STRING_MAX",        _SC_BC_STRING_MAX},
11138 #endif
11139 #ifdef _SC_CAP
11140     {"SC_CAP",  _SC_CAP},
11141 #endif
11142 #ifdef _SC_CHARCLASS_NAME_MAX
11143     {"SC_CHARCLASS_NAME_MAX",   _SC_CHARCLASS_NAME_MAX},
11144 #endif
11145 #ifdef _SC_CHAR_BIT
11146     {"SC_CHAR_BIT",     _SC_CHAR_BIT},
11147 #endif
11148 #ifdef _SC_CHAR_MAX
11149     {"SC_CHAR_MAX",     _SC_CHAR_MAX},
11150 #endif
11151 #ifdef _SC_CHAR_MIN
11152     {"SC_CHAR_MIN",     _SC_CHAR_MIN},
11153 #endif
11154 #ifdef _SC_CHILD_MAX
11155     {"SC_CHILD_MAX",    _SC_CHILD_MAX},
11156 #endif
11157 #ifdef _SC_CLK_TCK
11158     {"SC_CLK_TCK",      _SC_CLK_TCK},
11159 #endif
11160 #ifdef _SC_COHER_BLKSZ
11161     {"SC_COHER_BLKSZ",  _SC_COHER_BLKSZ},
11162 #endif
11163 #ifdef _SC_COLL_WEIGHTS_MAX
11164     {"SC_COLL_WEIGHTS_MAX",     _SC_COLL_WEIGHTS_MAX},
11165 #endif
11166 #ifdef _SC_DCACHE_ASSOC
11167     {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
11168 #endif
11169 #ifdef _SC_DCACHE_BLKSZ
11170     {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
11171 #endif
11172 #ifdef _SC_DCACHE_LINESZ
11173     {"SC_DCACHE_LINESZ",        _SC_DCACHE_LINESZ},
11174 #endif
11175 #ifdef _SC_DCACHE_SZ
11176     {"SC_DCACHE_SZ",    _SC_DCACHE_SZ},
11177 #endif
11178 #ifdef _SC_DCACHE_TBLKSZ
11179     {"SC_DCACHE_TBLKSZ",        _SC_DCACHE_TBLKSZ},
11180 #endif
11181 #ifdef _SC_DELAYTIMER_MAX
11182     {"SC_DELAYTIMER_MAX",       _SC_DELAYTIMER_MAX},
11183 #endif
11184 #ifdef _SC_EQUIV_CLASS_MAX
11185     {"SC_EQUIV_CLASS_MAX",      _SC_EQUIV_CLASS_MAX},
11186 #endif
11187 #ifdef _SC_EXPR_NEST_MAX
11188     {"SC_EXPR_NEST_MAX",        _SC_EXPR_NEST_MAX},
11189 #endif
11190 #ifdef _SC_FSYNC
11191     {"SC_FSYNC",        _SC_FSYNC},
11192 #endif
11193 #ifdef _SC_GETGR_R_SIZE_MAX
11194     {"SC_GETGR_R_SIZE_MAX",     _SC_GETGR_R_SIZE_MAX},
11195 #endif
11196 #ifdef _SC_GETPW_R_SIZE_MAX
11197     {"SC_GETPW_R_SIZE_MAX",     _SC_GETPW_R_SIZE_MAX},
11198 #endif
11199 #ifdef _SC_ICACHE_ASSOC
11200     {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
11201 #endif
11202 #ifdef _SC_ICACHE_BLKSZ
11203     {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
11204 #endif
11205 #ifdef _SC_ICACHE_LINESZ
11206     {"SC_ICACHE_LINESZ",        _SC_ICACHE_LINESZ},
11207 #endif
11208 #ifdef _SC_ICACHE_SZ
11209     {"SC_ICACHE_SZ",    _SC_ICACHE_SZ},
11210 #endif
11211 #ifdef _SC_INF
11212     {"SC_INF",  _SC_INF},
11213 #endif
11214 #ifdef _SC_INT_MAX
11215     {"SC_INT_MAX",      _SC_INT_MAX},
11216 #endif
11217 #ifdef _SC_INT_MIN
11218     {"SC_INT_MIN",      _SC_INT_MIN},
11219 #endif
11220 #ifdef _SC_IOV_MAX
11221     {"SC_IOV_MAX",      _SC_IOV_MAX},
11222 #endif
11223 #ifdef _SC_IP_SECOPTS
11224     {"SC_IP_SECOPTS",   _SC_IP_SECOPTS},
11225 #endif
11226 #ifdef _SC_JOB_CONTROL
11227     {"SC_JOB_CONTROL",  _SC_JOB_CONTROL},
11228 #endif
11229 #ifdef _SC_KERN_POINTERS
11230     {"SC_KERN_POINTERS",        _SC_KERN_POINTERS},
11231 #endif
11232 #ifdef _SC_KERN_SIM
11233     {"SC_KERN_SIM",     _SC_KERN_SIM},
11234 #endif
11235 #ifdef _SC_LINE_MAX
11236     {"SC_LINE_MAX",     _SC_LINE_MAX},
11237 #endif
11238 #ifdef _SC_LOGIN_NAME_MAX
11239     {"SC_LOGIN_NAME_MAX",       _SC_LOGIN_NAME_MAX},
11240 #endif
11241 #ifdef _SC_LOGNAME_MAX
11242     {"SC_LOGNAME_MAX",  _SC_LOGNAME_MAX},
11243 #endif
11244 #ifdef _SC_LONG_BIT
11245     {"SC_LONG_BIT",     _SC_LONG_BIT},
11246 #endif
11247 #ifdef _SC_MAC
11248     {"SC_MAC",  _SC_MAC},
11249 #endif
11250 #ifdef _SC_MAPPED_FILES
11251     {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
11252 #endif
11253 #ifdef _SC_MAXPID
11254     {"SC_MAXPID",       _SC_MAXPID},
11255 #endif
11256 #ifdef _SC_MB_LEN_MAX
11257     {"SC_MB_LEN_MAX",   _SC_MB_LEN_MAX},
11258 #endif
11259 #ifdef _SC_MEMLOCK
11260     {"SC_MEMLOCK",      _SC_MEMLOCK},
11261 #endif
11262 #ifdef _SC_MEMLOCK_RANGE
11263     {"SC_MEMLOCK_RANGE",        _SC_MEMLOCK_RANGE},
11264 #endif
11265 #ifdef _SC_MEMORY_PROTECTION
11266     {"SC_MEMORY_PROTECTION",    _SC_MEMORY_PROTECTION},
11267 #endif
11268 #ifdef _SC_MESSAGE_PASSING
11269     {"SC_MESSAGE_PASSING",      _SC_MESSAGE_PASSING},
11270 #endif
11271 #ifdef _SC_MMAP_FIXED_ALIGNMENT
11272     {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
11273 #endif
11274 #ifdef _SC_MQ_OPEN_MAX
11275     {"SC_MQ_OPEN_MAX",  _SC_MQ_OPEN_MAX},
11276 #endif
11277 #ifdef _SC_MQ_PRIO_MAX
11278     {"SC_MQ_PRIO_MAX",  _SC_MQ_PRIO_MAX},
11279 #endif
11280 #ifdef _SC_NACLS_MAX
11281     {"SC_NACLS_MAX",    _SC_NACLS_MAX},
11282 #endif
11283 #ifdef _SC_NGROUPS_MAX
11284     {"SC_NGROUPS_MAX",  _SC_NGROUPS_MAX},
11285 #endif
11286 #ifdef _SC_NL_ARGMAX
11287     {"SC_NL_ARGMAX",    _SC_NL_ARGMAX},
11288 #endif
11289 #ifdef _SC_NL_LANGMAX
11290     {"SC_NL_LANGMAX",   _SC_NL_LANGMAX},
11291 #endif
11292 #ifdef _SC_NL_MSGMAX
11293     {"SC_NL_MSGMAX",    _SC_NL_MSGMAX},
11294 #endif
11295 #ifdef _SC_NL_NMAX
11296     {"SC_NL_NMAX",      _SC_NL_NMAX},
11297 #endif
11298 #ifdef _SC_NL_SETMAX
11299     {"SC_NL_SETMAX",    _SC_NL_SETMAX},
11300 #endif
11301 #ifdef _SC_NL_TEXTMAX
11302     {"SC_NL_TEXTMAX",   _SC_NL_TEXTMAX},
11303 #endif
11304 #ifdef _SC_NPROCESSORS_CONF
11305     {"SC_NPROCESSORS_CONF",     _SC_NPROCESSORS_CONF},
11306 #endif
11307 #ifdef _SC_NPROCESSORS_ONLN
11308     {"SC_NPROCESSORS_ONLN",     _SC_NPROCESSORS_ONLN},
11309 #endif
11310 #ifdef _SC_NPROC_CONF
11311     {"SC_NPROC_CONF",   _SC_NPROC_CONF},
11312 #endif
11313 #ifdef _SC_NPROC_ONLN
11314     {"SC_NPROC_ONLN",   _SC_NPROC_ONLN},
11315 #endif
11316 #ifdef _SC_NZERO
11317     {"SC_NZERO",        _SC_NZERO},
11318 #endif
11319 #ifdef _SC_OPEN_MAX
11320     {"SC_OPEN_MAX",     _SC_OPEN_MAX},
11321 #endif
11322 #ifdef _SC_PAGESIZE
11323     {"SC_PAGESIZE",     _SC_PAGESIZE},
11324 #endif
11325 #ifdef _SC_PAGE_SIZE
11326     {"SC_PAGE_SIZE",    _SC_PAGE_SIZE},
11327 #endif
11328 #ifdef _SC_PASS_MAX
11329     {"SC_PASS_MAX",     _SC_PASS_MAX},
11330 #endif
11331 #ifdef _SC_PHYS_PAGES
11332     {"SC_PHYS_PAGES",   _SC_PHYS_PAGES},
11333 #endif
11334 #ifdef _SC_PII
11335     {"SC_PII",  _SC_PII},
11336 #endif
11337 #ifdef _SC_PII_INTERNET
11338     {"SC_PII_INTERNET", _SC_PII_INTERNET},
11339 #endif
11340 #ifdef _SC_PII_INTERNET_DGRAM
11341     {"SC_PII_INTERNET_DGRAM",   _SC_PII_INTERNET_DGRAM},
11342 #endif
11343 #ifdef _SC_PII_INTERNET_STREAM
11344     {"SC_PII_INTERNET_STREAM",  _SC_PII_INTERNET_STREAM},
11345 #endif
11346 #ifdef _SC_PII_OSI
11347     {"SC_PII_OSI",      _SC_PII_OSI},
11348 #endif
11349 #ifdef _SC_PII_OSI_CLTS
11350     {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
11351 #endif
11352 #ifdef _SC_PII_OSI_COTS
11353     {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
11354 #endif
11355 #ifdef _SC_PII_OSI_M
11356     {"SC_PII_OSI_M",    _SC_PII_OSI_M},
11357 #endif
11358 #ifdef _SC_PII_SOCKET
11359     {"SC_PII_SOCKET",   _SC_PII_SOCKET},
11360 #endif
11361 #ifdef _SC_PII_XTI
11362     {"SC_PII_XTI",      _SC_PII_XTI},
11363 #endif
11364 #ifdef _SC_POLL
11365     {"SC_POLL", _SC_POLL},
11366 #endif
11367 #ifdef _SC_PRIORITIZED_IO
11368     {"SC_PRIORITIZED_IO",       _SC_PRIORITIZED_IO},
11369 #endif
11370 #ifdef _SC_PRIORITY_SCHEDULING
11371     {"SC_PRIORITY_SCHEDULING",  _SC_PRIORITY_SCHEDULING},
11372 #endif
11373 #ifdef _SC_REALTIME_SIGNALS
11374     {"SC_REALTIME_SIGNALS",     _SC_REALTIME_SIGNALS},
11375 #endif
11376 #ifdef _SC_RE_DUP_MAX
11377     {"SC_RE_DUP_MAX",   _SC_RE_DUP_MAX},
11378 #endif
11379 #ifdef _SC_RTSIG_MAX
11380     {"SC_RTSIG_MAX",    _SC_RTSIG_MAX},
11381 #endif
11382 #ifdef _SC_SAVED_IDS
11383     {"SC_SAVED_IDS",    _SC_SAVED_IDS},
11384 #endif
11385 #ifdef _SC_SCHAR_MAX
11386     {"SC_SCHAR_MAX",    _SC_SCHAR_MAX},
11387 #endif
11388 #ifdef _SC_SCHAR_MIN
11389     {"SC_SCHAR_MIN",    _SC_SCHAR_MIN},
11390 #endif
11391 #ifdef _SC_SELECT
11392     {"SC_SELECT",       _SC_SELECT},
11393 #endif
11394 #ifdef _SC_SEMAPHORES
11395     {"SC_SEMAPHORES",   _SC_SEMAPHORES},
11396 #endif
11397 #ifdef _SC_SEM_NSEMS_MAX
11398     {"SC_SEM_NSEMS_MAX",        _SC_SEM_NSEMS_MAX},
11399 #endif
11400 #ifdef _SC_SEM_VALUE_MAX
11401     {"SC_SEM_VALUE_MAX",        _SC_SEM_VALUE_MAX},
11402 #endif
11403 #ifdef _SC_SHARED_MEMORY_OBJECTS
11404     {"SC_SHARED_MEMORY_OBJECTS",        _SC_SHARED_MEMORY_OBJECTS},
11405 #endif
11406 #ifdef _SC_SHRT_MAX
11407     {"SC_SHRT_MAX",     _SC_SHRT_MAX},
11408 #endif
11409 #ifdef _SC_SHRT_MIN
11410     {"SC_SHRT_MIN",     _SC_SHRT_MIN},
11411 #endif
11412 #ifdef _SC_SIGQUEUE_MAX
11413     {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
11414 #endif
11415 #ifdef _SC_SIGRT_MAX
11416     {"SC_SIGRT_MAX",    _SC_SIGRT_MAX},
11417 #endif
11418 #ifdef _SC_SIGRT_MIN
11419     {"SC_SIGRT_MIN",    _SC_SIGRT_MIN},
11420 #endif
11421 #ifdef _SC_SOFTPOWER
11422     {"SC_SOFTPOWER",    _SC_SOFTPOWER},
11423 #endif
11424 #ifdef _SC_SPLIT_CACHE
11425     {"SC_SPLIT_CACHE",  _SC_SPLIT_CACHE},
11426 #endif
11427 #ifdef _SC_SSIZE_MAX
11428     {"SC_SSIZE_MAX",    _SC_SSIZE_MAX},
11429 #endif
11430 #ifdef _SC_STACK_PROT
11431     {"SC_STACK_PROT",   _SC_STACK_PROT},
11432 #endif
11433 #ifdef _SC_STREAM_MAX
11434     {"SC_STREAM_MAX",   _SC_STREAM_MAX},
11435 #endif
11436 #ifdef _SC_SYNCHRONIZED_IO
11437     {"SC_SYNCHRONIZED_IO",      _SC_SYNCHRONIZED_IO},
11438 #endif
11439 #ifdef _SC_THREADS
11440     {"SC_THREADS",      _SC_THREADS},
11441 #endif
11442 #ifdef _SC_THREAD_ATTR_STACKADDR
11443     {"SC_THREAD_ATTR_STACKADDR",        _SC_THREAD_ATTR_STACKADDR},
11444 #endif
11445 #ifdef _SC_THREAD_ATTR_STACKSIZE
11446     {"SC_THREAD_ATTR_STACKSIZE",        _SC_THREAD_ATTR_STACKSIZE},
11447 #endif
11448 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
11449     {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
11450 #endif
11451 #ifdef _SC_THREAD_KEYS_MAX
11452     {"SC_THREAD_KEYS_MAX",      _SC_THREAD_KEYS_MAX},
11453 #endif
11454 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
11455     {"SC_THREAD_PRIORITY_SCHEDULING",   _SC_THREAD_PRIORITY_SCHEDULING},
11456 #endif
11457 #ifdef _SC_THREAD_PRIO_INHERIT
11458     {"SC_THREAD_PRIO_INHERIT",  _SC_THREAD_PRIO_INHERIT},
11459 #endif
11460 #ifdef _SC_THREAD_PRIO_PROTECT
11461     {"SC_THREAD_PRIO_PROTECT",  _SC_THREAD_PRIO_PROTECT},
11462 #endif
11463 #ifdef _SC_THREAD_PROCESS_SHARED
11464     {"SC_THREAD_PROCESS_SHARED",        _SC_THREAD_PROCESS_SHARED},
11465 #endif
11466 #ifdef _SC_THREAD_SAFE_FUNCTIONS
11467     {"SC_THREAD_SAFE_FUNCTIONS",        _SC_THREAD_SAFE_FUNCTIONS},
11468 #endif
11469 #ifdef _SC_THREAD_STACK_MIN
11470     {"SC_THREAD_STACK_MIN",     _SC_THREAD_STACK_MIN},
11471 #endif
11472 #ifdef _SC_THREAD_THREADS_MAX
11473     {"SC_THREAD_THREADS_MAX",   _SC_THREAD_THREADS_MAX},
11474 #endif
11475 #ifdef _SC_TIMERS
11476     {"SC_TIMERS",       _SC_TIMERS},
11477 #endif
11478 #ifdef _SC_TIMER_MAX
11479     {"SC_TIMER_MAX",    _SC_TIMER_MAX},
11480 #endif
11481 #ifdef _SC_TTY_NAME_MAX
11482     {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
11483 #endif
11484 #ifdef _SC_TZNAME_MAX
11485     {"SC_TZNAME_MAX",   _SC_TZNAME_MAX},
11486 #endif
11487 #ifdef _SC_T_IOV_MAX
11488     {"SC_T_IOV_MAX",    _SC_T_IOV_MAX},
11489 #endif
11490 #ifdef _SC_UCHAR_MAX
11491     {"SC_UCHAR_MAX",    _SC_UCHAR_MAX},
11492 #endif
11493 #ifdef _SC_UINT_MAX
11494     {"SC_UINT_MAX",     _SC_UINT_MAX},
11495 #endif
11496 #ifdef _SC_UIO_MAXIOV
11497     {"SC_UIO_MAXIOV",   _SC_UIO_MAXIOV},
11498 #endif
11499 #ifdef _SC_ULONG_MAX
11500     {"SC_ULONG_MAX",    _SC_ULONG_MAX},
11501 #endif
11502 #ifdef _SC_USHRT_MAX
11503     {"SC_USHRT_MAX",    _SC_USHRT_MAX},
11504 #endif
11505 #ifdef _SC_VERSION
11506     {"SC_VERSION",      _SC_VERSION},
11507 #endif
11508 #ifdef _SC_WORD_BIT
11509     {"SC_WORD_BIT",     _SC_WORD_BIT},
11510 #endif
11511 #ifdef _SC_XBS5_ILP32_OFF32
11512     {"SC_XBS5_ILP32_OFF32",     _SC_XBS5_ILP32_OFF32},
11513 #endif
11514 #ifdef _SC_XBS5_ILP32_OFFBIG
11515     {"SC_XBS5_ILP32_OFFBIG",    _SC_XBS5_ILP32_OFFBIG},
11516 #endif
11517 #ifdef _SC_XBS5_LP64_OFF64
11518     {"SC_XBS5_LP64_OFF64",      _SC_XBS5_LP64_OFF64},
11519 #endif
11520 #ifdef _SC_XBS5_LPBIG_OFFBIG
11521     {"SC_XBS5_LPBIG_OFFBIG",    _SC_XBS5_LPBIG_OFFBIG},
11522 #endif
11523 #ifdef _SC_XOPEN_CRYPT
11524     {"SC_XOPEN_CRYPT",  _SC_XOPEN_CRYPT},
11525 #endif
11526 #ifdef _SC_XOPEN_ENH_I18N
11527     {"SC_XOPEN_ENH_I18N",       _SC_XOPEN_ENH_I18N},
11528 #endif
11529 #ifdef _SC_XOPEN_LEGACY
11530     {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
11531 #endif
11532 #ifdef _SC_XOPEN_REALTIME
11533     {"SC_XOPEN_REALTIME",       _SC_XOPEN_REALTIME},
11534 #endif
11535 #ifdef _SC_XOPEN_REALTIME_THREADS
11536     {"SC_XOPEN_REALTIME_THREADS",       _SC_XOPEN_REALTIME_THREADS},
11537 #endif
11538 #ifdef _SC_XOPEN_SHM
11539     {"SC_XOPEN_SHM",    _SC_XOPEN_SHM},
11540 #endif
11541 #ifdef _SC_XOPEN_UNIX
11542     {"SC_XOPEN_UNIX",   _SC_XOPEN_UNIX},
11543 #endif
11544 #ifdef _SC_XOPEN_VERSION
11545     {"SC_XOPEN_VERSION",        _SC_XOPEN_VERSION},
11546 #endif
11547 #ifdef _SC_XOPEN_XCU_VERSION
11548     {"SC_XOPEN_XCU_VERSION",    _SC_XOPEN_XCU_VERSION},
11549 #endif
11550 #ifdef _SC_XOPEN_XPG2
11551     {"SC_XOPEN_XPG2",   _SC_XOPEN_XPG2},
11552 #endif
11553 #ifdef _SC_XOPEN_XPG3
11554     {"SC_XOPEN_XPG3",   _SC_XOPEN_XPG3},
11555 #endif
11556 #ifdef _SC_XOPEN_XPG4
11557     {"SC_XOPEN_XPG4",   _SC_XOPEN_XPG4},
11558 #endif
11559 };
11560 
11561 static int
conv_sysconf_confname(PyObject * arg,int * valuep)11562 conv_sysconf_confname(PyObject *arg, int *valuep)
11563 {
11564     return conv_confname(arg, valuep, posix_constants_sysconf,
11565                          sizeof(posix_constants_sysconf)
11566                            / sizeof(struct constdef));
11567 }
11568 
11569 
11570 /*[clinic input]
11571 os.sysconf -> long
11572     name: sysconf_confname
11573     /
11574 
11575 Return an integer-valued system configuration variable.
11576 [clinic start generated code]*/
11577 
11578 static long
os_sysconf_impl(PyObject * module,int name)11579 os_sysconf_impl(PyObject *module, int name)
11580 /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
11581 {
11582     long value;
11583 
11584     errno = 0;
11585     value = sysconf(name);
11586     if (value == -1 && errno != 0)
11587         posix_error();
11588     return value;
11589 }
11590 #endif /* HAVE_SYSCONF */
11591 
11592 
11593 /* This code is used to ensure that the tables of configuration value names
11594  * are in sorted order as required by conv_confname(), and also to build
11595  * the exported dictionaries that are used to publish information about the
11596  * names available on the host platform.
11597  *
11598  * Sorting the table at runtime ensures that the table is properly ordered
11599  * when used, even for platforms we're not able to test on.  It also makes
11600  * it easier to add additional entries to the tables.
11601  */
11602 
11603 static int
cmp_constdefs(const void * v1,const void * v2)11604 cmp_constdefs(const void *v1,  const void *v2)
11605 {
11606     const struct constdef *c1 =
11607     (const struct constdef *) v1;
11608     const struct constdef *c2 =
11609     (const struct constdef *) v2;
11610 
11611     return strcmp(c1->name, c2->name);
11612 }
11613 
11614 static int
setup_confname_table(struct constdef * table,size_t tablesize,const char * tablename,PyObject * module)11615 setup_confname_table(struct constdef *table, size_t tablesize,
11616                      const char *tablename, PyObject *module)
11617 {
11618     PyObject *d = NULL;
11619     size_t i;
11620 
11621     qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11622     d = PyDict_New();
11623     if (d == NULL)
11624         return -1;
11625 
11626     for (i=0; i < tablesize; ++i) {
11627         PyObject *o = PyLong_FromLong(table[i].value);
11628         if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11629             Py_XDECREF(o);
11630             Py_DECREF(d);
11631             return -1;
11632         }
11633         Py_DECREF(o);
11634     }
11635     return PyModule_AddObject(module, tablename, d);
11636 }
11637 
11638 /* Return -1 on failure, 0 on success. */
11639 static int
setup_confname_tables(PyObject * module)11640 setup_confname_tables(PyObject *module)
11641 {
11642 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
11643     if (setup_confname_table(posix_constants_pathconf,
11644                              sizeof(posix_constants_pathconf)
11645                                / sizeof(struct constdef),
11646                              "pathconf_names", module))
11647         return -1;
11648 #endif
11649 #ifdef HAVE_CONFSTR
11650     if (setup_confname_table(posix_constants_confstr,
11651                              sizeof(posix_constants_confstr)
11652                                / sizeof(struct constdef),
11653                              "confstr_names", module))
11654         return -1;
11655 #endif
11656 #ifdef HAVE_SYSCONF
11657     if (setup_confname_table(posix_constants_sysconf,
11658                              sizeof(posix_constants_sysconf)
11659                                / sizeof(struct constdef),
11660                              "sysconf_names", module))
11661         return -1;
11662 #endif
11663     return 0;
11664 }
11665 
11666 
11667 /*[clinic input]
11668 os.abort
11669 
11670 Abort the interpreter immediately.
11671 
11672 This function 'dumps core' or otherwise fails in the hardest way possible
11673 on the hosting operating system.  This function never returns.
11674 [clinic start generated code]*/
11675 
11676 static PyObject *
os_abort_impl(PyObject * module)11677 os_abort_impl(PyObject *module)
11678 /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
11679 {
11680     abort();
11681     /*NOTREACHED*/
11682 #ifndef __clang__
11683     /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11684        GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11685        is smarter and emits a warning on the return. */
11686     Py_FatalError("abort() called from Python code didn't abort!");
11687     return NULL;
11688 #endif
11689 }
11690 
11691 #ifdef MS_WINDOWS
11692 /* Grab ShellExecute dynamically from shell32 */
11693 static int has_ShellExecute = -1;
11694 static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11695                                               LPCWSTR, INT);
11696 static int
check_ShellExecute()11697 check_ShellExecute()
11698 {
11699     HINSTANCE hShell32;
11700 
11701     /* only recheck */
11702     if (-1 == has_ShellExecute) {
11703         Py_BEGIN_ALLOW_THREADS
11704         /* Security note: this call is not vulnerable to "DLL hijacking".
11705            SHELL32 is part of "KnownDLLs" and so Windows always load
11706            the system SHELL32.DLL, even if there is another SHELL32.DLL
11707            in the DLL search path. */
11708         hShell32 = LoadLibraryW(L"SHELL32");
11709         if (hShell32) {
11710             *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11711                                             "ShellExecuteW");
11712             has_ShellExecute = Py_ShellExecuteW != NULL;
11713         } else {
11714             has_ShellExecute = 0;
11715         }
11716         Py_END_ALLOW_THREADS
11717     }
11718     return has_ShellExecute;
11719 }
11720 
11721 
11722 /*[clinic input]
11723 os.startfile
11724     filepath: path_t
11725     operation: Py_UNICODE = NULL
11726 
11727 Start a file with its associated application.
11728 
11729 When "operation" is not specified or "open", this acts like
11730 double-clicking the file in Explorer, or giving the file name as an
11731 argument to the DOS "start" command: the file is opened with whatever
11732 application (if any) its extension is associated.
11733 When another "operation" is given, it specifies what should be done with
11734 the file.  A typical operation is "print".
11735 
11736 startfile returns as soon as the associated application is launched.
11737 There is no option to wait for the application to close, and no way
11738 to retrieve the application's exit status.
11739 
11740 The filepath is relative to the current directory.  If you want to use
11741 an absolute path, make sure the first character is not a slash ("/");
11742 the underlying Win32 ShellExecute function doesn't work if it is.
11743 [clinic start generated code]*/
11744 
11745 static PyObject *
os_startfile_impl(PyObject * module,path_t * filepath,const Py_UNICODE * operation)11746 os_startfile_impl(PyObject *module, path_t *filepath,
11747                   const Py_UNICODE *operation)
11748 /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
11749 {
11750     HINSTANCE rc;
11751 
11752     if(!check_ShellExecute()) {
11753         /* If the OS doesn't have ShellExecute, return a
11754            NotImplementedError. */
11755         return PyErr_Format(PyExc_NotImplementedError,
11756             "startfile not available on this platform");
11757     }
11758 
11759     if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
11760         return NULL;
11761     }
11762 
11763     Py_BEGIN_ALLOW_THREADS
11764     rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
11765                           NULL, NULL, SW_SHOWNORMAL);
11766     Py_END_ALLOW_THREADS
11767 
11768     if (rc <= (HINSTANCE)32) {
11769         win32_error_object("startfile", filepath->object);
11770         return NULL;
11771     }
11772     Py_RETURN_NONE;
11773 }
11774 #endif /* MS_WINDOWS */
11775 
11776 
11777 #ifdef HAVE_GETLOADAVG
11778 /*[clinic input]
11779 os.getloadavg
11780 
11781 Return average recent system load information.
11782 
11783 Return the number of processes in the system run queue averaged over
11784 the last 1, 5, and 15 minutes as a tuple of three floats.
11785 Raises OSError if the load average was unobtainable.
11786 [clinic start generated code]*/
11787 
11788 static PyObject *
os_getloadavg_impl(PyObject * module)11789 os_getloadavg_impl(PyObject *module)
11790 /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
11791 {
11792     double loadavg[3];
11793     if (getloadavg(loadavg, 3)!=3) {
11794         PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11795         return NULL;
11796     } else
11797         return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
11798 }
11799 #endif /* HAVE_GETLOADAVG */
11800 
11801 
11802 /*[clinic input]
11803 os.device_encoding
11804     fd: int
11805 
11806 Return a string describing the encoding of a terminal's file descriptor.
11807 
11808 The file descriptor must be attached to a terminal.
11809 If the device is not a terminal, return None.
11810 [clinic start generated code]*/
11811 
11812 static PyObject *
os_device_encoding_impl(PyObject * module,int fd)11813 os_device_encoding_impl(PyObject *module, int fd)
11814 /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
11815 {
11816     return _Py_device_encoding(fd);
11817 }
11818 
11819 
11820 #ifdef HAVE_SETRESUID
11821 /*[clinic input]
11822 os.setresuid
11823 
11824     ruid: uid_t
11825     euid: uid_t
11826     suid: uid_t
11827     /
11828 
11829 Set the current process's real, effective, and saved user ids.
11830 [clinic start generated code]*/
11831 
11832 static PyObject *
os_setresuid_impl(PyObject * module,uid_t ruid,uid_t euid,uid_t suid)11833 os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11834 /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
11835 {
11836     if (setresuid(ruid, euid, suid) < 0)
11837         return posix_error();
11838     Py_RETURN_NONE;
11839 }
11840 #endif /* HAVE_SETRESUID */
11841 
11842 
11843 #ifdef HAVE_SETRESGID
11844 /*[clinic input]
11845 os.setresgid
11846 
11847     rgid: gid_t
11848     egid: gid_t
11849     sgid: gid_t
11850     /
11851 
11852 Set the current process's real, effective, and saved group ids.
11853 [clinic start generated code]*/
11854 
11855 static PyObject *
os_setresgid_impl(PyObject * module,gid_t rgid,gid_t egid,gid_t sgid)11856 os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11857 /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
11858 {
11859     if (setresgid(rgid, egid, sgid) < 0)
11860         return posix_error();
11861     Py_RETURN_NONE;
11862 }
11863 #endif /* HAVE_SETRESGID */
11864 
11865 
11866 #ifdef HAVE_GETRESUID
11867 /*[clinic input]
11868 os.getresuid
11869 
11870 Return a tuple of the current process's real, effective, and saved user ids.
11871 [clinic start generated code]*/
11872 
11873 static PyObject *
os_getresuid_impl(PyObject * module)11874 os_getresuid_impl(PyObject *module)
11875 /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
11876 {
11877     uid_t ruid, euid, suid;
11878     if (getresuid(&ruid, &euid, &suid) < 0)
11879         return posix_error();
11880     return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11881                                   _PyLong_FromUid(euid),
11882                                   _PyLong_FromUid(suid));
11883 }
11884 #endif /* HAVE_GETRESUID */
11885 
11886 
11887 #ifdef HAVE_GETRESGID
11888 /*[clinic input]
11889 os.getresgid
11890 
11891 Return a tuple of the current process's real, effective, and saved group ids.
11892 [clinic start generated code]*/
11893 
11894 static PyObject *
os_getresgid_impl(PyObject * module)11895 os_getresgid_impl(PyObject *module)
11896 /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
11897 {
11898     gid_t rgid, egid, sgid;
11899     if (getresgid(&rgid, &egid, &sgid) < 0)
11900         return posix_error();
11901     return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11902                                   _PyLong_FromGid(egid),
11903                                   _PyLong_FromGid(sgid));
11904 }
11905 #endif /* HAVE_GETRESGID */
11906 
11907 
11908 #ifdef USE_XATTRS
11909 /*[clinic input]
11910 os.getxattr
11911 
11912     path: path_t(allow_fd=True)
11913     attribute: path_t
11914     *
11915     follow_symlinks: bool = True
11916 
11917 Return the value of extended attribute attribute on path.
11918 
11919 path may be either a string, a path-like object, or an open file descriptor.
11920 If follow_symlinks is False, and the last element of the path is a symbolic
11921   link, getxattr will examine the symbolic link itself instead of the file
11922   the link points to.
11923 
11924 [clinic start generated code]*/
11925 
11926 static PyObject *
os_getxattr_impl(PyObject * module,path_t * path,path_t * attribute,int follow_symlinks)11927 os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
11928                  int follow_symlinks)
11929 /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
11930 {
11931     Py_ssize_t i;
11932     PyObject *buffer = NULL;
11933 
11934     if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11935         return NULL;
11936 
11937     if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
11938         return NULL;
11939     }
11940 
11941     for (i = 0; ; i++) {
11942         void *ptr;
11943         ssize_t result;
11944         static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
11945         Py_ssize_t buffer_size = buffer_sizes[i];
11946         if (!buffer_size) {
11947             path_error(path);
11948             return NULL;
11949         }
11950         buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11951         if (!buffer)
11952             return NULL;
11953         ptr = PyBytes_AS_STRING(buffer);
11954 
11955         Py_BEGIN_ALLOW_THREADS;
11956         if (path->fd >= 0)
11957             result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
11958         else if (follow_symlinks)
11959             result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
11960         else
11961             result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
11962         Py_END_ALLOW_THREADS;
11963 
11964         if (result < 0) {
11965             Py_DECREF(buffer);
11966             if (errno == ERANGE)
11967                 continue;
11968             path_error(path);
11969             return NULL;
11970         }
11971 
11972         if (result != buffer_size) {
11973             /* Can only shrink. */
11974             _PyBytes_Resize(&buffer, result);
11975         }
11976         break;
11977     }
11978 
11979     return buffer;
11980 }
11981 
11982 
11983 /*[clinic input]
11984 os.setxattr
11985 
11986     path: path_t(allow_fd=True)
11987     attribute: path_t
11988     value: Py_buffer
11989     flags: int = 0
11990     *
11991     follow_symlinks: bool = True
11992 
11993 Set extended attribute attribute on path to value.
11994 
11995 path may be either a string, a path-like object,  or an open file descriptor.
11996 If follow_symlinks is False, and the last element of the path is a symbolic
11997   link, setxattr will modify the symbolic link itself instead of the file
11998   the link points to.
11999 
12000 [clinic start generated code]*/
12001 
12002 static PyObject *
os_setxattr_impl(PyObject * module,path_t * path,path_t * attribute,Py_buffer * value,int flags,int follow_symlinks)12003 os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
12004                  Py_buffer *value, int flags, int follow_symlinks)
12005 /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
12006 {
12007     ssize_t result;
12008 
12009     if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
12010         return NULL;
12011 
12012     if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
12013                     value->buf, value->len, flags) < 0) {
12014         return NULL;
12015     }
12016 
12017     Py_BEGIN_ALLOW_THREADS;
12018     if (path->fd > -1)
12019         result = fsetxattr(path->fd, attribute->narrow,
12020                            value->buf, value->len, flags);
12021     else if (follow_symlinks)
12022         result = setxattr(path->narrow, attribute->narrow,
12023                            value->buf, value->len, flags);
12024     else
12025         result = lsetxattr(path->narrow, attribute->narrow,
12026                            value->buf, value->len, flags);
12027     Py_END_ALLOW_THREADS;
12028 
12029     if (result) {
12030         path_error(path);
12031         return NULL;
12032     }
12033 
12034     Py_RETURN_NONE;
12035 }
12036 
12037 
12038 /*[clinic input]
12039 os.removexattr
12040 
12041     path: path_t(allow_fd=True)
12042     attribute: path_t
12043     *
12044     follow_symlinks: bool = True
12045 
12046 Remove extended attribute attribute on path.
12047 
12048 path may be either a string, a path-like object, or an open file descriptor.
12049 If follow_symlinks is False, and the last element of the path is a symbolic
12050   link, removexattr will modify the symbolic link itself instead of the file
12051   the link points to.
12052 
12053 [clinic start generated code]*/
12054 
12055 static PyObject *
os_removexattr_impl(PyObject * module,path_t * path,path_t * attribute,int follow_symlinks)12056 os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
12057                     int follow_symlinks)
12058 /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
12059 {
12060     ssize_t result;
12061 
12062     if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12063         return NULL;
12064 
12065     if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
12066         return NULL;
12067     }
12068 
12069     Py_BEGIN_ALLOW_THREADS;
12070     if (path->fd > -1)
12071         result = fremovexattr(path->fd, attribute->narrow);
12072     else if (follow_symlinks)
12073         result = removexattr(path->narrow, attribute->narrow);
12074     else
12075         result = lremovexattr(path->narrow, attribute->narrow);
12076     Py_END_ALLOW_THREADS;
12077 
12078     if (result) {
12079         return path_error(path);
12080     }
12081 
12082     Py_RETURN_NONE;
12083 }
12084 
12085 
12086 /*[clinic input]
12087 os.listxattr
12088 
12089     path: path_t(allow_fd=True, nullable=True) = None
12090     *
12091     follow_symlinks: bool = True
12092 
12093 Return a list of extended attributes on path.
12094 
12095 path may be either None, a string, a path-like object, or an open file descriptor.
12096 if path is None, listxattr will examine the current directory.
12097 If follow_symlinks is False, and the last element of the path is a symbolic
12098   link, listxattr will examine the symbolic link itself instead of the file
12099   the link points to.
12100 [clinic start generated code]*/
12101 
12102 static PyObject *
os_listxattr_impl(PyObject * module,path_t * path,int follow_symlinks)12103 os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
12104 /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
12105 {
12106     Py_ssize_t i;
12107     PyObject *result = NULL;
12108     const char *name;
12109     char *buffer = NULL;
12110 
12111     if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
12112         goto exit;
12113 
12114     if (PySys_Audit("os.listxattr", "(O)",
12115                     path->object ? path->object : Py_None) < 0) {
12116         return NULL;
12117     }
12118 
12119     name = path->narrow ? path->narrow : ".";
12120 
12121     for (i = 0; ; i++) {
12122         const char *start, *trace, *end;
12123         ssize_t length;
12124         static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
12125         Py_ssize_t buffer_size = buffer_sizes[i];
12126         if (!buffer_size) {
12127             /* ERANGE */
12128             path_error(path);
12129             break;
12130         }
12131         buffer = PyMem_MALLOC(buffer_size);
12132         if (!buffer) {
12133             PyErr_NoMemory();
12134             break;
12135         }
12136 
12137         Py_BEGIN_ALLOW_THREADS;
12138         if (path->fd > -1)
12139             length = flistxattr(path->fd, buffer, buffer_size);
12140         else if (follow_symlinks)
12141             length = listxattr(name, buffer, buffer_size);
12142         else
12143             length = llistxattr(name, buffer, buffer_size);
12144         Py_END_ALLOW_THREADS;
12145 
12146         if (length < 0) {
12147             if (errno == ERANGE) {
12148                 PyMem_FREE(buffer);
12149                 buffer = NULL;
12150                 continue;
12151             }
12152             path_error(path);
12153             break;
12154         }
12155 
12156         result = PyList_New(0);
12157         if (!result) {
12158             goto exit;
12159         }
12160 
12161         end = buffer + length;
12162         for (trace = start = buffer; trace != end; trace++) {
12163             if (!*trace) {
12164                 int error;
12165                 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12166                                                                  trace - start);
12167                 if (!attribute) {
12168                     Py_DECREF(result);
12169                     result = NULL;
12170                     goto exit;
12171                 }
12172                 error = PyList_Append(result, attribute);
12173                 Py_DECREF(attribute);
12174                 if (error) {
12175                     Py_DECREF(result);
12176                     result = NULL;
12177                     goto exit;
12178                 }
12179                 start = trace + 1;
12180             }
12181         }
12182     break;
12183     }
12184 exit:
12185     if (buffer)
12186         PyMem_FREE(buffer);
12187     return result;
12188 }
12189 #endif /* USE_XATTRS */
12190 
12191 
12192 /*[clinic input]
12193 os.urandom
12194 
12195     size: Py_ssize_t
12196     /
12197 
12198 Return a bytes object containing random bytes suitable for cryptographic use.
12199 [clinic start generated code]*/
12200 
12201 static PyObject *
os_urandom_impl(PyObject * module,Py_ssize_t size)12202 os_urandom_impl(PyObject *module, Py_ssize_t size)
12203 /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
12204 {
12205     PyObject *bytes;
12206     int result;
12207 
12208     if (size < 0)
12209         return PyErr_Format(PyExc_ValueError,
12210                             "negative argument not allowed");
12211     bytes = PyBytes_FromStringAndSize(NULL, size);
12212     if (bytes == NULL)
12213         return NULL;
12214 
12215     result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
12216     if (result == -1) {
12217         Py_DECREF(bytes);
12218         return NULL;
12219     }
12220     return bytes;
12221 }
12222 
12223 #ifdef HAVE_MEMFD_CREATE
12224 /*[clinic input]
12225 os.memfd_create
12226 
12227     name: FSConverter
12228     flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12229 
12230 [clinic start generated code]*/
12231 
12232 static PyObject *
os_memfd_create_impl(PyObject * module,PyObject * name,unsigned int flags)12233 os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12234 /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12235 {
12236     int fd;
12237     const char *bytes = PyBytes_AS_STRING(name);
12238     Py_BEGIN_ALLOW_THREADS
12239     fd = memfd_create(bytes, flags);
12240     Py_END_ALLOW_THREADS
12241     if (fd == -1) {
12242         return PyErr_SetFromErrno(PyExc_OSError);
12243     }
12244     return PyLong_FromLong(fd);
12245 }
12246 #endif
12247 
12248 /* Terminal size querying */
12249 
12250 static PyTypeObject* TerminalSizeType;
12251 
12252 PyDoc_STRVAR(TerminalSize_docstring,
12253     "A tuple of (columns, lines) for holding terminal window size");
12254 
12255 static PyStructSequence_Field TerminalSize_fields[] = {
12256     {"columns", "width of the terminal window in characters"},
12257     {"lines", "height of the terminal window in characters"},
12258     {NULL, NULL}
12259 };
12260 
12261 static PyStructSequence_Desc TerminalSize_desc = {
12262     "os.terminal_size",
12263     TerminalSize_docstring,
12264     TerminalSize_fields,
12265     2,
12266 };
12267 
12268 #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
12269 /* AC 3.5: fd should accept None */
12270 PyDoc_STRVAR(termsize__doc__,
12271     "Return the size of the terminal window as (columns, lines).\n"        \
12272     "\n"                                                                   \
12273     "The optional argument fd (default standard output) specifies\n"       \
12274     "which file descriptor should be queried.\n"                           \
12275     "\n"                                                                   \
12276     "If the file descriptor is not connected to a terminal, an OSError\n"  \
12277     "is thrown.\n"                                                         \
12278     "\n"                                                                   \
12279     "This function will only be defined if an implementation is\n"         \
12280     "available for this system.\n"                                         \
12281     "\n"                                                                   \
12282     "shutil.get_terminal_size is the high-level function which should\n"  \
12283     "normally be used, os.get_terminal_size is the low-level implementation.");
12284 
12285 static PyObject*
get_terminal_size(PyObject * self,PyObject * args)12286 get_terminal_size(PyObject *self, PyObject *args)
12287 {
12288     int columns, lines;
12289     PyObject *termsize;
12290 
12291     int fd = fileno(stdout);
12292     /* Under some conditions stdout may not be connected and
12293      * fileno(stdout) may point to an invalid file descriptor. For example
12294      * GUI apps don't have valid standard streams by default.
12295      *
12296      * If this happens, and the optional fd argument is not present,
12297      * the ioctl below will fail returning EBADF. This is what we want.
12298      */
12299 
12300     if (!PyArg_ParseTuple(args, "|i", &fd))
12301         return NULL;
12302 
12303 #ifdef TERMSIZE_USE_IOCTL
12304     {
12305         struct winsize w;
12306         if (ioctl(fd, TIOCGWINSZ, &w))
12307             return PyErr_SetFromErrno(PyExc_OSError);
12308         columns = w.ws_col;
12309         lines = w.ws_row;
12310     }
12311 #endif /* TERMSIZE_USE_IOCTL */
12312 
12313 #ifdef TERMSIZE_USE_CONIO
12314     {
12315         DWORD nhandle;
12316         HANDLE handle;
12317         CONSOLE_SCREEN_BUFFER_INFO csbi;
12318         switch (fd) {
12319         case 0: nhandle = STD_INPUT_HANDLE;
12320             break;
12321         case 1: nhandle = STD_OUTPUT_HANDLE;
12322             break;
12323         case 2: nhandle = STD_ERROR_HANDLE;
12324             break;
12325         default:
12326             return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12327         }
12328         handle = GetStdHandle(nhandle);
12329         if (handle == NULL)
12330             return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12331         if (handle == INVALID_HANDLE_VALUE)
12332             return PyErr_SetFromWindowsErr(0);
12333 
12334         if (!GetConsoleScreenBufferInfo(handle, &csbi))
12335             return PyErr_SetFromWindowsErr(0);
12336 
12337         columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12338         lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12339     }
12340 #endif /* TERMSIZE_USE_CONIO */
12341 
12342     termsize = PyStructSequence_New(TerminalSizeType);
12343     if (termsize == NULL)
12344         return NULL;
12345     PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12346     PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12347     if (PyErr_Occurred()) {
12348         Py_DECREF(termsize);
12349         return NULL;
12350     }
12351     return termsize;
12352 }
12353 #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12354 
12355 
12356 /*[clinic input]
12357 os.cpu_count
12358 
12359 Return the number of CPUs in the system; return None if indeterminable.
12360 
12361 This number is not equivalent to the number of CPUs the current process can
12362 use.  The number of usable CPUs can be obtained with
12363 ``len(os.sched_getaffinity(0))``
12364 [clinic start generated code]*/
12365 
12366 static PyObject *
os_cpu_count_impl(PyObject * module)12367 os_cpu_count_impl(PyObject *module)
12368 /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
12369 {
12370     int ncpu = 0;
12371 #ifdef MS_WINDOWS
12372     /* Declare prototype here to avoid pulling in all of the Win7 APIs in 3.8 */
12373     DWORD WINAPI GetActiveProcessorCount(WORD group);
12374     ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
12375 #elif defined(__hpux)
12376     ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12377 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12378     ncpu = sysconf(_SC_NPROCESSORS_ONLN);
12379 #elif defined(__DragonFly__) || \
12380       defined(__OpenBSD__)   || \
12381       defined(__FreeBSD__)   || \
12382       defined(__NetBSD__)    || \
12383       defined(__APPLE__)
12384     int mib[2];
12385     size_t len = sizeof(ncpu);
12386     mib[0] = CTL_HW;
12387     mib[1] = HW_NCPU;
12388     if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12389         ncpu = 0;
12390 #endif
12391     if (ncpu >= 1)
12392         return PyLong_FromLong(ncpu);
12393     else
12394         Py_RETURN_NONE;
12395 }
12396 
12397 
12398 /*[clinic input]
12399 os.get_inheritable -> bool
12400 
12401     fd: int
12402     /
12403 
12404 Get the close-on-exe flag of the specified file descriptor.
12405 [clinic start generated code]*/
12406 
12407 static int
os_get_inheritable_impl(PyObject * module,int fd)12408 os_get_inheritable_impl(PyObject *module, int fd)
12409 /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
12410 {
12411     int return_value;
12412     _Py_BEGIN_SUPPRESS_IPH
12413     return_value = _Py_get_inheritable(fd);
12414     _Py_END_SUPPRESS_IPH
12415     return return_value;
12416 }
12417 
12418 
12419 /*[clinic input]
12420 os.set_inheritable
12421     fd: int
12422     inheritable: int
12423     /
12424 
12425 Set the inheritable flag of the specified file descriptor.
12426 [clinic start generated code]*/
12427 
12428 static PyObject *
os_set_inheritable_impl(PyObject * module,int fd,int inheritable)12429 os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12430 /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
12431 {
12432     int result;
12433 
12434     _Py_BEGIN_SUPPRESS_IPH
12435     result = _Py_set_inheritable(fd, inheritable, NULL);
12436     _Py_END_SUPPRESS_IPH
12437     if (result < 0)
12438         return NULL;
12439     Py_RETURN_NONE;
12440 }
12441 
12442 
12443 #ifdef MS_WINDOWS
12444 /*[clinic input]
12445 os.get_handle_inheritable -> bool
12446     handle: intptr_t
12447     /
12448 
12449 Get the close-on-exe flag of the specified file descriptor.
12450 [clinic start generated code]*/
12451 
12452 static int
os_get_handle_inheritable_impl(PyObject * module,intptr_t handle)12453 os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
12454 /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
12455 {
12456     DWORD flags;
12457 
12458     if (!GetHandleInformation((HANDLE)handle, &flags)) {
12459         PyErr_SetFromWindowsErr(0);
12460         return -1;
12461     }
12462 
12463     return flags & HANDLE_FLAG_INHERIT;
12464 }
12465 
12466 
12467 /*[clinic input]
12468 os.set_handle_inheritable
12469     handle: intptr_t
12470     inheritable: bool
12471     /
12472 
12473 Set the inheritable flag of the specified handle.
12474 [clinic start generated code]*/
12475 
12476 static PyObject *
os_set_handle_inheritable_impl(PyObject * module,intptr_t handle,int inheritable)12477 os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
12478                                int inheritable)
12479 /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
12480 {
12481     DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
12482     if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12483         PyErr_SetFromWindowsErr(0);
12484         return NULL;
12485     }
12486     Py_RETURN_NONE;
12487 }
12488 #endif /* MS_WINDOWS */
12489 
12490 #ifndef MS_WINDOWS
12491 /*[clinic input]
12492 os.get_blocking -> bool
12493     fd: int
12494     /
12495 
12496 Get the blocking mode of the file descriptor.
12497 
12498 Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12499 [clinic start generated code]*/
12500 
12501 static int
os_get_blocking_impl(PyObject * module,int fd)12502 os_get_blocking_impl(PyObject *module, int fd)
12503 /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
12504 {
12505     int blocking;
12506 
12507     _Py_BEGIN_SUPPRESS_IPH
12508     blocking = _Py_get_blocking(fd);
12509     _Py_END_SUPPRESS_IPH
12510     return blocking;
12511 }
12512 
12513 /*[clinic input]
12514 os.set_blocking
12515     fd: int
12516     blocking: bool(accept={int})
12517     /
12518 
12519 Set the blocking mode of the specified file descriptor.
12520 
12521 Set the O_NONBLOCK flag if blocking is False,
12522 clear the O_NONBLOCK flag otherwise.
12523 [clinic start generated code]*/
12524 
12525 static PyObject *
os_set_blocking_impl(PyObject * module,int fd,int blocking)12526 os_set_blocking_impl(PyObject *module, int fd, int blocking)
12527 /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
12528 {
12529     int result;
12530 
12531     _Py_BEGIN_SUPPRESS_IPH
12532     result = _Py_set_blocking(fd, blocking);
12533     _Py_END_SUPPRESS_IPH
12534     if (result < 0)
12535         return NULL;
12536     Py_RETURN_NONE;
12537 }
12538 #endif   /* !MS_WINDOWS */
12539 
12540 
12541 /*[clinic input]
12542 class os.DirEntry "DirEntry *" "&DirEntryType"
12543 [clinic start generated code]*/
12544 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
12545 
12546 typedef struct {
12547     PyObject_HEAD
12548     PyObject *name;
12549     PyObject *path;
12550     PyObject *stat;
12551     PyObject *lstat;
12552 #ifdef MS_WINDOWS
12553     struct _Py_stat_struct win32_lstat;
12554     uint64_t win32_file_index;
12555     int got_file_index;
12556 #else /* POSIX */
12557 #ifdef HAVE_DIRENT_D_TYPE
12558     unsigned char d_type;
12559 #endif
12560     ino_t d_ino;
12561     int dir_fd;
12562 #endif
12563 } DirEntry;
12564 
12565 static void
DirEntry_dealloc(DirEntry * entry)12566 DirEntry_dealloc(DirEntry *entry)
12567 {
12568     Py_XDECREF(entry->name);
12569     Py_XDECREF(entry->path);
12570     Py_XDECREF(entry->stat);
12571     Py_XDECREF(entry->lstat);
12572     Py_TYPE(entry)->tp_free((PyObject *)entry);
12573 }
12574 
12575 /* Forward reference */
12576 static int
12577 DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12578 
12579 /*[clinic input]
12580 os.DirEntry.is_symlink -> bool
12581 
12582 Return True if the entry is a symbolic link; cached per entry.
12583 [clinic start generated code]*/
12584 
12585 static int
os_DirEntry_is_symlink_impl(DirEntry * self)12586 os_DirEntry_is_symlink_impl(DirEntry *self)
12587 /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
12588 {
12589 #ifdef MS_WINDOWS
12590     return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12591 #elif defined(HAVE_DIRENT_D_TYPE)
12592     /* POSIX */
12593     if (self->d_type != DT_UNKNOWN)
12594         return self->d_type == DT_LNK;
12595     else
12596         return DirEntry_test_mode(self, 0, S_IFLNK);
12597 #else
12598     /* POSIX without d_type */
12599     return DirEntry_test_mode(self, 0, S_IFLNK);
12600 #endif
12601 }
12602 
12603 static PyObject *
DirEntry_fetch_stat(DirEntry * self,int follow_symlinks)12604 DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12605 {
12606     int result;
12607     STRUCT_STAT st;
12608     PyObject *ub;
12609 
12610 #ifdef MS_WINDOWS
12611     if (!PyUnicode_FSDecoder(self->path, &ub))
12612         return NULL;
12613     const wchar_t *path = PyUnicode_AsUnicode(ub);
12614 #else /* POSIX */
12615     if (!PyUnicode_FSConverter(self->path, &ub))
12616         return NULL;
12617     const char *path = PyBytes_AS_STRING(ub);
12618     if (self->dir_fd != DEFAULT_DIR_FD) {
12619 #ifdef HAVE_FSTATAT
12620         result = fstatat(self->dir_fd, path, &st,
12621                          follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12622 #else
12623         PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12624         return NULL;
12625 #endif /* HAVE_FSTATAT */
12626     }
12627     else
12628 #endif
12629     {
12630         if (follow_symlinks)
12631             result = STAT(path, &st);
12632         else
12633             result = LSTAT(path, &st);
12634     }
12635     Py_DECREF(ub);
12636 
12637     if (result != 0)
12638         return path_object_error(self->path);
12639 
12640     return _pystat_fromstructstat(&st);
12641 }
12642 
12643 static PyObject *
DirEntry_get_lstat(DirEntry * self)12644 DirEntry_get_lstat(DirEntry *self)
12645 {
12646     if (!self->lstat) {
12647 #ifdef MS_WINDOWS
12648         self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12649 #else /* POSIX */
12650         self->lstat = DirEntry_fetch_stat(self, 0);
12651 #endif
12652     }
12653     Py_XINCREF(self->lstat);
12654     return self->lstat;
12655 }
12656 
12657 /*[clinic input]
12658 os.DirEntry.stat
12659     *
12660     follow_symlinks: bool = True
12661 
12662 Return stat_result object for the entry; cached per entry.
12663 [clinic start generated code]*/
12664 
12665 static PyObject *
os_DirEntry_stat_impl(DirEntry * self,int follow_symlinks)12666 os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12667 /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
12668 {
12669     if (!follow_symlinks)
12670         return DirEntry_get_lstat(self);
12671 
12672     if (!self->stat) {
12673         int result = os_DirEntry_is_symlink_impl(self);
12674         if (result == -1)
12675             return NULL;
12676         else if (result)
12677             self->stat = DirEntry_fetch_stat(self, 1);
12678         else
12679             self->stat = DirEntry_get_lstat(self);
12680     }
12681 
12682     Py_XINCREF(self->stat);
12683     return self->stat;
12684 }
12685 
12686 /* Set exception and return -1 on error, 0 for False, 1 for True */
12687 static int
DirEntry_test_mode(DirEntry * self,int follow_symlinks,unsigned short mode_bits)12688 DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12689 {
12690     PyObject *stat = NULL;
12691     PyObject *st_mode = NULL;
12692     long mode;
12693     int result;
12694 #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
12695     int is_symlink;
12696     int need_stat;
12697 #endif
12698 #ifdef MS_WINDOWS
12699     unsigned long dir_bits;
12700 #endif
12701     _Py_IDENTIFIER(st_mode);
12702 
12703 #ifdef MS_WINDOWS
12704     is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12705     need_stat = follow_symlinks && is_symlink;
12706 #elif defined(HAVE_DIRENT_D_TYPE)
12707     is_symlink = self->d_type == DT_LNK;
12708     need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12709 #endif
12710 
12711 #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
12712     if (need_stat) {
12713 #endif
12714         stat = os_DirEntry_stat_impl(self, follow_symlinks);
12715         if (!stat) {
12716             if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12717                 /* If file doesn't exist (anymore), then return False
12718                    (i.e., say it's not a file/directory) */
12719                 PyErr_Clear();
12720                 return 0;
12721             }
12722             goto error;
12723         }
12724         st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12725         if (!st_mode)
12726             goto error;
12727 
12728         mode = PyLong_AsLong(st_mode);
12729         if (mode == -1 && PyErr_Occurred())
12730             goto error;
12731         Py_CLEAR(st_mode);
12732         Py_CLEAR(stat);
12733         result = (mode & S_IFMT) == mode_bits;
12734 #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
12735     }
12736     else if (is_symlink) {
12737         assert(mode_bits != S_IFLNK);
12738         result = 0;
12739     }
12740     else {
12741         assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12742 #ifdef MS_WINDOWS
12743         dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12744         if (mode_bits == S_IFDIR)
12745             result = dir_bits != 0;
12746         else
12747             result = dir_bits == 0;
12748 #else /* POSIX */
12749         if (mode_bits == S_IFDIR)
12750             result = self->d_type == DT_DIR;
12751         else
12752             result = self->d_type == DT_REG;
12753 #endif
12754     }
12755 #endif
12756 
12757     return result;
12758 
12759 error:
12760     Py_XDECREF(st_mode);
12761     Py_XDECREF(stat);
12762     return -1;
12763 }
12764 
12765 /*[clinic input]
12766 os.DirEntry.is_dir -> bool
12767     *
12768     follow_symlinks: bool = True
12769 
12770 Return True if the entry is a directory; cached per entry.
12771 [clinic start generated code]*/
12772 
12773 static int
os_DirEntry_is_dir_impl(DirEntry * self,int follow_symlinks)12774 os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12775 /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12776 {
12777     return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
12778 }
12779 
12780 /*[clinic input]
12781 os.DirEntry.is_file -> bool
12782     *
12783     follow_symlinks: bool = True
12784 
12785 Return True if the entry is a file; cached per entry.
12786 [clinic start generated code]*/
12787 
12788 static int
os_DirEntry_is_file_impl(DirEntry * self,int follow_symlinks)12789 os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12790 /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
12791 {
12792     return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
12793 }
12794 
12795 /*[clinic input]
12796 os.DirEntry.inode
12797 
12798 Return inode of the entry; cached per entry.
12799 [clinic start generated code]*/
12800 
12801 static PyObject *
os_DirEntry_inode_impl(DirEntry * self)12802 os_DirEntry_inode_impl(DirEntry *self)
12803 /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
12804 {
12805 #ifdef MS_WINDOWS
12806     if (!self->got_file_index) {
12807         PyObject *unicode;
12808         const wchar_t *path;
12809         STRUCT_STAT stat;
12810         int result;
12811 
12812         if (!PyUnicode_FSDecoder(self->path, &unicode))
12813             return NULL;
12814         path = PyUnicode_AsUnicode(unicode);
12815         result = LSTAT(path, &stat);
12816         Py_DECREF(unicode);
12817 
12818         if (result != 0)
12819             return path_object_error(self->path);
12820 
12821         self->win32_file_index = stat.st_ino;
12822         self->got_file_index = 1;
12823     }
12824     Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12825     return PyLong_FromUnsignedLongLong(self->win32_file_index);
12826 #else /* POSIX */
12827     Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12828     return PyLong_FromUnsignedLongLong(self->d_ino);
12829 #endif
12830 }
12831 
12832 static PyObject *
DirEntry_repr(DirEntry * self)12833 DirEntry_repr(DirEntry *self)
12834 {
12835     return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12836 }
12837 
12838 /*[clinic input]
12839 os.DirEntry.__fspath__
12840 
12841 Returns the path for the entry.
12842 [clinic start generated code]*/
12843 
12844 static PyObject *
os_DirEntry___fspath___impl(DirEntry * self)12845 os_DirEntry___fspath___impl(DirEntry *self)
12846 /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
12847 {
12848     Py_INCREF(self->path);
12849     return self->path;
12850 }
12851 
12852 static PyMemberDef DirEntry_members[] = {
12853     {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12854      "the entry's base filename, relative to scandir() \"path\" argument"},
12855     {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12856      "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12857     {NULL}
12858 };
12859 
12860 #include "clinic/posixmodule.c.h"
12861 
12862 static PyMethodDef DirEntry_methods[] = {
12863     OS_DIRENTRY_IS_DIR_METHODDEF
12864     OS_DIRENTRY_IS_FILE_METHODDEF
12865     OS_DIRENTRY_IS_SYMLINK_METHODDEF
12866     OS_DIRENTRY_STAT_METHODDEF
12867     OS_DIRENTRY_INODE_METHODDEF
12868     OS_DIRENTRY___FSPATH___METHODDEF
12869     {NULL}
12870 };
12871 
12872 static PyTypeObject DirEntryType = {
12873     PyVarObject_HEAD_INIT(NULL, 0)
12874     MODNAME ".DirEntry",                    /* tp_name */
12875     sizeof(DirEntry),                       /* tp_basicsize */
12876     0,                                      /* tp_itemsize */
12877     /* methods */
12878     (destructor)DirEntry_dealloc,           /* tp_dealloc */
12879     0,                                      /* tp_vectorcall_offset */
12880     0,                                      /* tp_getattr */
12881     0,                                      /* tp_setattr */
12882     0,                                      /* tp_as_async */
12883     (reprfunc)DirEntry_repr,                /* tp_repr */
12884     0,                                      /* tp_as_number */
12885     0,                                      /* tp_as_sequence */
12886     0,                                      /* tp_as_mapping */
12887     0,                                      /* tp_hash */
12888     0,                                      /* tp_call */
12889     0,                                      /* tp_str */
12890     0,                                      /* tp_getattro */
12891     0,                                      /* tp_setattro */
12892     0,                                      /* tp_as_buffer */
12893     Py_TPFLAGS_DEFAULT,                     /* tp_flags */
12894     0,                                      /* tp_doc */
12895     0,                                      /* tp_traverse */
12896     0,                                      /* tp_clear */
12897     0,                                      /* tp_richcompare */
12898     0,                                      /* tp_weaklistoffset */
12899     0,                                      /* tp_iter */
12900     0,                                      /* tp_iternext */
12901     DirEntry_methods,                       /* tp_methods */
12902     DirEntry_members,                       /* tp_members */
12903 };
12904 
12905 #ifdef MS_WINDOWS
12906 
12907 static wchar_t *
join_path_filenameW(const wchar_t * path_wide,const wchar_t * filename)12908 join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
12909 {
12910     Py_ssize_t path_len;
12911     Py_ssize_t size;
12912     wchar_t *result;
12913     wchar_t ch;
12914 
12915     if (!path_wide) { /* Default arg: "." */
12916         path_wide = L".";
12917         path_len = 1;
12918     }
12919     else {
12920         path_len = wcslen(path_wide);
12921     }
12922 
12923     /* The +1's are for the path separator and the NUL */
12924     size = path_len + 1 + wcslen(filename) + 1;
12925     result = PyMem_New(wchar_t, size);
12926     if (!result) {
12927         PyErr_NoMemory();
12928         return NULL;
12929     }
12930     wcscpy(result, path_wide);
12931     if (path_len > 0) {
12932         ch = result[path_len - 1];
12933         if (ch != SEP && ch != ALTSEP && ch != L':')
12934             result[path_len++] = SEP;
12935         wcscpy(result + path_len, filename);
12936     }
12937     return result;
12938 }
12939 
12940 static PyObject *
DirEntry_from_find_data(path_t * path,WIN32_FIND_DATAW * dataW)12941 DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12942 {
12943     DirEntry *entry;
12944     BY_HANDLE_FILE_INFORMATION file_info;
12945     ULONG reparse_tag;
12946     wchar_t *joined_path;
12947 
12948     entry = PyObject_New(DirEntry, &DirEntryType);
12949     if (!entry)
12950         return NULL;
12951     entry->name = NULL;
12952     entry->path = NULL;
12953     entry->stat = NULL;
12954     entry->lstat = NULL;
12955     entry->got_file_index = 0;
12956 
12957     entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12958     if (!entry->name)
12959         goto error;
12960     if (path->narrow) {
12961         Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12962         if (!entry->name)
12963             goto error;
12964     }
12965 
12966     joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12967     if (!joined_path)
12968         goto error;
12969 
12970     entry->path = PyUnicode_FromWideChar(joined_path, -1);
12971     PyMem_Free(joined_path);
12972     if (!entry->path)
12973         goto error;
12974     if (path->narrow) {
12975         Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12976         if (!entry->path)
12977             goto error;
12978     }
12979 
12980     find_data_to_file_info(dataW, &file_info, &reparse_tag);
12981     _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12982 
12983     return (PyObject *)entry;
12984 
12985 error:
12986     Py_DECREF(entry);
12987     return NULL;
12988 }
12989 
12990 #else /* POSIX */
12991 
12992 static char *
join_path_filename(const char * path_narrow,const char * filename,Py_ssize_t filename_len)12993 join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
12994 {
12995     Py_ssize_t path_len;
12996     Py_ssize_t size;
12997     char *result;
12998 
12999     if (!path_narrow) { /* Default arg: "." */
13000         path_narrow = ".";
13001         path_len = 1;
13002     }
13003     else {
13004         path_len = strlen(path_narrow);
13005     }
13006 
13007     if (filename_len == -1)
13008         filename_len = strlen(filename);
13009 
13010     /* The +1's are for the path separator and the NUL */
13011     size = path_len + 1 + filename_len + 1;
13012     result = PyMem_New(char, size);
13013     if (!result) {
13014         PyErr_NoMemory();
13015         return NULL;
13016     }
13017     strcpy(result, path_narrow);
13018     if (path_len > 0 && result[path_len - 1] != '/')
13019         result[path_len++] = '/';
13020     strcpy(result + path_len, filename);
13021     return result;
13022 }
13023 
13024 static PyObject *
DirEntry_from_posix_info(path_t * path,const char * name,Py_ssize_t name_len,ino_t d_ino,unsigned char d_type)13025 DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
13026                          ino_t d_ino
13027 #ifdef HAVE_DIRENT_D_TYPE
13028                          , unsigned char d_type
13029 #endif
13030                          )
13031 {
13032     DirEntry *entry;
13033     char *joined_path;
13034 
13035     entry = PyObject_New(DirEntry, &DirEntryType);
13036     if (!entry)
13037         return NULL;
13038     entry->name = NULL;
13039     entry->path = NULL;
13040     entry->stat = NULL;
13041     entry->lstat = NULL;
13042 
13043     if (path->fd != -1) {
13044         entry->dir_fd = path->fd;
13045         joined_path = NULL;
13046     }
13047     else {
13048         entry->dir_fd = DEFAULT_DIR_FD;
13049         joined_path = join_path_filename(path->narrow, name, name_len);
13050         if (!joined_path)
13051             goto error;
13052     }
13053 
13054     if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
13055         entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
13056         if (joined_path)
13057             entry->path = PyUnicode_DecodeFSDefault(joined_path);
13058     }
13059     else {
13060         entry->name = PyBytes_FromStringAndSize(name, name_len);
13061         if (joined_path)
13062             entry->path = PyBytes_FromString(joined_path);
13063     }
13064     PyMem_Free(joined_path);
13065     if (!entry->name)
13066         goto error;
13067 
13068     if (path->fd != -1) {
13069         entry->path = entry->name;
13070         Py_INCREF(entry->path);
13071     }
13072     else if (!entry->path)
13073         goto error;
13074 
13075 #ifdef HAVE_DIRENT_D_TYPE
13076     entry->d_type = d_type;
13077 #endif
13078     entry->d_ino = d_ino;
13079 
13080     return (PyObject *)entry;
13081 
13082 error:
13083     Py_XDECREF(entry);
13084     return NULL;
13085 }
13086 
13087 #endif
13088 
13089 
13090 typedef struct {
13091     PyObject_HEAD
13092     path_t path;
13093 #ifdef MS_WINDOWS
13094     HANDLE handle;
13095     WIN32_FIND_DATAW file_data;
13096     int first_time;
13097 #else /* POSIX */
13098     DIR *dirp;
13099 #endif
13100 #ifdef HAVE_FDOPENDIR
13101     int fd;
13102 #endif
13103 } ScandirIterator;
13104 
13105 #ifdef MS_WINDOWS
13106 
13107 static int
ScandirIterator_is_closed(ScandirIterator * iterator)13108 ScandirIterator_is_closed(ScandirIterator *iterator)
13109 {
13110     return iterator->handle == INVALID_HANDLE_VALUE;
13111 }
13112 
13113 static void
ScandirIterator_closedir(ScandirIterator * iterator)13114 ScandirIterator_closedir(ScandirIterator *iterator)
13115 {
13116     HANDLE handle = iterator->handle;
13117 
13118     if (handle == INVALID_HANDLE_VALUE)
13119         return;
13120 
13121     iterator->handle = INVALID_HANDLE_VALUE;
13122     Py_BEGIN_ALLOW_THREADS
13123     FindClose(handle);
13124     Py_END_ALLOW_THREADS
13125 }
13126 
13127 static PyObject *
ScandirIterator_iternext(ScandirIterator * iterator)13128 ScandirIterator_iternext(ScandirIterator *iterator)
13129 {
13130     WIN32_FIND_DATAW *file_data = &iterator->file_data;
13131     BOOL success;
13132     PyObject *entry;
13133 
13134     /* Happens if the iterator is iterated twice, or closed explicitly */
13135     if (iterator->handle == INVALID_HANDLE_VALUE)
13136         return NULL;
13137 
13138     while (1) {
13139         if (!iterator->first_time) {
13140             Py_BEGIN_ALLOW_THREADS
13141             success = FindNextFileW(iterator->handle, file_data);
13142             Py_END_ALLOW_THREADS
13143             if (!success) {
13144                 /* Error or no more files */
13145                 if (GetLastError() != ERROR_NO_MORE_FILES)
13146                     path_error(&iterator->path);
13147                 break;
13148             }
13149         }
13150         iterator->first_time = 0;
13151 
13152         /* Skip over . and .. */
13153         if (wcscmp(file_data->cFileName, L".") != 0 &&
13154             wcscmp(file_data->cFileName, L"..") != 0) {
13155             entry = DirEntry_from_find_data(&iterator->path, file_data);
13156             if (!entry)
13157                 break;
13158             return entry;
13159         }
13160 
13161         /* Loop till we get a non-dot directory or finish iterating */
13162     }
13163 
13164     /* Error or no more files */
13165     ScandirIterator_closedir(iterator);
13166     return NULL;
13167 }
13168 
13169 #else /* POSIX */
13170 
13171 static int
ScandirIterator_is_closed(ScandirIterator * iterator)13172 ScandirIterator_is_closed(ScandirIterator *iterator)
13173 {
13174     return !iterator->dirp;
13175 }
13176 
13177 static void
ScandirIterator_closedir(ScandirIterator * iterator)13178 ScandirIterator_closedir(ScandirIterator *iterator)
13179 {
13180     DIR *dirp = iterator->dirp;
13181 
13182     if (!dirp)
13183         return;
13184 
13185     iterator->dirp = NULL;
13186     Py_BEGIN_ALLOW_THREADS
13187 #ifdef HAVE_FDOPENDIR
13188     if (iterator->path.fd != -1)
13189         rewinddir(dirp);
13190 #endif
13191     closedir(dirp);
13192     Py_END_ALLOW_THREADS
13193     return;
13194 }
13195 
13196 static PyObject *
ScandirIterator_iternext(ScandirIterator * iterator)13197 ScandirIterator_iternext(ScandirIterator *iterator)
13198 {
13199     struct dirent *direntp;
13200     Py_ssize_t name_len;
13201     int is_dot;
13202     PyObject *entry;
13203 
13204     /* Happens if the iterator is iterated twice, or closed explicitly */
13205     if (!iterator->dirp)
13206         return NULL;
13207 
13208     while (1) {
13209         errno = 0;
13210         Py_BEGIN_ALLOW_THREADS
13211         direntp = readdir(iterator->dirp);
13212         Py_END_ALLOW_THREADS
13213 
13214         if (!direntp) {
13215             /* Error or no more files */
13216             if (errno != 0)
13217                 path_error(&iterator->path);
13218             break;
13219         }
13220 
13221         /* Skip over . and .. */
13222         name_len = NAMLEN(direntp);
13223         is_dot = direntp->d_name[0] == '.' &&
13224                  (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13225         if (!is_dot) {
13226             entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
13227                                             name_len, direntp->d_ino
13228 #ifdef HAVE_DIRENT_D_TYPE
13229                                             , direntp->d_type
13230 #endif
13231                                             );
13232             if (!entry)
13233                 break;
13234             return entry;
13235         }
13236 
13237         /* Loop till we get a non-dot directory or finish iterating */
13238     }
13239 
13240     /* Error or no more files */
13241     ScandirIterator_closedir(iterator);
13242     return NULL;
13243 }
13244 
13245 #endif
13246 
13247 static PyObject *
ScandirIterator_close(ScandirIterator * self,PyObject * args)13248 ScandirIterator_close(ScandirIterator *self, PyObject *args)
13249 {
13250     ScandirIterator_closedir(self);
13251     Py_RETURN_NONE;
13252 }
13253 
13254 static PyObject *
ScandirIterator_enter(PyObject * self,PyObject * args)13255 ScandirIterator_enter(PyObject *self, PyObject *args)
13256 {
13257     Py_INCREF(self);
13258     return self;
13259 }
13260 
13261 static PyObject *
ScandirIterator_exit(ScandirIterator * self,PyObject * args)13262 ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13263 {
13264     ScandirIterator_closedir(self);
13265     Py_RETURN_NONE;
13266 }
13267 
13268 static void
ScandirIterator_finalize(ScandirIterator * iterator)13269 ScandirIterator_finalize(ScandirIterator *iterator)
13270 {
13271     PyObject *error_type, *error_value, *error_traceback;
13272 
13273     /* Save the current exception, if any. */
13274     PyErr_Fetch(&error_type, &error_value, &error_traceback);
13275 
13276     if (!ScandirIterator_is_closed(iterator)) {
13277         ScandirIterator_closedir(iterator);
13278 
13279         if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13280                                   "unclosed scandir iterator %R", iterator)) {
13281             /* Spurious errors can appear at shutdown */
13282             if (PyErr_ExceptionMatches(PyExc_Warning)) {
13283                 PyErr_WriteUnraisable((PyObject *) iterator);
13284             }
13285         }
13286     }
13287 
13288     path_cleanup(&iterator->path);
13289 
13290     /* Restore the saved exception. */
13291     PyErr_Restore(error_type, error_value, error_traceback);
13292 }
13293 
13294 static void
ScandirIterator_dealloc(ScandirIterator * iterator)13295 ScandirIterator_dealloc(ScandirIterator *iterator)
13296 {
13297     if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13298         return;
13299 
13300     Py_TYPE(iterator)->tp_free((PyObject *)iterator);
13301 }
13302 
13303 static PyMethodDef ScandirIterator_methods[] = {
13304     {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13305     {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13306     {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13307     {NULL}
13308 };
13309 
13310 static PyTypeObject ScandirIteratorType = {
13311     PyVarObject_HEAD_INIT(NULL, 0)
13312     MODNAME ".ScandirIterator",             /* tp_name */
13313     sizeof(ScandirIterator),                /* tp_basicsize */
13314     0,                                      /* tp_itemsize */
13315     /* methods */
13316     (destructor)ScandirIterator_dealloc,    /* tp_dealloc */
13317     0,                                      /* tp_vectorcall_offset */
13318     0,                                      /* tp_getattr */
13319     0,                                      /* tp_setattr */
13320     0,                                      /* tp_as_async */
13321     0,                                      /* tp_repr */
13322     0,                                      /* tp_as_number */
13323     0,                                      /* tp_as_sequence */
13324     0,                                      /* tp_as_mapping */
13325     0,                                      /* tp_hash */
13326     0,                                      /* tp_call */
13327     0,                                      /* tp_str */
13328     0,                                      /* tp_getattro */
13329     0,                                      /* tp_setattro */
13330     0,                                      /* tp_as_buffer */
13331     Py_TPFLAGS_DEFAULT,                     /* tp_flags */
13332     0,                                      /* tp_doc */
13333     0,                                      /* tp_traverse */
13334     0,                                      /* tp_clear */
13335     0,                                      /* tp_richcompare */
13336     0,                                      /* tp_weaklistoffset */
13337     PyObject_SelfIter,                      /* tp_iter */
13338     (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
13339     ScandirIterator_methods,                /* tp_methods */
13340     0,                                      /* tp_members */
13341     0,                                      /* tp_getset */
13342     0,                                      /* tp_base */
13343     0,                                      /* tp_dict */
13344     0,                                      /* tp_descr_get */
13345     0,                                      /* tp_descr_set */
13346     0,                                      /* tp_dictoffset */
13347     0,                                      /* tp_init */
13348     0,                                      /* tp_alloc */
13349     0,                                      /* tp_new */
13350     0,                                      /* tp_free */
13351     0,                                      /* tp_is_gc */
13352     0,                                      /* tp_bases */
13353     0,                                      /* tp_mro */
13354     0,                                      /* tp_cache */
13355     0,                                      /* tp_subclasses */
13356     0,                                      /* tp_weaklist */
13357     0,                                      /* tp_del */
13358     0,                                      /* tp_version_tag */
13359     (destructor)ScandirIterator_finalize,   /* tp_finalize */
13360 };
13361 
13362 /*[clinic input]
13363 os.scandir
13364 
13365     path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
13366 
13367 Return an iterator of DirEntry objects for given path.
13368 
13369 path can be specified as either str, bytes, or a path-like object.  If path
13370 is bytes, the names of yielded DirEntry objects will also be bytes; in
13371 all other circumstances they will be str.
13372 
13373 If path is None, uses the path='.'.
13374 [clinic start generated code]*/
13375 
13376 static PyObject *
os_scandir_impl(PyObject * module,path_t * path)13377 os_scandir_impl(PyObject *module, path_t *path)
13378 /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
13379 {
13380     ScandirIterator *iterator;
13381 #ifdef MS_WINDOWS
13382     wchar_t *path_strW;
13383 #else
13384     const char *path_str;
13385 #ifdef HAVE_FDOPENDIR
13386     int fd = -1;
13387 #endif
13388 #endif
13389 
13390     if (PySys_Audit("os.scandir", "O",
13391                     path->object ? path->object : Py_None) < 0) {
13392         return NULL;
13393     }
13394 
13395     iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
13396     if (!iterator)
13397         return NULL;
13398 
13399 #ifdef MS_WINDOWS
13400     iterator->handle = INVALID_HANDLE_VALUE;
13401 #else
13402     iterator->dirp = NULL;
13403 #endif
13404 
13405     memcpy(&iterator->path, path, sizeof(path_t));
13406     /* Move the ownership to iterator->path */
13407     path->object = NULL;
13408     path->cleanup = NULL;
13409 
13410 #ifdef MS_WINDOWS
13411     iterator->first_time = 1;
13412 
13413     path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13414     if (!path_strW)
13415         goto error;
13416 
13417     Py_BEGIN_ALLOW_THREADS
13418     iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13419     Py_END_ALLOW_THREADS
13420 
13421     PyMem_Free(path_strW);
13422 
13423     if (iterator->handle == INVALID_HANDLE_VALUE) {
13424         path_error(&iterator->path);
13425         goto error;
13426     }
13427 #else /* POSIX */
13428     errno = 0;
13429 #ifdef HAVE_FDOPENDIR
13430     if (path->fd != -1) {
13431         /* closedir() closes the FD, so we duplicate it */
13432         fd = _Py_dup(path->fd);
13433         if (fd == -1)
13434             goto error;
13435 
13436         Py_BEGIN_ALLOW_THREADS
13437         iterator->dirp = fdopendir(fd);
13438         Py_END_ALLOW_THREADS
13439     }
13440     else
13441 #endif
13442     {
13443         if (iterator->path.narrow)
13444             path_str = iterator->path.narrow;
13445         else
13446             path_str = ".";
13447 
13448         Py_BEGIN_ALLOW_THREADS
13449         iterator->dirp = opendir(path_str);
13450         Py_END_ALLOW_THREADS
13451     }
13452 
13453     if (!iterator->dirp) {
13454         path_error(&iterator->path);
13455 #ifdef HAVE_FDOPENDIR
13456         if (fd != -1) {
13457             Py_BEGIN_ALLOW_THREADS
13458             close(fd);
13459             Py_END_ALLOW_THREADS
13460         }
13461 #endif
13462         goto error;
13463     }
13464 #endif
13465 
13466     return (PyObject *)iterator;
13467 
13468 error:
13469     Py_DECREF(iterator);
13470     return NULL;
13471 }
13472 
13473 /*
13474     Return the file system path representation of the object.
13475 
13476     If the object is str or bytes, then allow it to pass through with
13477     an incremented refcount. If the object defines __fspath__(), then
13478     return the result of that method. All other types raise a TypeError.
13479 */
13480 PyObject *
PyOS_FSPath(PyObject * path)13481 PyOS_FSPath(PyObject *path)
13482 {
13483     /* For error message reasons, this function is manually inlined in
13484        path_converter(). */
13485     _Py_IDENTIFIER(__fspath__);
13486     PyObject *func = NULL;
13487     PyObject *path_repr = NULL;
13488 
13489     if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13490         Py_INCREF(path);
13491         return path;
13492     }
13493 
13494     func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13495     if (NULL == func) {
13496         return PyErr_Format(PyExc_TypeError,
13497                             "expected str, bytes or os.PathLike object, "
13498                             "not %.200s",
13499                             Py_TYPE(path)->tp_name);
13500     }
13501 
13502     path_repr = _PyObject_CallNoArg(func);
13503     Py_DECREF(func);
13504     if (NULL == path_repr) {
13505         return NULL;
13506     }
13507 
13508     if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13509         PyErr_Format(PyExc_TypeError,
13510                      "expected %.200s.__fspath__() to return str or bytes, "
13511                      "not %.200s", Py_TYPE(path)->tp_name,
13512                      Py_TYPE(path_repr)->tp_name);
13513         Py_DECREF(path_repr);
13514         return NULL;
13515     }
13516 
13517     return path_repr;
13518 }
13519 
13520 /*[clinic input]
13521 os.fspath
13522 
13523     path: object
13524 
13525 Return the file system path representation of the object.
13526 
13527 If the object is str or bytes, then allow it to pass through as-is. If the
13528 object defines __fspath__(), then return the result of that method. All other
13529 types raise a TypeError.
13530 [clinic start generated code]*/
13531 
13532 static PyObject *
os_fspath_impl(PyObject * module,PyObject * path)13533 os_fspath_impl(PyObject *module, PyObject *path)
13534 /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
13535 {
13536     return PyOS_FSPath(path);
13537 }
13538 
13539 #ifdef HAVE_GETRANDOM_SYSCALL
13540 /*[clinic input]
13541 os.getrandom
13542 
13543     size: Py_ssize_t
13544     flags: int=0
13545 
13546 Obtain a series of random bytes.
13547 [clinic start generated code]*/
13548 
13549 static PyObject *
os_getrandom_impl(PyObject * module,Py_ssize_t size,int flags)13550 os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13551 /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13552 {
13553     PyObject *bytes;
13554     Py_ssize_t n;
13555 
13556     if (size < 0) {
13557         errno = EINVAL;
13558         return posix_error();
13559     }
13560 
13561     bytes = PyBytes_FromStringAndSize(NULL, size);
13562     if (bytes == NULL) {
13563         PyErr_NoMemory();
13564         return NULL;
13565     }
13566 
13567     while (1) {
13568         n = syscall(SYS_getrandom,
13569                     PyBytes_AS_STRING(bytes),
13570                     PyBytes_GET_SIZE(bytes),
13571                     flags);
13572         if (n < 0 && errno == EINTR) {
13573             if (PyErr_CheckSignals() < 0) {
13574                 goto error;
13575             }
13576 
13577             /* getrandom() was interrupted by a signal: retry */
13578             continue;
13579         }
13580         break;
13581     }
13582 
13583     if (n < 0) {
13584         PyErr_SetFromErrno(PyExc_OSError);
13585         goto error;
13586     }
13587 
13588     if (n != size) {
13589         _PyBytes_Resize(&bytes, n);
13590     }
13591 
13592     return bytes;
13593 
13594 error:
13595     Py_DECREF(bytes);
13596     return NULL;
13597 }
13598 #endif   /* HAVE_GETRANDOM_SYSCALL */
13599 
13600 #ifdef MS_WINDOWS
13601 /* bpo-36085: Helper functions for managing DLL search directories
13602  * on win32
13603  */
13604 
13605 typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13606 typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13607 
13608 /*[clinic input]
13609 os._add_dll_directory
13610 
13611     path: path_t
13612 
13613 Add a path to the DLL search path.
13614 
13615 This search path is used when resolving dependencies for imported
13616 extension modules (the module itself is resolved through sys.path),
13617 and also by ctypes.
13618 
13619 Returns an opaque value that may be passed to os.remove_dll_directory
13620 to remove this directory from the search path.
13621 [clinic start generated code]*/
13622 
13623 static PyObject *
os__add_dll_directory_impl(PyObject * module,path_t * path)13624 os__add_dll_directory_impl(PyObject *module, path_t *path)
13625 /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13626 {
13627     HMODULE hKernel32;
13628     PAddDllDirectory AddDllDirectory;
13629     DLL_DIRECTORY_COOKIE cookie = 0;
13630     DWORD err = 0;
13631 
13632     if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
13633         return NULL;
13634     }
13635 
13636     /* For Windows 7, we have to load this. As this will be a fairly
13637        infrequent operation, just do it each time. Kernel32 is always
13638        loaded. */
13639     Py_BEGIN_ALLOW_THREADS
13640     if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13641         !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13642             hKernel32, "AddDllDirectory")) ||
13643         !(cookie = (*AddDllDirectory)(path->wide))) {
13644         err = GetLastError();
13645     }
13646     Py_END_ALLOW_THREADS
13647 
13648     if (err) {
13649         return win32_error_object_err("add_dll_directory",
13650                                       path->object, err);
13651     }
13652 
13653     return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13654 }
13655 
13656 /*[clinic input]
13657 os._remove_dll_directory
13658 
13659     cookie: object
13660 
13661 Removes a path from the DLL search path.
13662 
13663 The parameter is an opaque value that was returned from
13664 os.add_dll_directory. You can only remove directories that you added
13665 yourself.
13666 [clinic start generated code]*/
13667 
13668 static PyObject *
os__remove_dll_directory_impl(PyObject * module,PyObject * cookie)13669 os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13670 /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13671 {
13672     HMODULE hKernel32;
13673     PRemoveDllDirectory RemoveDllDirectory;
13674     DLL_DIRECTORY_COOKIE cookieValue;
13675     DWORD err = 0;
13676 
13677     if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13678         PyErr_SetString(PyExc_TypeError,
13679             "Provided cookie was not returned from os.add_dll_directory");
13680         return NULL;
13681     }
13682 
13683     cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13684         cookie, "DLL directory cookie");
13685 
13686     /* For Windows 7, we have to load this. As this will be a fairly
13687        infrequent operation, just do it each time. Kernel32 is always
13688        loaded. */
13689     Py_BEGIN_ALLOW_THREADS
13690     if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13691         !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13692             hKernel32, "RemoveDllDirectory")) ||
13693         !(*RemoveDllDirectory)(cookieValue)) {
13694         err = GetLastError();
13695     }
13696     Py_END_ALLOW_THREADS
13697 
13698     if (err) {
13699         return win32_error_object_err("remove_dll_directory",
13700                                       NULL, err);
13701     }
13702 
13703     if (PyCapsule_SetName(cookie, NULL)) {
13704         return NULL;
13705     }
13706 
13707     Py_RETURN_NONE;
13708 }
13709 
13710 #endif
13711 
13712 static PyMethodDef posix_methods[] = {
13713 
13714     OS_STAT_METHODDEF
13715     OS_ACCESS_METHODDEF
13716     OS_TTYNAME_METHODDEF
13717     OS_CHDIR_METHODDEF
13718     OS_CHFLAGS_METHODDEF
13719     OS_CHMOD_METHODDEF
13720     OS_FCHMOD_METHODDEF
13721     OS_LCHMOD_METHODDEF
13722     OS_CHOWN_METHODDEF
13723     OS_FCHOWN_METHODDEF
13724     OS_LCHOWN_METHODDEF
13725     OS_LCHFLAGS_METHODDEF
13726     OS_CHROOT_METHODDEF
13727     OS_CTERMID_METHODDEF
13728     OS_GETCWD_METHODDEF
13729     OS_GETCWDB_METHODDEF
13730     OS_LINK_METHODDEF
13731     OS_LISTDIR_METHODDEF
13732     OS_LSTAT_METHODDEF
13733     OS_MKDIR_METHODDEF
13734     OS_NICE_METHODDEF
13735     OS_GETPRIORITY_METHODDEF
13736     OS_SETPRIORITY_METHODDEF
13737     OS_POSIX_SPAWN_METHODDEF
13738     OS_POSIX_SPAWNP_METHODDEF
13739     OS_READLINK_METHODDEF
13740     OS_COPY_FILE_RANGE_METHODDEF
13741     OS_RENAME_METHODDEF
13742     OS_REPLACE_METHODDEF
13743     OS_RMDIR_METHODDEF
13744     OS_SYMLINK_METHODDEF
13745     OS_SYSTEM_METHODDEF
13746     OS_UMASK_METHODDEF
13747     OS_UNAME_METHODDEF
13748     OS_UNLINK_METHODDEF
13749     OS_REMOVE_METHODDEF
13750     OS_UTIME_METHODDEF
13751     OS_TIMES_METHODDEF
13752     OS__EXIT_METHODDEF
13753     OS__FCOPYFILE_METHODDEF
13754     OS_EXECV_METHODDEF
13755     OS_EXECVE_METHODDEF
13756     OS_SPAWNV_METHODDEF
13757     OS_SPAWNVE_METHODDEF
13758     OS_FORK1_METHODDEF
13759     OS_FORK_METHODDEF
13760     OS_REGISTER_AT_FORK_METHODDEF
13761     OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13762     OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13763     OS_SCHED_GETPARAM_METHODDEF
13764     OS_SCHED_GETSCHEDULER_METHODDEF
13765     OS_SCHED_RR_GET_INTERVAL_METHODDEF
13766     OS_SCHED_SETPARAM_METHODDEF
13767     OS_SCHED_SETSCHEDULER_METHODDEF
13768     OS_SCHED_YIELD_METHODDEF
13769     OS_SCHED_SETAFFINITY_METHODDEF
13770     OS_SCHED_GETAFFINITY_METHODDEF
13771     OS_OPENPTY_METHODDEF
13772     OS_FORKPTY_METHODDEF
13773     OS_GETEGID_METHODDEF
13774     OS_GETEUID_METHODDEF
13775     OS_GETGID_METHODDEF
13776 #ifdef HAVE_GETGROUPLIST
13777     {"getgrouplist",    posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13778 #endif
13779     OS_GETGROUPS_METHODDEF
13780     OS_GETPID_METHODDEF
13781     OS_GETPGRP_METHODDEF
13782     OS_GETPPID_METHODDEF
13783     OS_GETUID_METHODDEF
13784     OS_GETLOGIN_METHODDEF
13785     OS_KILL_METHODDEF
13786     OS_KILLPG_METHODDEF
13787     OS_PLOCK_METHODDEF
13788 #ifdef MS_WINDOWS
13789     OS_STARTFILE_METHODDEF
13790 #endif
13791     OS_SETUID_METHODDEF
13792     OS_SETEUID_METHODDEF
13793     OS_SETREUID_METHODDEF
13794     OS_SETGID_METHODDEF
13795     OS_SETEGID_METHODDEF
13796     OS_SETREGID_METHODDEF
13797     OS_SETGROUPS_METHODDEF
13798 #ifdef HAVE_INITGROUPS
13799     {"initgroups",      posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
13800 #endif /* HAVE_INITGROUPS */
13801     OS_GETPGID_METHODDEF
13802     OS_SETPGRP_METHODDEF
13803     OS_WAIT_METHODDEF
13804     OS_WAIT3_METHODDEF
13805     OS_WAIT4_METHODDEF
13806     OS_WAITID_METHODDEF
13807     OS_WAITPID_METHODDEF
13808     OS_GETSID_METHODDEF
13809     OS_SETSID_METHODDEF
13810     OS_SETPGID_METHODDEF
13811     OS_TCGETPGRP_METHODDEF
13812     OS_TCSETPGRP_METHODDEF
13813     OS_OPEN_METHODDEF
13814     OS_CLOSE_METHODDEF
13815     OS_CLOSERANGE_METHODDEF
13816     OS_DEVICE_ENCODING_METHODDEF
13817     OS_DUP_METHODDEF
13818     OS_DUP2_METHODDEF
13819     OS_LOCKF_METHODDEF
13820     OS_LSEEK_METHODDEF
13821     OS_READ_METHODDEF
13822     OS_READV_METHODDEF
13823     OS_PREAD_METHODDEF
13824     OS_PREADV_METHODDEF
13825     OS_WRITE_METHODDEF
13826     OS_WRITEV_METHODDEF
13827     OS_PWRITE_METHODDEF
13828     OS_PWRITEV_METHODDEF
13829 #ifdef HAVE_SENDFILE
13830     {"sendfile",        (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
13831                             posix_sendfile__doc__},
13832 #endif
13833     OS_FSTAT_METHODDEF
13834     OS_ISATTY_METHODDEF
13835     OS_PIPE_METHODDEF
13836     OS_PIPE2_METHODDEF
13837     OS_MKFIFO_METHODDEF
13838     OS_MKNOD_METHODDEF
13839     OS_MAJOR_METHODDEF
13840     OS_MINOR_METHODDEF
13841     OS_MAKEDEV_METHODDEF
13842     OS_FTRUNCATE_METHODDEF
13843     OS_TRUNCATE_METHODDEF
13844     OS_POSIX_FALLOCATE_METHODDEF
13845     OS_POSIX_FADVISE_METHODDEF
13846     OS_PUTENV_METHODDEF
13847     OS_UNSETENV_METHODDEF
13848     OS_STRERROR_METHODDEF
13849     OS_FCHDIR_METHODDEF
13850     OS_FSYNC_METHODDEF
13851     OS_SYNC_METHODDEF
13852     OS_FDATASYNC_METHODDEF
13853     OS_WCOREDUMP_METHODDEF
13854     OS_WIFCONTINUED_METHODDEF
13855     OS_WIFSTOPPED_METHODDEF
13856     OS_WIFSIGNALED_METHODDEF
13857     OS_WIFEXITED_METHODDEF
13858     OS_WEXITSTATUS_METHODDEF
13859     OS_WTERMSIG_METHODDEF
13860     OS_WSTOPSIG_METHODDEF
13861     OS_FSTATVFS_METHODDEF
13862     OS_STATVFS_METHODDEF
13863     OS_CONFSTR_METHODDEF
13864     OS_SYSCONF_METHODDEF
13865     OS_FPATHCONF_METHODDEF
13866     OS_PATHCONF_METHODDEF
13867     OS_ABORT_METHODDEF
13868     OS__GETFULLPATHNAME_METHODDEF
13869     OS__GETDISKUSAGE_METHODDEF
13870     OS__GETFINALPATHNAME_METHODDEF
13871     OS__GETVOLUMEPATHNAME_METHODDEF
13872     OS_GETLOADAVG_METHODDEF
13873     OS_URANDOM_METHODDEF
13874     OS_SETRESUID_METHODDEF
13875     OS_SETRESGID_METHODDEF
13876     OS_GETRESUID_METHODDEF
13877     OS_GETRESGID_METHODDEF
13878 
13879     OS_GETXATTR_METHODDEF
13880     OS_SETXATTR_METHODDEF
13881     OS_REMOVEXATTR_METHODDEF
13882     OS_LISTXATTR_METHODDEF
13883 
13884 #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13885     {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13886 #endif
13887     OS_CPU_COUNT_METHODDEF
13888     OS_GET_INHERITABLE_METHODDEF
13889     OS_SET_INHERITABLE_METHODDEF
13890     OS_GET_HANDLE_INHERITABLE_METHODDEF
13891     OS_SET_HANDLE_INHERITABLE_METHODDEF
13892 #ifndef MS_WINDOWS
13893     OS_GET_BLOCKING_METHODDEF
13894     OS_SET_BLOCKING_METHODDEF
13895 #endif
13896     OS_SCANDIR_METHODDEF
13897     OS_FSPATH_METHODDEF
13898     OS_GETRANDOM_METHODDEF
13899     OS_MEMFD_CREATE_METHODDEF
13900 #ifdef MS_WINDOWS
13901     OS__ADD_DLL_DIRECTORY_METHODDEF
13902     OS__REMOVE_DLL_DIRECTORY_METHODDEF
13903 #endif
13904     {NULL,              NULL}            /* Sentinel */
13905 };
13906 
13907 static int
all_ins(PyObject * m)13908 all_ins(PyObject *m)
13909 {
13910 #ifdef F_OK
13911     if (PyModule_AddIntMacro(m, F_OK)) return -1;
13912 #endif
13913 #ifdef R_OK
13914     if (PyModule_AddIntMacro(m, R_OK)) return -1;
13915 #endif
13916 #ifdef W_OK
13917     if (PyModule_AddIntMacro(m, W_OK)) return -1;
13918 #endif
13919 #ifdef X_OK
13920     if (PyModule_AddIntMacro(m, X_OK)) return -1;
13921 #endif
13922 #ifdef NGROUPS_MAX
13923     if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
13924 #endif
13925 #ifdef TMP_MAX
13926     if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
13927 #endif
13928 #ifdef WCONTINUED
13929     if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
13930 #endif
13931 #ifdef WNOHANG
13932     if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
13933 #endif
13934 #ifdef WUNTRACED
13935     if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
13936 #endif
13937 #ifdef O_RDONLY
13938     if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
13939 #endif
13940 #ifdef O_WRONLY
13941     if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
13942 #endif
13943 #ifdef O_RDWR
13944     if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
13945 #endif
13946 #ifdef O_NDELAY
13947     if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
13948 #endif
13949 #ifdef O_NONBLOCK
13950     if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
13951 #endif
13952 #ifdef O_APPEND
13953     if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
13954 #endif
13955 #ifdef O_DSYNC
13956     if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
13957 #endif
13958 #ifdef O_RSYNC
13959     if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
13960 #endif
13961 #ifdef O_SYNC
13962     if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
13963 #endif
13964 #ifdef O_NOCTTY
13965     if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
13966 #endif
13967 #ifdef O_CREAT
13968     if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
13969 #endif
13970 #ifdef O_EXCL
13971     if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
13972 #endif
13973 #ifdef O_TRUNC
13974     if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
13975 #endif
13976 #ifdef O_BINARY
13977     if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
13978 #endif
13979 #ifdef O_TEXT
13980     if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
13981 #endif
13982 #ifdef O_XATTR
13983     if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
13984 #endif
13985 #ifdef O_LARGEFILE
13986     if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
13987 #endif
13988 #ifndef __GNU__
13989 #ifdef O_SHLOCK
13990     if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
13991 #endif
13992 #ifdef O_EXLOCK
13993     if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
13994 #endif
13995 #endif
13996 #ifdef O_EXEC
13997     if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
13998 #endif
13999 #ifdef O_SEARCH
14000     if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
14001 #endif
14002 #ifdef O_PATH
14003     if (PyModule_AddIntMacro(m, O_PATH)) return -1;
14004 #endif
14005 #ifdef O_TTY_INIT
14006     if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
14007 #endif
14008 #ifdef O_TMPFILE
14009     if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
14010 #endif
14011 #ifdef PRIO_PROCESS
14012     if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
14013 #endif
14014 #ifdef PRIO_PGRP
14015     if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
14016 #endif
14017 #ifdef PRIO_USER
14018     if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
14019 #endif
14020 #ifdef O_CLOEXEC
14021     if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
14022 #endif
14023 #ifdef O_ACCMODE
14024     if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
14025 #endif
14026 
14027 
14028 #ifdef SEEK_HOLE
14029     if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
14030 #endif
14031 #ifdef SEEK_DATA
14032     if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
14033 #endif
14034 
14035 /* MS Windows */
14036 #ifdef O_NOINHERIT
14037     /* Don't inherit in child processes. */
14038     if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
14039 #endif
14040 #ifdef _O_SHORT_LIVED
14041     /* Optimize for short life (keep in memory). */
14042     /* MS forgot to define this one with a non-underscore form too. */
14043     if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
14044 #endif
14045 #ifdef O_TEMPORARY
14046     /* Automatically delete when last handle is closed. */
14047     if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
14048 #endif
14049 #ifdef O_RANDOM
14050     /* Optimize for random access. */
14051     if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
14052 #endif
14053 #ifdef O_SEQUENTIAL
14054     /* Optimize for sequential access. */
14055     if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
14056 #endif
14057 
14058 /* GNU extensions. */
14059 #ifdef O_ASYNC
14060     /* Send a SIGIO signal whenever input or output
14061        becomes available on file descriptor */
14062     if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
14063 #endif
14064 #ifdef O_DIRECT
14065     /* Direct disk access. */
14066     if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
14067 #endif
14068 #ifdef O_DIRECTORY
14069     /* Must be a directory.      */
14070     if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
14071 #endif
14072 #ifdef O_NOFOLLOW
14073     /* Do not follow links.      */
14074     if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
14075 #endif
14076 #ifdef O_NOLINKS
14077     /* Fails if link count of the named file is greater than 1 */
14078     if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
14079 #endif
14080 #ifdef O_NOATIME
14081     /* Do not update the access time. */
14082     if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
14083 #endif
14084 
14085     /* These come from sysexits.h */
14086 #ifdef EX_OK
14087     if (PyModule_AddIntMacro(m, EX_OK)) return -1;
14088 #endif /* EX_OK */
14089 #ifdef EX_USAGE
14090     if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
14091 #endif /* EX_USAGE */
14092 #ifdef EX_DATAERR
14093     if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
14094 #endif /* EX_DATAERR */
14095 #ifdef EX_NOINPUT
14096     if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
14097 #endif /* EX_NOINPUT */
14098 #ifdef EX_NOUSER
14099     if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
14100 #endif /* EX_NOUSER */
14101 #ifdef EX_NOHOST
14102     if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
14103 #endif /* EX_NOHOST */
14104 #ifdef EX_UNAVAILABLE
14105     if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
14106 #endif /* EX_UNAVAILABLE */
14107 #ifdef EX_SOFTWARE
14108     if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
14109 #endif /* EX_SOFTWARE */
14110 #ifdef EX_OSERR
14111     if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
14112 #endif /* EX_OSERR */
14113 #ifdef EX_OSFILE
14114     if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
14115 #endif /* EX_OSFILE */
14116 #ifdef EX_CANTCREAT
14117     if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
14118 #endif /* EX_CANTCREAT */
14119 #ifdef EX_IOERR
14120     if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
14121 #endif /* EX_IOERR */
14122 #ifdef EX_TEMPFAIL
14123     if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
14124 #endif /* EX_TEMPFAIL */
14125 #ifdef EX_PROTOCOL
14126     if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
14127 #endif /* EX_PROTOCOL */
14128 #ifdef EX_NOPERM
14129     if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
14130 #endif /* EX_NOPERM */
14131 #ifdef EX_CONFIG
14132     if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
14133 #endif /* EX_CONFIG */
14134 #ifdef EX_NOTFOUND
14135     if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
14136 #endif /* EX_NOTFOUND */
14137 
14138     /* statvfs */
14139 #ifdef ST_RDONLY
14140     if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
14141 #endif /* ST_RDONLY */
14142 #ifdef ST_NOSUID
14143     if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
14144 #endif /* ST_NOSUID */
14145 
14146        /* GNU extensions */
14147 #ifdef ST_NODEV
14148     if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14149 #endif /* ST_NODEV */
14150 #ifdef ST_NOEXEC
14151     if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14152 #endif /* ST_NOEXEC */
14153 #ifdef ST_SYNCHRONOUS
14154     if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14155 #endif /* ST_SYNCHRONOUS */
14156 #ifdef ST_MANDLOCK
14157     if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14158 #endif /* ST_MANDLOCK */
14159 #ifdef ST_WRITE
14160     if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14161 #endif /* ST_WRITE */
14162 #ifdef ST_APPEND
14163     if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14164 #endif /* ST_APPEND */
14165 #ifdef ST_NOATIME
14166     if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14167 #endif /* ST_NOATIME */
14168 #ifdef ST_NODIRATIME
14169     if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14170 #endif /* ST_NODIRATIME */
14171 #ifdef ST_RELATIME
14172     if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14173 #endif /* ST_RELATIME */
14174 
14175     /* FreeBSD sendfile() constants */
14176 #ifdef SF_NODISKIO
14177     if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
14178 #endif
14179 #ifdef SF_MNOWAIT
14180     if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
14181 #endif
14182 #ifdef SF_SYNC
14183     if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
14184 #endif
14185 
14186     /* constants for posix_fadvise */
14187 #ifdef POSIX_FADV_NORMAL
14188     if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
14189 #endif
14190 #ifdef POSIX_FADV_SEQUENTIAL
14191     if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
14192 #endif
14193 #ifdef POSIX_FADV_RANDOM
14194     if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
14195 #endif
14196 #ifdef POSIX_FADV_NOREUSE
14197     if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
14198 #endif
14199 #ifdef POSIX_FADV_WILLNEED
14200     if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
14201 #endif
14202 #ifdef POSIX_FADV_DONTNEED
14203     if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
14204 #endif
14205 
14206     /* constants for waitid */
14207 #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
14208     if (PyModule_AddIntMacro(m, P_PID)) return -1;
14209     if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14210     if (PyModule_AddIntMacro(m, P_ALL)) return -1;
14211 #endif
14212 #ifdef WEXITED
14213     if (PyModule_AddIntMacro(m, WEXITED)) return -1;
14214 #endif
14215 #ifdef WNOWAIT
14216     if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
14217 #endif
14218 #ifdef WSTOPPED
14219     if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
14220 #endif
14221 #ifdef CLD_EXITED
14222     if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
14223 #endif
14224 #ifdef CLD_DUMPED
14225     if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
14226 #endif
14227 #ifdef CLD_TRAPPED
14228     if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
14229 #endif
14230 #ifdef CLD_CONTINUED
14231     if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
14232 #endif
14233 
14234     /* constants for lockf */
14235 #ifdef F_LOCK
14236     if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
14237 #endif
14238 #ifdef F_TLOCK
14239     if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
14240 #endif
14241 #ifdef F_ULOCK
14242     if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
14243 #endif
14244 #ifdef F_TEST
14245     if (PyModule_AddIntMacro(m, F_TEST)) return -1;
14246 #endif
14247 
14248 #ifdef RWF_DSYNC
14249     if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14250 #endif
14251 #ifdef RWF_HIPRI
14252     if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14253 #endif
14254 #ifdef RWF_SYNC
14255     if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14256 #endif
14257 #ifdef RWF_NOWAIT
14258     if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14259 #endif
14260 
14261 /* constants for posix_spawn */
14262 #ifdef HAVE_POSIX_SPAWN
14263     if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14264     if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14265     if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14266 #endif
14267 
14268 #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
14269     if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14270     if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
14271     if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
14272 #endif
14273 #ifdef HAVE_SPAWNV
14274     if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
14275     if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
14276 #endif
14277 
14278 #ifdef HAVE_SCHED_H
14279 #ifdef SCHED_OTHER
14280     if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
14281 #endif
14282 #ifdef SCHED_FIFO
14283     if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
14284 #endif
14285 #ifdef SCHED_RR
14286     if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
14287 #endif
14288 #ifdef SCHED_SPORADIC
14289     if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
14290 #endif
14291 #ifdef SCHED_BATCH
14292     if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
14293 #endif
14294 #ifdef SCHED_IDLE
14295     if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
14296 #endif
14297 #ifdef SCHED_RESET_ON_FORK
14298     if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
14299 #endif
14300 #ifdef SCHED_SYS
14301     if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
14302 #endif
14303 #ifdef SCHED_IA
14304     if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
14305 #endif
14306 #ifdef SCHED_FSS
14307     if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
14308 #endif
14309 #ifdef SCHED_FX
14310     if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
14311 #endif
14312 #endif
14313 
14314 #ifdef USE_XATTRS
14315     if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14316     if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14317     if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
14318 #endif
14319 
14320 #if HAVE_DECL_RTLD_LAZY
14321     if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
14322 #endif
14323 #if HAVE_DECL_RTLD_NOW
14324     if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
14325 #endif
14326 #if HAVE_DECL_RTLD_GLOBAL
14327     if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
14328 #endif
14329 #if HAVE_DECL_RTLD_LOCAL
14330     if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
14331 #endif
14332 #if HAVE_DECL_RTLD_NODELETE
14333     if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
14334 #endif
14335 #if HAVE_DECL_RTLD_NOLOAD
14336     if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
14337 #endif
14338 #if HAVE_DECL_RTLD_DEEPBIND
14339     if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
14340 #endif
14341 #if HAVE_DECL_RTLD_MEMBER
14342     if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14343 #endif
14344 
14345 #ifdef HAVE_GETRANDOM_SYSCALL
14346     if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14347     if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14348 #endif
14349 #ifdef HAVE_MEMFD_CREATE
14350     if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14351     if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14352 #ifdef MFD_HUGETLB
14353     if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
14354 #endif
14355 #ifdef MFD_HUGE_SHIFT
14356     if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
14357 #endif
14358 #ifdef MFD_HUGE_MASK
14359     if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
14360 #endif
14361 #ifdef MFD_HUGE_64KB
14362     if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
14363 #endif
14364 #ifdef MFD_HUGE_512KB
14365     if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
14366 #endif
14367 #ifdef MFD_HUGE_1MB
14368     if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
14369 #endif
14370 #ifdef MFD_HUGE_2MB
14371     if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
14372 #endif
14373 #ifdef MFD_HUGE_8MB
14374     if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
14375 #endif
14376 #ifdef MFD_HUGE_16MB
14377     if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
14378 #endif
14379 #ifdef MFD_HUGE_32MB
14380     if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
14381 #endif
14382 #ifdef MFD_HUGE_256MB
14383     if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
14384 #endif
14385 #ifdef MFD_HUGE_512MB
14386     if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
14387 #endif
14388 #ifdef MFD_HUGE_1GB
14389     if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
14390 #endif
14391 #ifdef MFD_HUGE_2GB
14392     if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
14393 #endif
14394 #ifdef MFD_HUGE_16GB
14395     if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14396 #endif
14397 #endif
14398 
14399 #if defined(__APPLE__)
14400     if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14401 #endif
14402 
14403 #ifdef MS_WINDOWS
14404     if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14405     if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14406     if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14407     if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14408     if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14409 #endif
14410 
14411     return 0;
14412 }
14413 
14414 
14415 static struct PyModuleDef posixmodule = {
14416     PyModuleDef_HEAD_INIT,
14417     MODNAME,
14418     posix__doc__,
14419     -1,
14420     posix_methods,
14421     NULL,
14422     NULL,
14423     NULL,
14424     NULL
14425 };
14426 
14427 
14428 static const char * const have_functions[] = {
14429 
14430 #ifdef HAVE_FACCESSAT
14431     "HAVE_FACCESSAT",
14432 #endif
14433 
14434 #ifdef HAVE_FCHDIR
14435     "HAVE_FCHDIR",
14436 #endif
14437 
14438 #ifdef HAVE_FCHMOD
14439     "HAVE_FCHMOD",
14440 #endif
14441 
14442 #ifdef HAVE_FCHMODAT
14443     "HAVE_FCHMODAT",
14444 #endif
14445 
14446 #ifdef HAVE_FCHOWN
14447     "HAVE_FCHOWN",
14448 #endif
14449 
14450 #ifdef HAVE_FCHOWNAT
14451     "HAVE_FCHOWNAT",
14452 #endif
14453 
14454 #ifdef HAVE_FEXECVE
14455     "HAVE_FEXECVE",
14456 #endif
14457 
14458 #ifdef HAVE_FDOPENDIR
14459     "HAVE_FDOPENDIR",
14460 #endif
14461 
14462 #ifdef HAVE_FPATHCONF
14463     "HAVE_FPATHCONF",
14464 #endif
14465 
14466 #ifdef HAVE_FSTATAT
14467     "HAVE_FSTATAT",
14468 #endif
14469 
14470 #ifdef HAVE_FSTATVFS
14471     "HAVE_FSTATVFS",
14472 #endif
14473 
14474 #if defined HAVE_FTRUNCATE || defined MS_WINDOWS
14475     "HAVE_FTRUNCATE",
14476 #endif
14477 
14478 #ifdef HAVE_FUTIMENS
14479     "HAVE_FUTIMENS",
14480 #endif
14481 
14482 #ifdef HAVE_FUTIMES
14483     "HAVE_FUTIMES",
14484 #endif
14485 
14486 #ifdef HAVE_FUTIMESAT
14487     "HAVE_FUTIMESAT",
14488 #endif
14489 
14490 #ifdef HAVE_LINKAT
14491     "HAVE_LINKAT",
14492 #endif
14493 
14494 #ifdef HAVE_LCHFLAGS
14495     "HAVE_LCHFLAGS",
14496 #endif
14497 
14498 #ifdef HAVE_LCHMOD
14499     "HAVE_LCHMOD",
14500 #endif
14501 
14502 #ifdef HAVE_LCHOWN
14503     "HAVE_LCHOWN",
14504 #endif
14505 
14506 #ifdef HAVE_LSTAT
14507     "HAVE_LSTAT",
14508 #endif
14509 
14510 #ifdef HAVE_LUTIMES
14511     "HAVE_LUTIMES",
14512 #endif
14513 
14514 #ifdef HAVE_MEMFD_CREATE
14515     "HAVE_MEMFD_CREATE",
14516 #endif
14517 
14518 #ifdef HAVE_MKDIRAT
14519     "HAVE_MKDIRAT",
14520 #endif
14521 
14522 #ifdef HAVE_MKFIFOAT
14523     "HAVE_MKFIFOAT",
14524 #endif
14525 
14526 #ifdef HAVE_MKNODAT
14527     "HAVE_MKNODAT",
14528 #endif
14529 
14530 #ifdef HAVE_OPENAT
14531     "HAVE_OPENAT",
14532 #endif
14533 
14534 #ifdef HAVE_READLINKAT
14535     "HAVE_READLINKAT",
14536 #endif
14537 
14538 #ifdef HAVE_RENAMEAT
14539     "HAVE_RENAMEAT",
14540 #endif
14541 
14542 #ifdef HAVE_SYMLINKAT
14543     "HAVE_SYMLINKAT",
14544 #endif
14545 
14546 #ifdef HAVE_UNLINKAT
14547     "HAVE_UNLINKAT",
14548 #endif
14549 
14550 #ifdef HAVE_UTIMENSAT
14551     "HAVE_UTIMENSAT",
14552 #endif
14553 
14554 #ifdef MS_WINDOWS
14555     "MS_WINDOWS",
14556 #endif
14557 
14558     NULL
14559 };
14560 
14561 
14562 PyMODINIT_FUNC
INITFUNC(void)14563 INITFUNC(void)
14564 {
14565     PyObject *m, *v;
14566     PyObject *list;
14567     const char * const *trace;
14568 
14569     m = PyModule_Create(&posixmodule);
14570     if (m == NULL)
14571         return NULL;
14572 
14573     /* Initialize environ dictionary */
14574     v = convertenviron();
14575     Py_XINCREF(v);
14576     if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14577         return NULL;
14578     Py_DECREF(v);
14579 
14580     if (all_ins(m))
14581         return NULL;
14582 
14583     if (setup_confname_tables(m))
14584         return NULL;
14585 
14586     Py_INCREF(PyExc_OSError);
14587     PyModule_AddObject(m, "error", PyExc_OSError);
14588 
14589 #ifdef HAVE_PUTENV
14590     if (posix_putenv_garbage == NULL)
14591         posix_putenv_garbage = PyDict_New();
14592 #endif
14593 
14594     if (!initialized) {
14595 #if defined(HAVE_WAITID) && !defined(__APPLE__)
14596         waitid_result_desc.name = MODNAME ".waitid_result";
14597         WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14598         if (WaitidResultType == NULL) {
14599             return NULL;
14600         }
14601 #endif
14602 
14603         stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
14604         stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14605         stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14606         stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
14607         StatResultType = PyStructSequence_NewType(&stat_result_desc);
14608         if (StatResultType == NULL) {
14609             return NULL;
14610         }
14611         structseq_new = StatResultType->tp_new;
14612         StatResultType->tp_new = statresult_new;
14613 
14614         statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
14615         StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14616         if (StatVFSResultType == NULL) {
14617             return NULL;
14618         }
14619 #ifdef NEED_TICKS_PER_SECOND
14620 #  if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
14621         ticks_per_second = sysconf(_SC_CLK_TCK);
14622 #  elif defined(HZ)
14623         ticks_per_second = HZ;
14624 #  else
14625         ticks_per_second = 60; /* magic fallback value; may be bogus */
14626 #  endif
14627 #endif
14628 
14629 #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
14630         sched_param_desc.name = MODNAME ".sched_param";
14631         SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14632         if (SchedParamType == NULL) {
14633             return NULL;
14634         }
14635         SchedParamType->tp_new = os_sched_param;
14636 #endif
14637 
14638         /* initialize TerminalSize_info */
14639         TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14640         if (TerminalSizeType == NULL) {
14641             return NULL;
14642         }
14643 
14644         /* initialize scandir types */
14645         if (PyType_Ready(&ScandirIteratorType) < 0)
14646             return NULL;
14647         if (PyType_Ready(&DirEntryType) < 0)
14648             return NULL;
14649     }
14650 #if defined(HAVE_WAITID) && !defined(__APPLE__)
14651     Py_INCREF((PyObject*) WaitidResultType);
14652     PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
14653 #endif
14654     Py_INCREF((PyObject*) StatResultType);
14655     PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14656     Py_INCREF((PyObject*) StatVFSResultType);
14657     PyModule_AddObject(m, "statvfs_result",
14658                        (PyObject*) StatVFSResultType);
14659 
14660 #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
14661     Py_INCREF(SchedParamType);
14662     PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
14663 #endif
14664 
14665     times_result_desc.name = MODNAME ".times_result";
14666     TimesResultType = PyStructSequence_NewType(&times_result_desc);
14667     if (TimesResultType == NULL) {
14668         return NULL;
14669     }
14670     PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
14671 
14672     uname_result_desc.name = MODNAME ".uname_result";
14673     UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14674     if (UnameResultType == NULL) {
14675         return NULL;
14676     }
14677     PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
14678 
14679 #ifdef __APPLE__
14680     /*
14681      * Step 2 of weak-linking support on Mac OS X.
14682      *
14683      * The code below removes functions that are not available on the
14684      * currently active platform.
14685      *
14686      * This block allow one to use a python binary that was build on
14687      * OSX 10.4 on OSX 10.3, without losing access to new APIs on
14688      * OSX 10.4.
14689      */
14690 #ifdef HAVE_FSTATVFS
14691     if (fstatvfs == NULL) {
14692         if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14693             return NULL;
14694         }
14695     }
14696 #endif /* HAVE_FSTATVFS */
14697 
14698 #ifdef HAVE_STATVFS
14699     if (statvfs == NULL) {
14700         if (PyObject_DelAttrString(m, "statvfs") == -1) {
14701             return NULL;
14702         }
14703     }
14704 #endif /* HAVE_STATVFS */
14705 
14706 # ifdef HAVE_LCHOWN
14707     if (lchown == NULL) {
14708         if (PyObject_DelAttrString(m, "lchown") == -1) {
14709             return NULL;
14710         }
14711     }
14712 #endif /* HAVE_LCHOWN */
14713 
14714 
14715 #endif /* __APPLE__ */
14716 
14717     Py_INCREF(TerminalSizeType);
14718     PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
14719 
14720     billion = PyLong_FromLong(1000000000);
14721     if (!billion)
14722         return NULL;
14723 
14724     /* suppress "function not used" warnings */
14725     {
14726     int ignored;
14727     fd_specified("", -1);
14728     follow_symlinks_specified("", 1);
14729     dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14730     dir_fd_converter(Py_None, &ignored);
14731     dir_fd_unavailable(Py_None, &ignored);
14732     }
14733 
14734     /*
14735      * provide list of locally available functions
14736      * so os.py can populate support_* lists
14737      */
14738     list = PyList_New(0);
14739     if (!list)
14740         return NULL;
14741     for (trace = have_functions; *trace; trace++) {
14742         PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14743         if (!unicode)
14744             return NULL;
14745         if (PyList_Append(list, unicode))
14746             return NULL;
14747         Py_DECREF(unicode);
14748     }
14749     PyModule_AddObject(m, "_have_functions", list);
14750 
14751     Py_INCREF((PyObject *) &DirEntryType);
14752     PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
14753 
14754     initialized = 1;
14755 
14756     return m;
14757 }
14758 
14759 #ifdef __cplusplus
14760 }
14761 #endif
14762