Coverage Report

Created: 2025-08-26 06:26

/src/cpython/Modules/posixmodule.c
Line
Count
Source (jump to first uncovered line)
1
/* POSIX module implementation */
2
3
/* This file is also used for Windows NT/MS-Win.  In that case the
4
   module actually calls itself 'nt', not 'posix', and a few
5
   functions are either unimplemented or implemented differently.  The source
6
   assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
7
   of the compiler used.  Different compilers define their own feature
8
   test macro, e.g. '_MSC_VER'. */
9
10
// --- Python includes ------------------------------------------------------
11
12
#include "Python.h"
13
14
#ifdef __VXWORKS__
15
#  include "pycore_bitutils.h"    // _Py_popcount32()
16
#endif
17
#include "pycore_abstract.h"      // _PyNumber_Index()
18
#include "pycore_call.h"          // _PyObject_CallNoArgs()
19
#include "pycore_ceval.h"         // _PyEval_ReInitThreads()
20
#include "pycore_fileutils.h"     // _Py_closerange()
21
#include "pycore_import.h"        // _PyImport_AcquireLock()
22
#include "pycore_initconfig.h"    // _PyStatus_EXCEPTION()
23
#include "pycore_long.h"          // _PyLong_IsNegative()
24
#include "pycore_moduleobject.h"  // _PyModule_GetState()
25
#include "pycore_object.h"        // _PyObject_LookupSpecial()
26
#include "pycore_pylifecycle.h"   // _PyOS_URandom()
27
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
28
#include "pycore_signal.h"        // Py_NSIG
29
#include "pycore_time.h"          // _PyLong_FromTime_t()
30
#include "pycore_typeobject.h"    // _PyType_AddMethod()
31
32
#ifndef MS_WINDOWS
33
#  include "posixmodule.h"        // _PyLong_FromUid()
34
#else
35
#  include "pycore_fileutils_windows.h" // _Py_GetFileInformationByName()
36
#  include "osdefs.h"             // SEP
37
#  include "winreparse.h"         // _Py_REPARSE_DATA_BUFFER
38
#endif
39
40
41
// --- System includes ------------------------------------------------------
42
43
#include <stdio.h>                // ctermid()
44
#include <stdlib.h>               // system()
45
46
#ifdef HAVE_UNISTD_H
47
#  include <unistd.h>             // symlink()
48
#endif
49
50
#ifdef __APPLE__
51
   /* Needed for the implementation of os.statvfs */
52
#  include <sys/param.h>
53
#  include <sys/mount.h>
54
#endif
55
56
#ifdef HAVE_SYS_TIME_H
57
#  include <sys/time.h>           // futimes()
58
#endif
59
60
#ifdef HAVE_SYS_PIDFD_H
61
#  include <sys/pidfd.h>          // PIDFD_NONBLOCK
62
#endif
63
64
#ifdef __EMSCRIPTEN__
65
#  include "emscripten.h"         // emscripten_debugger()
66
#endif
67
68
#ifdef HAVE_SYS_UIO_H
69
#  include <sys/uio.h>
70
#endif
71
72
#ifdef HAVE_SYS_TYPES_H
73
   /* Should be included before <sys/sysmacros.h> on HP-UX v3 */
74
#  include <sys/types.h>
75
#endif
76
#ifdef HAVE_SYS_SYSMACROS_H
77
   /* GNU C Library: major(), minor(), makedev() */
78
#  include <sys/sysmacros.h>
79
#endif
80
81
#ifdef HAVE_SYS_STAT_H
82
#  include <sys/stat.h>
83
#endif
84
85
#ifdef HAVE_SYS_WAIT_H
86
#  include <sys/wait.h>           // WNOHANG
87
#endif
88
89
#ifdef HAVE_LINUX_WAIT_H
90
#  include <linux/wait.h>         // P_PIDFD
91
#endif
92
93
#ifdef HAVE_SIGNAL_H
94
#  include <signal.h>
95
#endif
96
97
#ifdef HAVE_FCNTL_H
98
#  include <fcntl.h>              // fcntl()
99
#endif
100
101
#ifdef HAVE_GRP_H
102
#  include <grp.h>                // setgroups()
103
#endif
104
105
#ifdef HAVE_SYSEXITS_H
106
#  include <sysexits.h>           // EX_OK
107
#endif
108
109
#ifdef HAVE_SYS_LOADAVG_H
110
#  include <sys/loadavg.h>        // getloadavg()
111
#endif
112
113
#ifdef HAVE_SYS_SENDFILE_H
114
#  include <sys/sendfile.h>       // sendfile()
115
#endif
116
117
#if defined(__APPLE__)
118
#  include <copyfile.h>           // fcopyfile()
119
#endif
120
121
#ifdef HAVE_SCHED_H
122
#  include <sched.h>              // sched_setscheduler()
123
#endif
124
#ifdef HAVE_LINUX_SCHED_H
125
#  include <linux/sched.h>        // SCHED_IDLE, SCHED_RR
126
#endif
127
128
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
129
#  ifdef HAVE_SYS_SOCKET_H
130
#    include <sys/socket.h>
131
#  endif
132
#endif
133
134
#ifdef HAVE_DLFCN_H
135
#  include <dlfcn.h>
136
#endif
137
138
#ifdef __hpux
139
#  include <sys/mpctl.h>
140
#endif
141
142
#if defined(__DragonFly__) || \
143
    defined(__OpenBSD__)   || \
144
    defined(__FreeBSD__)   || \
145
    defined(__NetBSD__)    || \
146
    defined(__APPLE__)
147
#  include <sys/sysctl.h>
148
#endif
149
150
#ifdef HAVE_LINUX_RANDOM_H
151
#  include <linux/random.h>       // GRND_RANDOM
152
#endif
153
#ifdef HAVE_GETRANDOM_SYSCALL
154
#  include <sys/syscall.h>        // syscall()
155
#endif
156
157
#ifdef HAVE_POSIX_SPAWN
158
#  include <spawn.h>              // posix_spawn()
159
#endif
160
161
#ifdef HAVE_UTIME_H
162
#  include <utime.h>              // utime()
163
#endif
164
165
#ifdef HAVE_SYS_UTIME_H
166
#  include <sys/utime.h>
167
#  define HAVE_UTIME_H /* pretend we do for the rest of this file */
168
#endif
169
170
#ifdef HAVE_SYS_TIMES_H
171
#  include <sys/times.h>          // times()
172
#endif
173
174
#ifdef HAVE_SYS_PARAM_H
175
#  include <sys/param.h>
176
#endif
177
178
#ifdef HAVE_SYS_UTSNAME_H
179
#  include <sys/utsname.h>        // uname()
180
#endif
181
182
/* memfd_create is either defined in sys/mman.h or sys/memfd.h
183
 * linux/memfd.h defines additional flags
184
 */
185
#ifdef HAVE_SYS_MMAN_H
186
#  include <sys/mman.h>           // memfd_create()
187
#endif
188
#ifdef HAVE_SYS_MEMFD_H
189
#  include <sys/memfd.h>          // memfd_create()
190
#endif
191
#ifdef HAVE_LINUX_MEMFD_H
192
#  include <linux/memfd.h>        // memfd_create(), MFD_CLOEXEC
193
#endif
194
195
#ifdef HAVE_SYS_EVENTFD_H
196
#  include <sys/eventfd.h>        // eventfd()
197
#endif
198
199
#ifdef HAVE_SYS_TIMERFD_H
200
#  include <sys/timerfd.h>        // timerfd_create()
201
#endif
202
203
#ifdef _Py_MEMORY_SANITIZER
204
#  include <sanitizer/msan_interface.h> // __msan_unpoison()
205
#endif
206
207
208
// --- More complex system includes -----------------------------------------
209
210
#ifdef MS_WINDOWS
211
#  include <windows.h>
212
#  if !defined(MS_WINDOWS_GAMES) || defined(MS_WINDOWS_DESKTOP)
213
#    include <pathcch.h>          // PathCchSkipRoot()
214
#  endif
215
#  include <aclapi.h>             // SetEntriesInAcl
216
#  include <lmcons.h>             // UNLEN
217
#  include <sddl.h>               // SDDL_REVISION_1
218
#  include <winioctl.h>           // FSCTL_GET_REPARSE_POINT
219
#  if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
220
#    define HAVE_SYMLINK
221
#  endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_SYSTEM */
222
#endif
223
224
225
#ifdef _MSC_VER
226
#  ifdef HAVE_DIRECT_H
227
#    include <direct.h>
228
#  endif
229
#  ifdef HAVE_IO_H
230
#    include <io.h>
231
#  endif
232
#  ifdef HAVE_PROCESS_H
233
#    include <process.h>          // getpid(), _cwait()
234
#  endif
235
#  include <malloc.h>
236
#endif /* _MSC_VER */
237
238
239
#ifdef HAVE__GETPTY
240
#  include <sys/types.h>          // mode_t
241
   // SGI apparently needs this forward declaration
242
   extern char * _getpty(int *, int, mode_t, int);
243
#endif
244
245
246
#if defined(HAVE_SYS_XATTR_H)
247
#  if defined(HAVE_LINUX_LIMITS_H) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
248
#    define USE_XATTRS
249
#    include <linux/limits.h>  // Needed for XATTR_SIZE_MAX on musl libc.
250
#  endif
251
#  if defined(__CYGWIN__)
252
#    define USE_XATTRS
253
#    include <cygwin/limits.h>  // Needed for XATTR_SIZE_MAX and XATTR_LIST_MAX.
254
#  endif
255
#endif
256
#ifdef USE_XATTRS
257
#  include <sys/xattr.h>          // fgetxattr()
258
#endif
259
260
261
#ifdef HAVE_WINDOWS_CONSOLE_IO
262
#  define TERMSIZE_USE_CONIO
263
#elif defined(HAVE_SYS_IOCTL_H)
264
#  include <sys/ioctl.h>          // ioctl(), TIOCGWINSZ
265
#  if defined(HAVE_TERMIOS_H)
266
#    include <termios.h>
267
#  endif
268
#  if defined(TIOCGWINSZ)
269
#    define TERMSIZE_USE_IOCTL
270
#  endif
271
#endif
272
273
274
/* Various compilers have only certain posix functions */
275
/* XXX Gosh I wish these were all moved into pyconfig.h */
276
#if defined(__WATCOMC__) && !defined(__QNX__)           /* Watcom compiler */
277
#  define HAVE_OPENDIR    1
278
#  define HAVE_SYSTEM     1
279
#  include <process.h>
280
#elif defined( _MSC_VER)
281
  /* Microsoft compiler */
282
#  if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
283
#    define HAVE_GETPPID    1
284
#  endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_APP | MS_WINDOWS_SYSTEM */
285
#  if defined(MS_WINDOWS_DESKTOP)
286
#    define HAVE_GETLOGIN   1
287
#  endif /* MS_WINDOWS_DESKTOP */
288
#  if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
289
#    define HAVE_SPAWNV     1
290
#    define HAVE_EXECV      1
291
#    define HAVE_WSPAWNV    1
292
#    define HAVE_WEXECV     1
293
#    define HAVE_SYSTEM     1
294
#    define HAVE_CWAIT      1
295
#  endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_SYSTEM */
296
#  define HAVE_PIPE       1
297
#  define HAVE_FSYNC      1
298
#  define fsync _commit
299
#endif
300
301
302
#if !defined(_MSC_VER) && defined(__sgi) && _COMPILER_VERSION>=700
303
/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
304
   (default) */
305
extern char *ctermid_r(char *);
306
#endif
307
308
309
#if defined(__VXWORKS__)
310
#  include <vxCpuLib.h>
311
#  include <rtpLib.h>
312
#  include <wait.h>
313
#  include <taskLib.h>
314
#  ifndef _P_WAIT
315
#    define _P_WAIT          0
316
#    define _P_NOWAIT        1
317
#    define _P_NOWAITO       1
318
#  endif
319
#endif /* __VXWORKS__ */
320
321
322
#ifdef HAVE_DIRENT_H
323
#  include <dirent.h>             // opendir()
324
7.05k
#  define NAMLEN(dirent) strlen((dirent)->d_name)
325
#else
326
#  if defined(__WATCOMC__) && !defined(__QNX__)
327
#    include <direct.h>
328
#    define NAMLEN(dirent) strlen((dirent)->d_name)
329
#  else
330
#    define dirent direct
331
#    define NAMLEN(dirent) (dirent)->d_namlen
332
#  endif
333
#  ifdef HAVE_SYS_NDIR_H
334
#    include <sys/ndir.h>
335
#  endif
336
#  ifdef HAVE_SYS_DIR_H
337
#    include <sys/dir.h>
338
#  endif
339
#  ifdef HAVE_NDIR_H
340
#    include <ndir.h>
341
#  endif
342
#endif
343
344
345
#if defined(MAJOR_IN_MKDEV)
346
#  include <sys/mkdev.h>
347
#else
348
#  if defined(MAJOR_IN_SYSMACROS)
349
#    include <sys/sysmacros.h>
350
#  endif
351
#  if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
352
#    include <sys/mkdev.h>
353
#  endif
354
#endif
355
356
357
// --- Macros ---------------------------------------------------------------
358
359
#ifndef MAXPATHLEN
360
#  if defined(PATH_MAX) && PATH_MAX > 1024
361
#    define MAXPATHLEN PATH_MAX
362
#  else
363
#    define MAXPATHLEN 1024
364
#  endif
365
#endif /* MAXPATHLEN */
366
367
368
#ifdef UNION_WAIT
369
   /* Emulate some macros on systems that have a union instead of macros */
370
#  ifndef WIFEXITED
371
#    define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
372
#  endif
373
#  ifndef WEXITSTATUS
374
#    define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
375
#  endif
376
#  ifndef WTERMSIG
377
#    define WTERMSIG(u_wait) ((u_wait).w_termsig)
378
#  endif
379
#  define WAIT_TYPE union wait
380
#  define WAIT_STATUS_INT(s) (s.w_status)
381
#else
382
   /* !UNION_WAIT */
383
0
#  define WAIT_TYPE int
384
0
#  define WAIT_STATUS_INT(s) (s)
385
#endif /* UNION_WAIT */
386
387
388
/* Don't use the "_r" form if we don't need it (also, won't have a
389
   prototype for it, at least on Solaris -- maybe others as well?). */
390
#if defined(HAVE_CTERMID_R)
391
#  define USE_CTERMID_R
392
#endif
393
394
395
/* choose the appropriate stat and fstat functions and return structs */
396
#undef STAT
397
#undef FSTAT
398
#undef STRUCT_STAT
399
#ifdef MS_WINDOWS
400
#  define STAT win32_stat
401
#  define LSTAT win32_lstat
402
#  define FSTAT _Py_fstat_noraise
403
#  define STRUCT_STAT struct _Py_stat_struct
404
#else
405
5.00k
#  define STAT stat
406
0
#  define LSTAT lstat
407
0
#  define FSTAT fstat
408
5.00k
#  define STRUCT_STAT struct stat
409
#endif
410
411
412
#if !defined(EX_OK) && defined(EXIT_SUCCESS)
413
#  define EX_OK EXIT_SUCCESS
414
#endif
415
416
#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
417
#  undef HAVE_SCHED_SETAFFINITY
418
#endif
419
420
/* On android API level 21, 'AT_EACCESS' is not declared although
421
 * HAVE_FACCESSAT is defined. */
422
#ifdef __ANDROID__
423
#  undef HAVE_FACCESSAT
424
#endif
425
426
#if defined(__sun)
427
/* Something to implement in autoconf, not present in autoconf 2.69 */
428
#  define HAVE_STRUCT_STAT_ST_FSTYPE 1
429
#endif
430
431
432
// --- Apple __builtin_available() macros -----------------------------------
433
434
/*
435
 * A number of APIs are available on macOS from a certain macOS version.
436
 * To support building with a new SDK while deploying to older versions
437
 * the availability test is split into two:
438
 *   - HAVE_<FUNCTION>:  The configure check for compile time availability
439
 *   - HAVE_<FUNCTION>_RUNTIME: Runtime check for availability
440
 *
441
 * The latter is always true when not on macOS, or when using a compiler
442
 * that does not support __has_builtin (older versions of Xcode).
443
 *
444
 * Due to compiler restrictions there is one valid use of HAVE_<FUNCTION>_RUNTIME:
445
 *    if (HAVE_<FUNCTION>_RUNTIME) { ... }
446
 *
447
 * In mixing the test with other tests or using negations will result in compile
448
 * errors.
449
 */
450
#if defined(__APPLE__)
451
452
#include <mach/mach.h>
453
454
#if defined(__has_builtin)
455
#if __has_builtin(__builtin_available)
456
#define HAVE_BUILTIN_AVAILABLE 1
457
#endif
458
#endif
459
460
#ifdef HAVE_BUILTIN_AVAILABLE
461
#  define HAVE_FSTATAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
462
#  define HAVE_FACCESSAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
463
#  define HAVE_FCHMODAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
464
#  define HAVE_FCHOWNAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
465
#  define HAVE_LINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
466
#  define HAVE_FDOPENDIR_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
467
#  define HAVE_MKDIRAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
468
#  define HAVE_RENAMEAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
469
#  define HAVE_UNLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
470
#  define HAVE_OPENAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
471
#  define HAVE_READLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
472
#  define HAVE_SYMLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
473
#  define HAVE_FUTIMENS_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
474
#  define HAVE_UTIMENSAT_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
475
#  define HAVE_PWRITEV_RUNTIME __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
476
#  define HAVE_MKFIFOAT_RUNTIME __builtin_available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
477
#  define HAVE_MKNODAT_RUNTIME __builtin_available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
478
#  define HAVE_PTSNAME_R_RUNTIME __builtin_available(macOS 10.13.4, iOS 11.3, tvOS 11.3, watchOS 4.3, *)
479
480
#  define HAVE_POSIX_SPAWN_SETSID_RUNTIME __builtin_available(macOS 10.15, *)
481
482
#else /* Xcode 8 or earlier */
483
484
   /* __builtin_available is not present in these compilers, but
485
    * some of the symbols might be weak linked (10.10 SDK or later
486
    * deploying on 10.9.
487
    *
488
    * Fall back to the older style of availability checking for
489
    * symbols introduced in macOS 10.10.
490
    */
491
492
#  ifdef HAVE_FSTATAT
493
#    define HAVE_FSTATAT_RUNTIME (fstatat != NULL)
494
#  endif
495
496
#  ifdef HAVE_FACCESSAT
497
#    define HAVE_FACCESSAT_RUNTIME (faccessat != NULL)
498
#  endif
499
500
#  ifdef HAVE_FCHMODAT
501
#    define HAVE_FCHMODAT_RUNTIME (fchmodat != NULL)
502
#  endif
503
504
#  ifdef HAVE_FCHOWNAT
505
#    define HAVE_FCHOWNAT_RUNTIME (fchownat != NULL)
506
#  endif
507
508
#  ifdef HAVE_LINKAT
509
#    define HAVE_LINKAT_RUNTIME (linkat != NULL)
510
#  endif
511
512
#  ifdef HAVE_FDOPENDIR
513
#    define HAVE_FDOPENDIR_RUNTIME (fdopendir != NULL)
514
#  endif
515
516
#  ifdef HAVE_MKDIRAT
517
#    define HAVE_MKDIRAT_RUNTIME (mkdirat != NULL)
518
#  endif
519
520
#  ifdef HAVE_RENAMEAT
521
#    define HAVE_RENAMEAT_RUNTIME (renameat != NULL)
522
#  endif
523
524
#  ifdef HAVE_UNLINKAT
525
#    define HAVE_UNLINKAT_RUNTIME (unlinkat != NULL)
526
#  endif
527
528
#  ifdef HAVE_OPENAT
529
#    define HAVE_OPENAT_RUNTIME (openat != NULL)
530
#  endif
531
532
#  ifdef HAVE_READLINKAT
533
#    define HAVE_READLINKAT_RUNTIME (readlinkat != NULL)
534
#  endif
535
536
#  ifdef HAVE_SYMLINKAT
537
#    define HAVE_SYMLINKAT_RUNTIME (symlinkat != NULL)
538
#  endif
539
540
#  ifdef HAVE_UTIMENSAT
541
#    define HAVE_UTIMENSAT_RUNTIME (utimensat != NULL)
542
#  endif
543
544
#  ifdef HAVE_FUTIMENS
545
#    define HAVE_FUTIMENS_RUNTIME (futimens != NULL)
546
#  endif
547
548
#  ifdef HAVE_PWRITEV
549
#    define HAVE_PWRITEV_RUNTIME (pwritev != NULL)
550
#  endif
551
552
#  ifdef HAVE_MKFIFOAT
553
#    define HAVE_MKFIFOAT_RUNTIME (mkfifoat != NULL)
554
#  endif
555
556
#  ifdef HAVE_MKNODAT
557
#    define HAVE_MKNODAT_RUNTIME (mknodat != NULL)
558
#  endif
559
560
#  ifdef HAVE_PTSNAME_R
561
#    define HAVE_PTSNAME_R_RUNTIME (ptsname_r != NULL)
562
#  endif
563
564
#endif
565
566
#ifdef HAVE_FUTIMESAT
567
/* Some of the logic for weak linking depends on this assertion */
568
# error "HAVE_FUTIMESAT unexpectedly defined"
569
#endif
570
571
#else
572
0
#  define HAVE_FSTATAT_RUNTIME 1
573
0
#  define HAVE_FACCESSAT_RUNTIME 1
574
0
#  define HAVE_FCHMODAT_RUNTIME 1
575
0
#  define HAVE_FCHOWNAT_RUNTIME 1
576
#ifdef __wasi__
577
#  define HAVE_LINKAT_RUNTIME 0
578
# else
579
0
#  define HAVE_LINKAT_RUNTIME 1
580
# endif
581
0
#  define HAVE_FDOPENDIR_RUNTIME 1
582
0
#  define HAVE_MKDIRAT_RUNTIME 1
583
0
#  define HAVE_RENAMEAT_RUNTIME 1
584
0
#  define HAVE_UNLINKAT_RUNTIME 1
585
0
#  define HAVE_OPENAT_RUNTIME 1
586
0
#  define HAVE_READLINKAT_RUNTIME 1
587
0
#  define HAVE_SYMLINKAT_RUNTIME 1
588
0
#  define HAVE_FUTIMENS_RUNTIME 1
589
0
#  define HAVE_UTIMENSAT_RUNTIME 1
590
16
#  define HAVE_PWRITEV_RUNTIME 1
591
0
#  define HAVE_MKFIFOAT_RUNTIME 1
592
0
#  define HAVE_MKNODAT_RUNTIME 1
593
0
#  define HAVE_PTSNAME_R_RUNTIME 1
594
#endif
595
596
597
// --- os module ------------------------------------------------------------
598
599
#ifdef MS_WINDOWS
600
#  define INITFUNC PyInit_nt
601
#  define MODNAME "nt"
602
#  define MODNAME_OBJ &_Py_ID(nt)
603
#else
604
#  define INITFUNC PyInit_posix
605
48
#  define MODNAME "posix"
606
0
#  define MODNAME_OBJ &_Py_ID(posix)
607
#endif
608
609
/*[clinic input]
610
# one of the few times we lie about this name!
611
module os
612
[clinic start generated code]*/
613
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
614
615
PyDoc_STRVAR(posix__doc__,
616
"This module provides access to operating system functionality that is\n\
617
standardized by the C Standard and the POSIX standard (a thinly\n\
618
disguised Unix interface).  Refer to the library manual and\n\
619
corresponding Unix manual entries for more information on calls.");
620
621
622
// --- Functions ------------------------------------------------------------
623
624
#ifdef HAVE_FORK
625
static void
626
run_at_forkers(PyObject *lst, int reverse)
627
0
{
628
0
    Py_ssize_t i;
629
0
    PyObject *cpy;
630
631
0
    if (lst != NULL) {
632
0
        assert(PyList_CheckExact(lst));
633
634
        /* Use a list copy in case register_at_fork() is called from
635
         * one of the callbacks.
636
         */
637
0
        cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
638
0
        if (cpy == NULL) {
639
0
            PyErr_FormatUnraisable("Exception ignored in atfork callback "
640
0
                                   "while copying list %R", lst);
641
0
        }
642
0
        else {
643
0
            if (reverse)
644
0
                PyList_Reverse(cpy);
645
0
            for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
646
0
                PyObject *func, *res;
647
0
                func = PyList_GET_ITEM(cpy, i);
648
0
                res = _PyObject_CallNoArgs(func);
649
0
                if (res == NULL) {
650
0
                    PyErr_FormatUnraisable("Exception ignored "
651
0
                                           "in atfork callback %R", func);
652
0
                }
653
0
                else {
654
0
                    Py_DECREF(res);
655
0
                }
656
0
            }
657
0
            Py_DECREF(cpy);
658
0
        }
659
0
    }
660
0
}
661
662
void
663
PyOS_BeforeFork(void)
664
0
{
665
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
666
0
    run_at_forkers(interp->before_forkers, 1);
667
668
0
    _PyImport_AcquireLock(interp);
669
0
    _PyEval_StopTheWorldAll(&_PyRuntime);
670
0
    HEAD_LOCK(&_PyRuntime);
671
0
}
672
673
void
674
PyOS_AfterFork_Parent(void)
675
0
{
676
0
    HEAD_UNLOCK(&_PyRuntime);
677
0
    _PyEval_StartTheWorldAll(&_PyRuntime);
678
679
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
680
0
    _PyImport_ReleaseLock(interp);
681
0
    run_at_forkers(interp->after_forkers_parent, 0);
682
0
}
683
684
static void
685
reset_remotedebug_data(PyThreadState *tstate)
686
0
{
687
0
    tstate->remote_debugger_support.debugger_pending_call = 0;
688
0
    memset(tstate->remote_debugger_support.debugger_script_path, 0,
689
0
           Py_MAX_SCRIPT_PATH_SIZE);
690
0
}
691
692
693
void
694
PyOS_AfterFork_Child(void)
695
0
{
696
0
    PyStatus status;
697
0
    _PyRuntimeState *runtime = &_PyRuntime;
698
699
    // re-creates runtime->interpreters.mutex (HEAD_UNLOCK)
700
0
    status = _PyRuntimeState_ReInitThreads(runtime);
701
0
    if (_PyStatus_EXCEPTION(status)) {
702
0
        goto fatal_error;
703
0
    }
704
705
0
    PyThreadState *tstate = _PyThreadState_GET();
706
0
    _Py_EnsureTstateNotNULL(tstate);
707
708
0
    assert(tstate->thread_id == PyThread_get_thread_ident());
709
0
#ifdef PY_HAVE_THREAD_NATIVE_ID
710
0
    tstate->native_thread_id = PyThread_get_thread_native_id();
711
0
#endif
712
713
#ifdef Py_GIL_DISABLED
714
    _Py_brc_after_fork(tstate->interp);
715
    _Py_qsbr_after_fork((_PyThreadStateImpl *)tstate);
716
#endif
717
718
    // Ideally we could guarantee tstate is running main.
719
0
    _PyInterpreterState_ReinitRunningMain(tstate);
720
721
0
    status = _PyEval_ReInitThreads(tstate);
722
0
    if (_PyStatus_EXCEPTION(status)) {
723
0
        goto fatal_error;
724
0
    }
725
726
0
    reset_remotedebug_data(tstate);
727
728
    // Remove the dead thread states. We "start the world" once we are the only
729
    // thread state left to undo the stop the world call in `PyOS_BeforeFork`.
730
    // That needs to happen before `_PyThreadState_DeleteList`, because that
731
    // may call destructors.
732
0
    PyThreadState *list = _PyThreadState_RemoveExcept(tstate);
733
0
    _PyEval_StartTheWorldAll(&_PyRuntime);
734
0
    _PyThreadState_DeleteList(list, /*is_after_fork=*/1);
735
736
0
    _PyImport_ReInitLock(tstate->interp);
737
0
    _PyImport_ReleaseLock(tstate->interp);
738
739
0
    _PySignal_AfterFork();
740
741
0
    status = _PyInterpreterState_DeleteExceptMain(runtime);
742
0
    if (_PyStatus_EXCEPTION(status)) {
743
0
        goto fatal_error;
744
0
    }
745
0
    assert(_PyThreadState_GET() == tstate);
746
747
0
    status = _PyPerfTrampoline_AfterFork_Child();
748
0
    if (_PyStatus_EXCEPTION(status)) {
749
0
        goto fatal_error;
750
0
    }
751
752
0
    run_at_forkers(tstate->interp->after_forkers_child, 0);
753
0
    return;
754
755
0
fatal_error:
756
0
    Py_ExitStatusException(status);
757
0
}
758
759
static int
760
register_at_forker(PyObject **lst, PyObject *func)
761
6
{
762
6
    if (func == NULL)  /* nothing to register? do nothing. */
763
4
        return 0;
764
2
    if (*lst == NULL) {
765
2
        *lst = PyList_New(0);
766
2
        if (*lst == NULL)
767
0
            return -1;
768
2
    }
769
2
    return PyList_Append(*lst, func);
770
2
}
771
#endif  /* HAVE_FORK */
772
773
774
/* Legacy wrapper */
775
void
776
PyOS_AfterFork(void)
777
0
{
778
0
#ifdef HAVE_FORK
779
0
    PyOS_AfterFork_Child();
780
0
#endif
781
0
}
782
783
784
#ifdef MS_WINDOWS
785
/* defined in fileutils.c */
786
void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
787
void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, ULONG,
788
                                FILE_BASIC_INFO *, FILE_ID_INFO *,
789
                                struct _Py_stat_struct *);
790
void _Py_stat_basic_info_to_stat(FILE_STAT_BASIC_INFORMATION *,
791
                                 struct _Py_stat_struct *);
792
#endif
793
794
795
#ifndef MS_WINDOWS
796
PyObject *
797
_PyLong_FromUid(uid_t uid)
798
4.72k
{
799
4.72k
    if (uid == (uid_t)-1)
800
0
        return PyLong_FromLong(-1);
801
4.72k
    return PyLong_FromUnsignedLong(uid);
802
4.72k
}
803
804
PyObject *
805
_PyLong_FromGid(gid_t gid)
806
4.72k
{
807
4.72k
    if (gid == (gid_t)-1)
808
0
        return PyLong_FromLong(-1);
809
4.72k
    return PyLong_FromUnsignedLong(gid);
810
4.72k
}
811
812
int
813
_Py_Uid_Converter(PyObject *obj, uid_t *p)
814
0
{
815
0
    uid_t uid;
816
0
    PyObject *index;
817
0
    int overflow;
818
0
    long result;
819
0
    unsigned long uresult;
820
821
0
    index = _PyNumber_Index(obj);
822
0
    if (index == NULL) {
823
0
        PyErr_Format(PyExc_TypeError,
824
0
                     "uid should be integer, not %.200s",
825
0
                     _PyType_Name(Py_TYPE(obj)));
826
0
        return 0;
827
0
    }
828
829
    /*
830
     * Handling uid_t is complicated for two reasons:
831
     *  * Although uid_t is (always?) unsigned, it still
832
     *    accepts -1.
833
     *  * We don't know its size in advance--it may be
834
     *    bigger than an int, or it may be smaller than
835
     *    a long.
836
     *
837
     * So a bit of defensive programming is in order.
838
     * Start with interpreting the value passed
839
     * in as a signed long and see if it works.
840
     */
841
842
0
    result = PyLong_AsLongAndOverflow(index, &overflow);
843
844
0
    if (!overflow) {
845
0
        uid = (uid_t)result;
846
847
0
        if (result == -1) {
848
0
            if (PyErr_Occurred())
849
0
                goto fail;
850
            /* It's a legitimate -1, we're done. */
851
0
            goto success;
852
0
        }
853
854
        /* Any other negative number is disallowed. */
855
0
        if (result < 0)
856
0
            goto underflow;
857
858
        /* Ensure the value wasn't truncated. */
859
0
        if (sizeof(uid_t) < sizeof(long) &&
860
0
            (long)uid != result)
861
0
            goto underflow;
862
0
        goto success;
863
0
    }
864
865
0
    if (overflow < 0)
866
0
        goto underflow;
867
868
    /*
869
     * Okay, the value overflowed a signed long.  If it
870
     * fits in an *unsigned* long, it may still be okay,
871
     * as uid_t may be unsigned long on this platform.
872
     */
873
0
    uresult = PyLong_AsUnsignedLong(index);
874
0
    if (PyErr_Occurred()) {
875
0
        if (PyErr_ExceptionMatches(PyExc_OverflowError))
876
0
            goto overflow;
877
0
        goto fail;
878
0
    }
879
880
0
    uid = (uid_t)uresult;
881
882
    /*
883
     * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
884
     * but this value would get interpreted as (uid_t)-1  by chown
885
     * and its siblings.   That's not what the user meant!  So we
886
     * throw an overflow exception instead.   (We already
887
     * handled a real -1 with PyLong_AsLongAndOverflow() above.)
888
     */
889
0
    if (uid == (uid_t)-1)
890
0
        goto overflow;
891
892
    /* Ensure the value wasn't truncated. */
893
0
    if (sizeof(uid_t) < sizeof(long) &&
894
0
        (unsigned long)uid != uresult)
895
0
        goto overflow;
896
    /* fallthrough */
897
898
0
success:
899
0
    Py_DECREF(index);
900
0
    *p = uid;
901
0
    return 1;
902
903
0
underflow:
904
0
    PyErr_SetString(PyExc_OverflowError,
905
0
                    "uid is less than minimum");
906
0
    goto fail;
907
908
0
overflow:
909
0
    PyErr_SetString(PyExc_OverflowError,
910
0
                    "uid is greater than maximum");
911
    /* fallthrough */
912
913
0
fail:
914
0
    Py_DECREF(index);
915
0
    return 0;
916
0
}
917
918
int
919
_Py_Gid_Converter(PyObject *obj, gid_t *p)
920
0
{
921
0
    gid_t gid;
922
0
    PyObject *index;
923
0
    int overflow;
924
0
    long result;
925
0
    unsigned long uresult;
926
927
0
    index = _PyNumber_Index(obj);
928
0
    if (index == NULL) {
929
0
        PyErr_Format(PyExc_TypeError,
930
0
                     "gid should be integer, not %.200s",
931
0
                     _PyType_Name(Py_TYPE(obj)));
932
0
        return 0;
933
0
    }
934
935
    /*
936
     * Handling gid_t is complicated for two reasons:
937
     *  * Although gid_t is (always?) unsigned, it still
938
     *    accepts -1.
939
     *  * We don't know its size in advance--it may be
940
     *    bigger than an int, or it may be smaller than
941
     *    a long.
942
     *
943
     * So a bit of defensive programming is in order.
944
     * Start with interpreting the value passed
945
     * in as a signed long and see if it works.
946
     */
947
948
0
    result = PyLong_AsLongAndOverflow(index, &overflow);
949
950
0
    if (!overflow) {
951
0
        gid = (gid_t)result;
952
953
0
        if (result == -1) {
954
0
            if (PyErr_Occurred())
955
0
                goto fail;
956
            /* It's a legitimate -1, we're done. */
957
0
            goto success;
958
0
        }
959
960
        /* Any other negative number is disallowed. */
961
0
        if (result < 0) {
962
0
            goto underflow;
963
0
        }
964
965
        /* Ensure the value wasn't truncated. */
966
0
        if (sizeof(gid_t) < sizeof(long) &&
967
0
            (long)gid != result)
968
0
            goto underflow;
969
0
        goto success;
970
0
    }
971
972
0
    if (overflow < 0)
973
0
        goto underflow;
974
975
    /*
976
     * Okay, the value overflowed a signed long.  If it
977
     * fits in an *unsigned* long, it may still be okay,
978
     * as gid_t may be unsigned long on this platform.
979
     */
980
0
    uresult = PyLong_AsUnsignedLong(index);
981
0
    if (PyErr_Occurred()) {
982
0
        if (PyErr_ExceptionMatches(PyExc_OverflowError))
983
0
            goto overflow;
984
0
        goto fail;
985
0
    }
986
987
0
    gid = (gid_t)uresult;
988
989
    /*
990
     * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
991
     * but this value would get interpreted as (gid_t)-1  by chown
992
     * and its siblings.   That's not what the user meant!  So we
993
     * throw an overflow exception instead.   (We already
994
     * handled a real -1 with PyLong_AsLongAndOverflow() above.)
995
     */
996
0
    if (gid == (gid_t)-1)
997
0
        goto overflow;
998
999
    /* Ensure the value wasn't truncated. */
1000
0
    if (sizeof(gid_t) < sizeof(long) &&
1001
0
        (unsigned long)gid != uresult)
1002
0
        goto overflow;
1003
    /* fallthrough */
1004
1005
0
success:
1006
0
    Py_DECREF(index);
1007
0
    *p = gid;
1008
0
    return 1;
1009
1010
0
underflow:
1011
0
    PyErr_SetString(PyExc_OverflowError,
1012
0
                    "gid is less than minimum");
1013
0
    goto fail;
1014
1015
0
overflow:
1016
0
    PyErr_SetString(PyExc_OverflowError,
1017
0
                    "gid is greater than maximum");
1018
    /* fallthrough */
1019
1020
0
fail:
1021
0
    Py_DECREF(index);
1022
0
    return 0;
1023
0
}
1024
#endif /* MS_WINDOWS */
1025
1026
1027
static PyObject *
1028
_PyLong_FromDev(dev_t dev)
1029
4.69k
{
1030
4.69k
#ifdef NODEV
1031
4.69k
    if (dev == NODEV) {
1032
0
        return PyLong_FromLongLong((long long)dev);
1033
0
    }
1034
4.69k
#endif
1035
4.69k
    return PyLong_FromUnsignedLongLong((unsigned long long)dev);
1036
4.69k
}
1037
1038
1039
#if (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)) || defined(HAVE_DEVICE_MACROS)
1040
static int
1041
_Py_Dev_Converter(PyObject *obj, void *p)
1042
0
{
1043
0
    if (!PyLong_Check(obj)) {
1044
0
        obj = _PyNumber_Index(obj);
1045
0
        if (obj == NULL) {
1046
0
            return 0;
1047
0
        }
1048
0
    }
1049
0
    else {
1050
0
        Py_INCREF(obj);
1051
0
    }
1052
0
    assert(PyLong_Check(obj));
1053
0
#ifdef NODEV
1054
0
    if (_PyLong_IsNegative((PyLongObject *)obj)) {
1055
0
        int overflow;
1056
0
        long long result = PyLong_AsLongLongAndOverflow(obj, &overflow);
1057
0
        if (result == -1 && PyErr_Occurred()) {
1058
0
            Py_DECREF(obj);
1059
0
            return 0;
1060
0
        }
1061
0
        if (!overflow && result == (long long)NODEV) {
1062
0
            *((dev_t *)p) = NODEV;
1063
0
            Py_DECREF(obj);
1064
0
            return 1;
1065
0
        }
1066
0
    }
1067
0
#endif
1068
1069
0
    unsigned long long result = PyLong_AsUnsignedLongLong(obj);
1070
0
    Py_DECREF(obj);
1071
0
    if (result == (unsigned long long)-1 && PyErr_Occurred()) {
1072
0
        return 0;
1073
0
    }
1074
0
    if ((unsigned long long)(dev_t)result != result) {
1075
0
        PyErr_SetString(PyExc_OverflowError,
1076
0
                        "Python int too large to convert to C dev_t");
1077
0
        return 0;
1078
0
    }
1079
0
    *((dev_t *)p) = (dev_t)result;
1080
0
    return 1;
1081
0
}
1082
#endif /* (HAVE_MKNOD && HAVE_MAKEDEV) || HAVE_DEVICE_MACROS */
1083
1084
1085
#ifdef AT_FDCWD
1086
/*
1087
 * Why the (int) cast?  Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
1088
 * without the int cast, the value gets interpreted as uint (4291925331),
1089
 * which doesn't play nicely with all the initializer lines in this file that
1090
 * look like this:
1091
 *      int dir_fd = DEFAULT_DIR_FD;
1092
 */
1093
21.9k
#define DEFAULT_DIR_FD (int)AT_FDCWD
1094
#else
1095
#define DEFAULT_DIR_FD (-100)
1096
#endif
1097
1098
static int
1099
_fd_converter(PyObject *o, int *p)
1100
0
{
1101
0
    int overflow;
1102
0
    long long_value;
1103
1104
0
    if (PyBool_Check(o)) {
1105
0
        if (PyErr_WarnEx(PyExc_RuntimeWarning,
1106
0
                "bool is used as a file descriptor", 1))
1107
0
        {
1108
0
            return 0;
1109
0
        }
1110
0
    }
1111
0
    PyObject *index = _PyNumber_Index(o);
1112
0
    if (index == NULL) {
1113
0
        return 0;
1114
0
    }
1115
1116
0
    assert(PyLong_Check(index));
1117
0
    long_value = PyLong_AsLongAndOverflow(index, &overflow);
1118
0
    Py_DECREF(index);
1119
0
    assert(!PyErr_Occurred());
1120
0
    if (overflow > 0 || long_value > INT_MAX) {
1121
0
        PyErr_SetString(PyExc_OverflowError,
1122
0
                        "fd is greater than maximum");
1123
0
        return 0;
1124
0
    }
1125
0
    if (overflow < 0 || long_value < INT_MIN) {
1126
0
        PyErr_SetString(PyExc_OverflowError,
1127
0
                        "fd is less than minimum");
1128
0
        return 0;
1129
0
    }
1130
1131
0
    *p = (int)long_value;
1132
0
    return 1;
1133
0
}
1134
1135
static int
1136
dir_fd_converter(PyObject *o, void *p)
1137
32
{
1138
32
    if (o == Py_None) {
1139
32
        *(int *)p = DEFAULT_DIR_FD;
1140
32
        return 1;
1141
32
    }
1142
0
    else if (PyIndex_Check(o)) {
1143
0
        return _fd_converter(o, (int *)p);
1144
0
    }
1145
0
    else {
1146
0
        PyErr_Format(PyExc_TypeError,
1147
0
                     "argument should be integer or None, not %.200s",
1148
0
                     _PyType_Name(Py_TYPE(o)));
1149
0
        return 0;
1150
0
    }
1151
32
}
1152
1153
typedef struct {
1154
    PyObject *billion;
1155
    PyObject *DirEntryType;
1156
    PyObject *ScandirIteratorType;
1157
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
1158
    PyObject *SchedParamType;
1159
#endif
1160
    newfunc statresult_new_orig;
1161
    PyObject *StatResultType;
1162
    PyObject *StatVFSResultType;
1163
    PyObject *TerminalSizeType;
1164
    PyObject *TimesResultType;
1165
    PyObject *UnameResultType;
1166
#if defined(HAVE_WAITID)
1167
    PyObject *WaitidResultType;
1168
#endif
1169
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
1170
    PyObject *struct_rusage;
1171
#endif
1172
    PyObject *st_mode;
1173
#ifndef MS_WINDOWS
1174
    // times() clock frequency in hertz; used by os.times()
1175
    long ticks_per_second;
1176
#endif
1177
} _posixstate;
1178
1179
1180
static inline _posixstate*
1181
get_posix_state(PyObject *module)
1182
23.2k
{
1183
23.2k
    void *state = _PyModule_GetState(module);
1184
23.2k
    assert(state != NULL);
1185
23.2k
    return (_posixstate *)state;
1186
23.2k
}
1187
1188
/*
1189
 * A PyArg_ParseTuple "converter" function
1190
 * that handles filesystem paths in the manner
1191
 * preferred by the os module.
1192
 *
1193
 * path_converter accepts (Unicode) strings and their
1194
 * subclasses, and bytes and their subclasses.  What
1195
 * it does with the argument depends on path.make_wide:
1196
 *
1197
 *   * If path.make_wide is nonzero, if we get a (Unicode)
1198
 *     string we extract the wchar_t * and return it; if we
1199
 *     get bytes we decode to wchar_t * and return that.
1200
 *
1201
 *   * If path.make_wide is zero, if we get bytes we extract
1202
 *     the char_t * and return it; if we get a (Unicode)
1203
 *     string we encode to char_t * and return that.
1204
 *
1205
 * path_converter also optionally accepts signed
1206
 * integers (representing open file descriptors) instead
1207
 * of path strings.
1208
 *
1209
 * Input fields:
1210
 *   path.nullable
1211
 *     If nonzero, the path is permitted to be None.
1212
 *   path.nonstrict
1213
 *     If nonzero, the path is permitted to contain
1214
 *     embedded null characters and have any length.
1215
 *   path.make_wide
1216
 *     If nonzero, the converter always uses wide, decoding if necessary, else
1217
 *     it always uses narrow, encoding if necessary. The default value is
1218
 *     nonzero on Windows, else zero.
1219
 *   path.suppress_value_error
1220
 *     If nonzero, raising ValueError is suppressed.
1221
 *   path.allow_fd
1222
 *     If nonzero, the path is permitted to be a file handle
1223
 *     (a signed int) instead of a string.
1224
 *   path.function_name
1225
 *     If non-NULL, path_converter will use that as the name
1226
 *     of the function in error messages.
1227
 *     (If path.function_name is NULL it omits the function name.)
1228
 *   path.argument_name
1229
 *     If non-NULL, path_converter will use that as the name
1230
 *     of the parameter in error messages.
1231
 *     (If path.argument_name is NULL it uses "path".)
1232
 *
1233
 * Output fields:
1234
 *   path.wide
1235
 *     Points to the path if it was expressed as Unicode
1236
 *     or if it was bytes and decoded to Unicode.
1237
 *   path.narrow
1238
 *     Points to the path if it was expressed as bytes,
1239
 *     or if it was Unicode and encoded to bytes.
1240
 *   path.fd
1241
 *     Contains a file descriptor if path.accept_fd was true
1242
 *     and the caller provided a signed integer instead of any
1243
 *     sort of string.
1244
 *
1245
 *     WARNING: if your "path" parameter is optional, and is
1246
 *     unspecified, path_converter will never get called.
1247
 *     So if you set allow_fd, you *MUST* initialize path.fd = -1
1248
 *     yourself!
1249
 *   path.value_error
1250
 *     If nonzero, then suppress_value_error was specified and a ValueError
1251
 *     occurred.
1252
 *   path.length
1253
 *     The length of the path in characters, if specified as
1254
 *     a string.
1255
 *   path.object
1256
 *     The original object passed in (if get a PathLike object,
1257
 *     the result of PyOS_FSPath() is treated as the original object).
1258
 *     Own a reference to the object.
1259
 *   path.cleanup
1260
 *     For internal use only.  May point to a temporary object.
1261
 *     (Pay no attention to the man behind the curtain.)
1262
 *
1263
 *   At most one of path.wide or path.narrow will be non-NULL.
1264
 *   If path was None and path.nullable was set,
1265
 *     or if path was an integer and path.allow_fd was set,
1266
 *     both path.wide and path.narrow will be NULL
1267
 *     and path.length will be 0.
1268
 *
1269
 *   path_converter takes care to not write to the path_t
1270
 *   unless it's successful.  However it must reset the
1271
 *   "cleanup" field each time it's called.
1272
 *
1273
 * Use as follows:
1274
 *      path_t path;
1275
 *      memset(&path, 0, sizeof(path));
1276
 *      PyArg_ParseTuple(args, "O&", path_converter, &path);
1277
 *      // ... use values from path ...
1278
 *      path_cleanup(&path);
1279
 *
1280
 * (Note that if PyArg_Parse fails you don't need to call
1281
 * path_cleanup().  However it is safe to do so.)
1282
 */
1283
typedef struct {
1284
    // Input fields
1285
    const char *function_name;
1286
    const char *argument_name;
1287
    int nullable;
1288
    int nonstrict;
1289
    int make_wide;
1290
    int suppress_value_error;
1291
    int allow_fd;
1292
    // Output fields
1293
    const wchar_t *wide;
1294
    const char *narrow;
1295
    int fd;
1296
    int value_error;
1297
    Py_ssize_t length;
1298
    PyObject *object;
1299
    PyObject *cleanup;
1300
} path_t;
1301
1302
#define PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, \
1303
                          make_wide, suppress_value_error, allow_fd) \
1304
5.91k
    {function_name, argument_name, nullable, nonstrict, make_wide, \
1305
5.91k
     suppress_value_error, allow_fd, NULL, NULL, -1, 0, 0, NULL, NULL}
1306
#ifdef MS_WINDOWS
1307
#define PATH_T_INITIALIZE_P(function_name, argument_name, nullable, \
1308
                            nonstrict, suppress_value_error, allow_fd) \
1309
    PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, 1, \
1310
                      suppress_value_error, allow_fd)
1311
#else
1312
#define PATH_T_INITIALIZE_P(function_name, argument_name, nullable, \
1313
                            nonstrict, suppress_value_error, allow_fd) \
1314
5.83k
    PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, 0, \
1315
5.83k
                      suppress_value_error, allow_fd)
1316
#endif
1317
1318
static void
1319
path_cleanup(path_t *path)
1320
5.91k
{
1321
5.91k
    wchar_t *wide = (wchar_t *)path->wide;
1322
5.91k
    path->wide = NULL;
1323
5.91k
    PyMem_Free(wide);
1324
5.91k
    Py_CLEAR(path->object);
1325
5.91k
    Py_CLEAR(path->cleanup);
1326
5.91k
}
1327
1328
static int
1329
path_converter(PyObject *o, void *p)
1330
5.91k
{
1331
5.91k
    path_t *path = (path_t *)p;
1332
5.91k
    PyObject *bytes = NULL;
1333
5.91k
    Py_ssize_t length = 0;
1334
5.91k
    int is_index, is_bytes, is_unicode;
1335
5.91k
    const char *narrow;
1336
5.91k
    PyObject *wo = NULL;
1337
5.91k
    wchar_t *wide = NULL;
1338
1339
5.91k
#define FORMAT_EXCEPTION(exc, fmt) \
1340
5.91k
    PyErr_Format(exc, "%s%s" fmt, \
1341
0
        path->function_name ? path->function_name : "", \
1342
0
        path->function_name ? ": "                : "", \
1343
0
        path->argument_name ? path->argument_name : "path")
1344
1345
    /* Py_CLEANUP_SUPPORTED support */
1346
5.91k
    if (o == NULL) {
1347
0
        path_cleanup(path);
1348
0
        return 1;
1349
0
    }
1350
1351
    /* Ensure it's always safe to call path_cleanup(). */
1352
5.91k
    path->object = path->cleanup = NULL;
1353
    /* path->object owns a reference to the original object */
1354
5.91k
    Py_INCREF(o);
1355
1356
5.91k
    if ((o == Py_None) && path->nullable) {
1357
0
        path->wide = NULL;
1358
0
        path->narrow = NULL;
1359
0
        path->fd = -1;
1360
0
        goto success_exit;
1361
0
    }
1362
1363
    /* Only call this here so that we don't treat the return value of
1364
       os.fspath() as an fd or buffer. */
1365
5.91k
    is_index = path->allow_fd && PyIndex_Check(o);
1366
5.91k
    is_bytes = PyBytes_Check(o);
1367
5.91k
    is_unicode = PyUnicode_Check(o);
1368
1369
5.91k
    if (!is_index && !is_unicode && !is_bytes) {
1370
        /* Inline PyOS_FSPath() for better error messages. */
1371
0
        PyObject *func, *res;
1372
1373
0
        func = _PyObject_LookupSpecial(o, &_Py_ID(__fspath__));
1374
0
        if ((NULL == func) || (func == Py_None)) {
1375
0
            goto error_format;
1376
0
        }
1377
0
        res = _PyObject_CallNoArgs(func);
1378
0
        Py_DECREF(func);
1379
0
        if (NULL == res) {
1380
0
            goto error_exit;
1381
0
        }
1382
0
        else if (PyUnicode_Check(res)) {
1383
0
            is_unicode = 1;
1384
0
        }
1385
0
        else if (PyBytes_Check(res)) {
1386
0
            is_bytes = 1;
1387
0
        }
1388
0
        else {
1389
0
            PyErr_Format(PyExc_TypeError,
1390
0
                 "expected %.200s.__fspath__() to return str or bytes, "
1391
0
                 "not %.200s", _PyType_Name(Py_TYPE(o)),
1392
0
                 _PyType_Name(Py_TYPE(res)));
1393
0
            Py_DECREF(res);
1394
0
            goto error_exit;
1395
0
        }
1396
1397
        /* still owns a reference to the original object */
1398
0
        Py_SETREF(o, res);
1399
0
    }
1400
1401
5.91k
    if (is_unicode) {
1402
5.91k
        if (path->make_wide) {
1403
80
            wide = PyUnicode_AsWideCharString(o, &length);
1404
80
            if (!wide) {
1405
0
                goto error_exit;
1406
0
            }
1407
#ifdef MS_WINDOWS
1408
            if (!path->nonstrict && length > 32767) {
1409
                FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
1410
                goto error_exit;
1411
            }
1412
#endif
1413
80
            if (!path->nonstrict && wcslen(wide) != (size_t)length) {
1414
0
                FORMAT_EXCEPTION(PyExc_ValueError,
1415
0
                                 "embedded null character in %s");
1416
0
                goto error_exit;
1417
0
            }
1418
1419
80
            path->wide = wide;
1420
80
            path->narrow = NULL;
1421
80
            path->fd = -1;
1422
80
            wide = NULL;
1423
80
            goto success_exit;
1424
80
        }
1425
5.83k
        bytes = PyUnicode_EncodeFSDefault(o);
1426
5.83k
        if (!bytes) {
1427
0
            goto error_exit;
1428
0
        }
1429
5.83k
    }
1430
0
    else if (is_bytes) {
1431
0
        bytes = Py_NewRef(o);
1432
0
    }
1433
0
    else if (is_index) {
1434
0
        if (!_fd_converter(o, &path->fd)) {
1435
0
            goto error_exit;
1436
0
        }
1437
0
        path->wide = NULL;
1438
0
        path->narrow = NULL;
1439
0
        goto success_exit;
1440
0
    }
1441
0
    else {
1442
0
 error_format:
1443
0
        PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1444
0
            path->function_name ? path->function_name : "",
1445
0
            path->function_name ? ": "                : "",
1446
0
            path->argument_name ? path->argument_name : "path",
1447
0
            path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1448
0
                                               "integer or None" :
1449
0
            path->allow_fd ? "string, bytes, os.PathLike or integer" :
1450
0
            path->nullable ? "string, bytes, os.PathLike or None" :
1451
0
                             "string, bytes or os.PathLike",
1452
0
            _PyType_Name(Py_TYPE(o)));
1453
0
        goto error_exit;
1454
0
    }
1455
1456
5.83k
    length = PyBytes_GET_SIZE(bytes);
1457
5.83k
    narrow = PyBytes_AS_STRING(bytes);
1458
5.83k
    if (!path->nonstrict && strlen(narrow) != (size_t)length) {
1459
0
        FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
1460
0
        goto error_exit;
1461
0
    }
1462
1463
5.83k
    if (path->make_wide) {
1464
0
        wo = PyUnicode_DecodeFSDefaultAndSize(narrow, length);
1465
0
        if (!wo) {
1466
0
            goto error_exit;
1467
0
        }
1468
1469
0
        wide = PyUnicode_AsWideCharString(wo, &length);
1470
0
        Py_DECREF(wo);
1471
0
        if (!wide) {
1472
0
            goto error_exit;
1473
0
        }
1474
#ifdef MS_WINDOWS
1475
        if (!path->nonstrict && length > 32767) {
1476
            FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
1477
            goto error_exit;
1478
        }
1479
#endif
1480
0
        if (!path->nonstrict && wcslen(wide) != (size_t)length) {
1481
0
            FORMAT_EXCEPTION(PyExc_ValueError,
1482
0
                             "embedded null character in %s");
1483
0
            goto error_exit;
1484
0
        }
1485
0
        path->wide = wide;
1486
0
        path->narrow = NULL;
1487
0
        Py_DECREF(bytes);
1488
0
        wide = NULL;
1489
0
    }
1490
5.83k
    else {
1491
5.83k
        path->wide = NULL;
1492
5.83k
        path->narrow = narrow;
1493
5.83k
        if (bytes == o) {
1494
            /* Still a reference owned by path->object, don't have to
1495
            worry about path->narrow is used after free. */
1496
0
            Py_DECREF(bytes);
1497
0
        }
1498
5.83k
        else {
1499
5.83k
            path->cleanup = bytes;
1500
5.83k
        }
1501
5.83k
    }
1502
5.83k
    path->fd = -1;
1503
1504
5.91k
 success_exit:
1505
5.91k
    path->value_error = 0;
1506
5.91k
    path->length = length;
1507
5.91k
    path->object = o;
1508
5.91k
    return Py_CLEANUP_SUPPORTED;
1509
1510
0
 error_exit:
1511
0
    Py_XDECREF(o);
1512
0
    Py_XDECREF(bytes);
1513
0
    PyMem_Free(wide);
1514
0
    if (!path->suppress_value_error ||
1515
0
        !PyErr_ExceptionMatches(PyExc_ValueError))
1516
0
    {
1517
0
        return 0;
1518
0
    }
1519
0
    PyErr_Clear();
1520
0
    path->wide = NULL;
1521
0
    path->narrow = NULL;
1522
0
    path->fd = -1;
1523
0
    path->value_error = 1;
1524
0
    path->length = 0;
1525
0
    path->object = NULL;
1526
0
    return Py_CLEANUP_SUPPORTED;
1527
0
}
1528
1529
static void
1530
argument_unavailable_error(const char *function_name, const char *argument_name)
1531
0
{
1532
0
    PyErr_Format(PyExc_NotImplementedError,
1533
0
        "%s%s%s unavailable on this platform",
1534
0
        (function_name != NULL) ? function_name : "",
1535
0
        (function_name != NULL) ? ": ": "",
1536
0
        argument_name);
1537
0
}
1538
1539
static int
1540
dir_fd_unavailable(PyObject *o, void *p)
1541
16
{
1542
16
    int dir_fd;
1543
16
    if (!dir_fd_converter(o, &dir_fd))
1544
0
        return 0;
1545
16
    if (dir_fd != DEFAULT_DIR_FD) {
1546
0
        argument_unavailable_error(NULL, "dir_fd");
1547
0
        return 0;
1548
0
    }
1549
16
    *(int *)p = dir_fd;
1550
16
    return 1;
1551
16
}
1552
1553
static int
1554
fd_specified(const char *function_name, int fd)
1555
16
{
1556
16
    if (fd == -1)
1557
16
        return 0;
1558
1559
0
    argument_unavailable_error(function_name, "fd");
1560
0
    return 1;
1561
16
}
1562
1563
static int
1564
follow_symlinks_specified(const char *function_name, int follow_symlinks)
1565
16
{
1566
16
    if (follow_symlinks)
1567
16
        return 0;
1568
1569
0
    argument_unavailable_error(function_name, "follow_symlinks");
1570
0
    return 1;
1571
16
}
1572
1573
static int
1574
path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1575
5.00k
{
1576
5.00k
    if (!path->wide && (dir_fd != DEFAULT_DIR_FD) && !path->narrow) {
1577
0
        PyErr_Format(PyExc_ValueError,
1578
0
                     "%s: can't specify dir_fd without matching path",
1579
0
                     function_name);
1580
0
        return 1;
1581
0
    }
1582
5.00k
    return 0;
1583
5.00k
}
1584
1585
static int
1586
dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1587
5.00k
{
1588
5.00k
    if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1589
0
        PyErr_Format(PyExc_ValueError,
1590
0
                     "%s: can't specify both dir_fd and fd",
1591
0
                     function_name);
1592
0
        return 1;
1593
0
    }
1594
5.00k
    return 0;
1595
5.00k
}
1596
1597
static int
1598
fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1599
                               int follow_symlinks)
1600
5.00k
{
1601
5.00k
    if ((fd > 0) && (!follow_symlinks)) {
1602
0
        PyErr_Format(PyExc_ValueError,
1603
0
                     "%s: cannot use fd and follow_symlinks together",
1604
0
                     function_name);
1605
0
        return 1;
1606
0
    }
1607
5.00k
    return 0;
1608
5.00k
}
1609
1610
static int
1611
dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1612
                                   int follow_symlinks)
1613
16
{
1614
16
    if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1615
0
        PyErr_Format(PyExc_ValueError,
1616
0
                     "%s: cannot use dir_fd and follow_symlinks together",
1617
0
                     function_name);
1618
0
        return 1;
1619
0
    }
1620
16
    return 0;
1621
16
}
1622
1623
#if defined(HAVE_WAITID)
1624
static int
1625
idtype_t_converter(PyObject *arg, void *addr)
1626
0
{
1627
0
    int value = PyLong_AsInt(arg);
1628
0
    if (value == -1 && PyErr_Occurred()) {
1629
0
        return 0;
1630
0
    }
1631
0
    *((idtype_t *)addr) = (idtype_t)(value);
1632
0
    return 1;
1633
0
}
1634
#endif
1635
1636
#ifdef MS_WINDOWS
1637
    typedef long long Py_off_t;
1638
#else
1639
    typedef off_t Py_off_t;
1640
#endif
1641
1642
static int
1643
Py_off_t_converter(PyObject *arg, void *addr)
1644
0
{
1645
#ifdef HAVE_LARGEFILE_SUPPORT
1646
    *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1647
#else
1648
0
    *((Py_off_t *)addr) = PyLong_AsLong(arg);
1649
0
#endif
1650
0
    if (PyErr_Occurred())
1651
0
        return 0;
1652
0
    return 1;
1653
0
}
1654
1655
static PyObject *
1656
PyLong_FromPy_off_t(Py_off_t offset)
1657
0
{
1658
#ifdef HAVE_LARGEFILE_SUPPORT
1659
    return PyLong_FromLongLong(offset);
1660
#else
1661
0
    return PyLong_FromLong(offset);
1662
0
#endif
1663
0
}
1664
1665
#ifdef HAVE_SIGSET_T
1666
/* Convert an iterable of integers to a sigset.
1667
   Return 1 on success, return 0 and raise an exception on error. */
1668
int
1669
_Py_Sigset_Converter(PyObject *obj, void *addr)
1670
0
{
1671
0
    sigset_t *mask = (sigset_t *)addr;
1672
0
    PyObject *iterator, *item;
1673
0
    long signum;
1674
0
    int overflow;
1675
1676
    // The extra parens suppress the unreachable-code warning with clang on MacOS
1677
0
    if (sigemptyset(mask) < (0)) {
1678
        /* Probably only if mask == NULL. */
1679
0
        PyErr_SetFromErrno(PyExc_OSError);
1680
0
        return 0;
1681
0
    }
1682
1683
0
    iterator = PyObject_GetIter(obj);
1684
0
    if (iterator == NULL) {
1685
0
        return 0;
1686
0
    }
1687
1688
0
    while ((item = PyIter_Next(iterator)) != NULL) {
1689
0
        signum = PyLong_AsLongAndOverflow(item, &overflow);
1690
0
        Py_DECREF(item);
1691
0
        if (signum <= 0 || signum >= Py_NSIG) {
1692
0
            if (overflow || signum != -1 || !PyErr_Occurred()) {
1693
0
                PyErr_Format(PyExc_ValueError,
1694
0
                             "signal number %ld out of range [1; %i]",
1695
0
                             signum, Py_NSIG - 1);
1696
0
            }
1697
0
            goto error;
1698
0
        }
1699
0
        if (sigaddset(mask, (int)signum)) {
1700
0
            if (errno != EINVAL) {
1701
                /* Probably impossible */
1702
0
                PyErr_SetFromErrno(PyExc_OSError);
1703
0
                goto error;
1704
0
            }
1705
            /* For backwards compatibility, allow idioms such as
1706
             * `range(1, NSIG)` but warn about invalid signal numbers
1707
             */
1708
0
            const char msg[] =
1709
0
                "invalid signal number %ld, please use valid_signals()";
1710
0
            if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1711
0
                goto error;
1712
0
            }
1713
0
        }
1714
0
    }
1715
0
    if (!PyErr_Occurred()) {
1716
0
        Py_DECREF(iterator);
1717
0
        return 1;
1718
0
    }
1719
1720
0
error:
1721
0
    Py_DECREF(iterator);
1722
0
    return 0;
1723
0
}
1724
#endif /* HAVE_SIGSET_T */
1725
1726
/* Return a dictionary corresponding to the POSIX environment table */
1727
#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1728
/* On Darwin/MacOSX a shared library or framework has no access to
1729
** environ directly, we must obtain it with _NSGetEnviron(). See also
1730
** man environ(7).
1731
*/
1732
#include <crt_externs.h>
1733
#define USE_DARWIN_NS_GET_ENVIRON 1
1734
#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
1735
extern char **environ;
1736
#endif /* !_MSC_VER */
1737
1738
static PyObject *
1739
convertenviron(void)
1740
16
{
1741
16
    PyObject *d;
1742
#ifdef MS_WINDOWS
1743
    wchar_t **e;
1744
#else
1745
16
    char **e;
1746
16
#endif
1747
1748
16
    d = PyDict_New();
1749
16
    if (d == NULL)
1750
0
        return NULL;
1751
#ifdef MS_WINDOWS
1752
    /* _wenviron must be initialized in this way if the program is started
1753
       through main() instead of wmain(). */
1754
    (void)_wgetenv(L"");
1755
    e = _wenviron;
1756
#elif defined(USE_DARWIN_NS_GET_ENVIRON)
1757
    /* environ is not accessible as an extern in a shared object on OSX; use
1758
       _NSGetEnviron to resolve it. The value changes if you add environment
1759
       variables between calls to Py_Initialize, so don't cache the value. */
1760
    e = *_NSGetEnviron();
1761
#else
1762
16
    e = environ;
1763
16
#endif
1764
16
    if (e == NULL)
1765
0
        return d;
1766
592
    for (; *e != NULL; e++) {
1767
576
        PyObject *k;
1768
576
        PyObject *v;
1769
#ifdef MS_WINDOWS
1770
        const wchar_t *p = wcschr(*e, L'=');
1771
#else
1772
576
        const char *p = strchr(*e, '=');
1773
576
#endif
1774
576
        if (p == NULL)
1775
0
            continue;
1776
#ifdef MS_WINDOWS
1777
        k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1778
#else
1779
576
        k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
1780
576
#endif
1781
576
        if (k == NULL) {
1782
0
            Py_DECREF(d);
1783
0
            return NULL;
1784
0
        }
1785
#ifdef MS_WINDOWS
1786
        v = PyUnicode_FromWideChar(p+1, -1);
1787
#else
1788
576
        v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
1789
576
#endif
1790
576
        if (v == NULL) {
1791
0
            Py_DECREF(k);
1792
0
            Py_DECREF(d);
1793
0
            return NULL;
1794
0
        }
1795
576
        if (PyDict_SetDefaultRef(d, k, v, NULL) < 0) {
1796
0
            Py_DECREF(v);
1797
0
            Py_DECREF(k);
1798
0
            Py_DECREF(d);
1799
0
            return NULL;
1800
0
        }
1801
576
        Py_DECREF(k);
1802
576
        Py_DECREF(v);
1803
576
    }
1804
16
    return d;
1805
16
}
1806
1807
/* Set a POSIX-specific error from errno, and return NULL */
1808
1809
static PyObject *
1810
posix_error(void)
1811
0
{
1812
0
    return PyErr_SetFromErrno(PyExc_OSError);
1813
0
}
1814
1815
#ifdef MS_WINDOWS
1816
static PyObject *
1817
win32_error(const char* function, const char* filename)
1818
{
1819
    /* XXX We should pass the function name along in the future.
1820
       (winreg.c also wants to pass the function name.)
1821
       This would however require an additional param to the
1822
       Windows error object, which is non-trivial.
1823
    */
1824
    errno = GetLastError();
1825
    if (filename)
1826
        return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1827
    else
1828
        return PyErr_SetFromWindowsErr(errno);
1829
}
1830
1831
static PyObject *
1832
win32_error_object_err(const char* function, PyObject* filename, DWORD err)
1833
{
1834
    /* XXX - see win32_error for comments on 'function' */
1835
    if (filename)
1836
        return PyErr_SetExcFromWindowsErrWithFilenameObject(
1837
                    PyExc_OSError,
1838
                    err,
1839
                    filename);
1840
    else
1841
        return PyErr_SetFromWindowsErr(err);
1842
}
1843
1844
static PyObject *
1845
win32_error_object(const char* function, PyObject* filename)
1846
{
1847
    errno = GetLastError();
1848
    return win32_error_object_err(function, filename, errno);
1849
}
1850
1851
#endif /* MS_WINDOWS */
1852
1853
static PyObject *
1854
posix_path_object_error(PyObject *path)
1855
314
{
1856
314
    return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1857
314
}
1858
1859
static PyObject *
1860
path_object_error(PyObject *path)
1861
314
{
1862
#ifdef MS_WINDOWS
1863
    return PyErr_SetExcFromWindowsErrWithFilenameObject(
1864
                PyExc_OSError, 0, path);
1865
#else
1866
314
    return posix_path_object_error(path);
1867
314
#endif
1868
314
}
1869
1870
static PyObject *
1871
path_object_error2(PyObject *path, PyObject *path2)
1872
0
{
1873
#ifdef MS_WINDOWS
1874
    return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1875
                PyExc_OSError, 0, path, path2);
1876
#else
1877
0
    return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1878
0
#endif
1879
0
}
1880
1881
static PyObject *
1882
path_error(path_t *path)
1883
314
{
1884
314
    return path_object_error(path->object);
1885
314
}
1886
1887
static PyObject *
1888
posix_path_error(path_t *path)
1889
0
{
1890
0
    return posix_path_object_error(path->object);
1891
0
}
1892
1893
static PyObject *
1894
path_error2(path_t *path, path_t *path2)
1895
0
{
1896
0
    return path_object_error2(path->object, path2->object);
1897
0
}
1898
1899
1900
/* POSIX generic methods */
1901
1902
static PyObject *
1903
posix_fildes_fd(int fd, int (*func)(int))
1904
0
{
1905
0
    int res;
1906
0
    int async_err = 0;
1907
1908
0
    do {
1909
0
        Py_BEGIN_ALLOW_THREADS
1910
0
        _Py_BEGIN_SUPPRESS_IPH
1911
0
        res = (*func)(fd);
1912
0
        _Py_END_SUPPRESS_IPH
1913
0
        Py_END_ALLOW_THREADS
1914
0
    } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1915
0
    if (res != 0)
1916
0
        return (!async_err) ? posix_error() : NULL;
1917
0
    Py_RETURN_NONE;
1918
0
}
1919
1920
1921
#ifdef MS_WINDOWS
1922
/* This is a reimplementation of the C library's chdir function,
1923
   but one that produces Win32 errors instead of DOS error codes.
1924
   chdir is essentially a wrapper around SetCurrentDirectory; however,
1925
   it also needs to set "magic" environment variables indicating
1926
   the per-drive current directory, which are of the form =<drive>: */
1927
static BOOL __stdcall
1928
win32_wchdir(LPCWSTR path)
1929
{
1930
    wchar_t path_buf[MAX_PATH], *new_path = path_buf;
1931
    int result;
1932
    wchar_t env[4] = L"=x:";
1933
1934
    if(!SetCurrentDirectoryW(path))
1935
        return FALSE;
1936
    result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
1937
    if (!result)
1938
        return FALSE;
1939
    if (result > Py_ARRAY_LENGTH(path_buf)) {
1940
        new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
1941
        if (!new_path) {
1942
            SetLastError(ERROR_OUTOFMEMORY);
1943
            return FALSE;
1944
        }
1945
        result = GetCurrentDirectoryW(result, new_path);
1946
        if (!result) {
1947
            PyMem_RawFree(new_path);
1948
            return FALSE;
1949
        }
1950
    }
1951
    int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1952
                            wcsncmp(new_path, L"//", 2) == 0);
1953
    if (!is_unc_like_path) {
1954
        env[1] = new_path[0];
1955
        result = SetEnvironmentVariableW(env, new_path);
1956
    }
1957
    if (new_path != path_buf)
1958
        PyMem_RawFree(new_path);
1959
    return result ? TRUE : FALSE;
1960
}
1961
#endif
1962
1963
#ifdef MS_WINDOWS
1964
/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1965
   - time stamps are restricted to second resolution
1966
   - file modification times suffer from forth-and-back conversions between
1967
     UTC and local time
1968
   Therefore, we implement our own stat, based on the Win32 API directly.
1969
*/
1970
#define HAVE_STAT_NSEC 1
1971
#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
1972
#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
1973
1974
static void
1975
find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1976
                       BY_HANDLE_FILE_INFORMATION *info,
1977
                       ULONG *reparse_tag)
1978
{
1979
    memset(info, 0, sizeof(*info));
1980
    info->dwFileAttributes = pFileData->dwFileAttributes;
1981
    info->ftCreationTime   = pFileData->ftCreationTime;
1982
    info->ftLastAccessTime = pFileData->ftLastAccessTime;
1983
    info->ftLastWriteTime  = pFileData->ftLastWriteTime;
1984
    info->nFileSizeHigh    = pFileData->nFileSizeHigh;
1985
    info->nFileSizeLow     = pFileData->nFileSizeLow;
1986
/*  info->nNumberOfLinks   = 1; */
1987
    if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1988
        *reparse_tag = pFileData->dwReserved0;
1989
    else
1990
        *reparse_tag = 0;
1991
}
1992
1993
static BOOL
1994
attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
1995
{
1996
    HANDLE hFindFile;
1997
    WIN32_FIND_DATAW FileData;
1998
    LPCWSTR filename = pszFile;
1999
    size_t n = wcslen(pszFile);
2000
    if (n && (pszFile[n - 1] == L'\\' || pszFile[n - 1] == L'/')) {
2001
        // cannot use PyMem_Malloc here because we do not hold the GIL
2002
        filename = (LPCWSTR)malloc((n + 1) * sizeof(filename[0]));
2003
        if(!filename) {
2004
            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2005
            return FALSE;
2006
        }
2007
        wcsncpy_s((LPWSTR)filename, n + 1, pszFile, n);
2008
        while (--n > 0 && (filename[n] == L'\\' || filename[n] == L'/')) {
2009
            ((LPWSTR)filename)[n] = L'\0';
2010
        }
2011
        if (!n || (n == 1 && filename[1] == L':')) {
2012
            // Nothing left to query
2013
            free((void *)filename);
2014
            return FALSE;
2015
        }
2016
    }
2017
    hFindFile = FindFirstFileW(filename, &FileData);
2018
    if (pszFile != filename) {
2019
        free((void *)filename);
2020
    }
2021
    if (hFindFile == INVALID_HANDLE_VALUE) {
2022
        return FALSE;
2023
    }
2024
    FindClose(hFindFile);
2025
    find_data_to_file_info(&FileData, info, reparse_tag);
2026
    return TRUE;
2027
}
2028
2029
2030
static void
2031
update_st_mode_from_path(const wchar_t *path, DWORD attr,
2032
                         struct _Py_stat_struct *result)
2033
{
2034
    if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
2035
        /* Fix the file execute permissions. This hack sets S_IEXEC if
2036
           the filename has an extension that is commonly used by files
2037
           that CreateProcessW can execute. A real implementation calls
2038
           GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
2039
           AccessCheck to check for generic read, write, and execute
2040
           access. */
2041
        const wchar_t *fileExtension = wcsrchr(path, '.');
2042
        if (fileExtension) {
2043
            if (_wcsicmp(fileExtension, L".exe") == 0 ||
2044
                _wcsicmp(fileExtension, L".bat") == 0 ||
2045
                _wcsicmp(fileExtension, L".cmd") == 0 ||
2046
                _wcsicmp(fileExtension, L".com") == 0) {
2047
                result->st_mode |= 0111;
2048
            }
2049
        }
2050
    }
2051
}
2052
2053
2054
static int
2055
win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,
2056
                      BOOL traverse)
2057
{
2058
    HANDLE hFile;
2059
    BY_HANDLE_FILE_INFORMATION fileInfo;
2060
    FILE_BASIC_INFO basicInfo;
2061
    FILE_BASIC_INFO *pBasicInfo = NULL;
2062
    FILE_ID_INFO idInfo;
2063
    FILE_ID_INFO *pIdInfo = NULL;
2064
    FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
2065
    DWORD fileType, error;
2066
    BOOL isUnhandledTag = FALSE;
2067
    int retval = 0;
2068
2069
    DWORD access = FILE_READ_ATTRIBUTES;
2070
    DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
2071
    if (!traverse) {
2072
        flags |= FILE_FLAG_OPEN_REPARSE_POINT;
2073
    }
2074
2075
    hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
2076
    if (hFile == INVALID_HANDLE_VALUE) {
2077
        /* Either the path doesn't exist, or the caller lacks access. */
2078
        error = GetLastError();
2079
        switch (error) {
2080
        case ERROR_ACCESS_DENIED:     /* Cannot sync or read attributes. */
2081
        case ERROR_SHARING_VIOLATION: /* It's a paging file. */
2082
            /* Try reading the parent directory. */
2083
            if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
2084
                /* Cannot read the parent directory. */
2085
                switch (GetLastError()) {
2086
                case ERROR_FILE_NOT_FOUND: /* File cannot be found */
2087
                case ERROR_PATH_NOT_FOUND: /* File parent directory cannot be found */
2088
                case ERROR_NOT_READY: /* Drive exists but unavailable */
2089
                case ERROR_BAD_NET_NAME: /* Remote drive unavailable */
2090
                    break;
2091
                /* Restore the error from CreateFileW(). */
2092
                default:
2093
                    SetLastError(error);
2094
                }
2095
2096
                return -1;
2097
            }
2098
            if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2099
                if (traverse ||
2100
                    !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
2101
                    /* The stat call has to traverse but cannot, so fail. */
2102
                    SetLastError(error);
2103
                    return -1;
2104
                }
2105
            }
2106
            break;
2107
2108
        case ERROR_INVALID_PARAMETER:
2109
            /* \\.\con requires read or write access. */
2110
            hFile = CreateFileW(path, access | GENERIC_READ,
2111
                        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2112
                        OPEN_EXISTING, flags, NULL);
2113
            if (hFile == INVALID_HANDLE_VALUE) {
2114
                SetLastError(error);
2115
                return -1;
2116
            }
2117
            break;
2118
2119
        case ERROR_CANT_ACCESS_FILE:
2120
            /* bpo37834: open unhandled reparse points if traverse fails. */
2121
            if (traverse) {
2122
                traverse = FALSE;
2123
                isUnhandledTag = TRUE;
2124
                hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
2125
                            flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2126
            }
2127
            if (hFile == INVALID_HANDLE_VALUE) {
2128
                SetLastError(error);
2129
                return -1;
2130
            }
2131
            break;
2132
2133
        default:
2134
            return -1;
2135
        }
2136
    }
2137
2138
    if (hFile != INVALID_HANDLE_VALUE) {
2139
        /* Handle types other than files on disk. */
2140
        fileType = GetFileType(hFile);
2141
        if (fileType != FILE_TYPE_DISK) {
2142
            if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
2143
                retval = -1;
2144
                goto cleanup;
2145
            }
2146
            DWORD fileAttributes = GetFileAttributesW(path);
2147
            memset(result, 0, sizeof(*result));
2148
            if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
2149
                fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2150
                /* \\.\pipe\ or \\.\mailslot\ */
2151
                result->st_mode = _S_IFDIR;
2152
            } else if (fileType == FILE_TYPE_CHAR) {
2153
                /* \\.\nul */
2154
                result->st_mode = _S_IFCHR;
2155
            } else if (fileType == FILE_TYPE_PIPE) {
2156
                /* \\.\pipe\spam */
2157
                result->st_mode = _S_IFIFO;
2158
            }
2159
            /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
2160
            goto cleanup;
2161
        }
2162
2163
        /* Query the reparse tag, and traverse a non-link. */
2164
        if (!traverse) {
2165
            if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
2166
                    &tagInfo, sizeof(tagInfo))) {
2167
                /* Allow devices that do not support FileAttributeTagInfo. */
2168
                switch (GetLastError()) {
2169
                case ERROR_INVALID_PARAMETER:
2170
                case ERROR_INVALID_FUNCTION:
2171
                case ERROR_NOT_SUPPORTED:
2172
                    tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
2173
                    tagInfo.ReparseTag = 0;
2174
                    break;
2175
                default:
2176
                    retval = -1;
2177
                    goto cleanup;
2178
                }
2179
            } else if (tagInfo.FileAttributes &
2180
                         FILE_ATTRIBUTE_REPARSE_POINT) {
2181
                if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
2182
                    if (isUnhandledTag) {
2183
                        /* Traversing previously failed for either this link
2184
                           or its target. */
2185
                        SetLastError(ERROR_CANT_ACCESS_FILE);
2186
                        retval = -1;
2187
                        goto cleanup;
2188
                    }
2189
                /* Traverse a non-link, but not if traversing already failed
2190
                   for an unhandled tag. */
2191
                } else if (!isUnhandledTag) {
2192
                    CloseHandle(hFile);
2193
                    return win32_xstat_slow_impl(path, result, TRUE);
2194
                }
2195
            }
2196
        }
2197
2198
        if (!GetFileInformationByHandle(hFile, &fileInfo) ||
2199
            !GetFileInformationByHandleEx(hFile, FileBasicInfo,
2200
                                          &basicInfo, sizeof(basicInfo))) {
2201
            switch (GetLastError()) {
2202
            case ERROR_INVALID_PARAMETER:
2203
            case ERROR_INVALID_FUNCTION:
2204
            case ERROR_NOT_SUPPORTED:
2205
                /* Volumes and physical disks are block devices, e.g.
2206
                   \\.\C: and \\.\PhysicalDrive0. */
2207
                memset(result, 0, sizeof(*result));
2208
                result->st_mode = 0x6000; /* S_IFBLK */
2209
                goto cleanup;
2210
            }
2211
            retval = -1;
2212
            goto cleanup;
2213
        }
2214
2215
        /* Successfully got FileBasicInfo, so we'll pass it along */
2216
        pBasicInfo = &basicInfo;
2217
2218
        if (GetFileInformationByHandleEx(hFile, FileIdInfo, &idInfo, sizeof(idInfo))) {
2219
            /* Successfully got FileIdInfo, so pass it along */
2220
            pIdInfo = &idInfo;
2221
        }
2222
    }
2223
2224
    _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, pBasicInfo, pIdInfo, result);
2225
    update_st_mode_from_path(path, fileInfo.dwFileAttributes, result);
2226
2227
cleanup:
2228
    if (hFile != INVALID_HANDLE_VALUE) {
2229
        /* Preserve last error if we are failing */
2230
        error = retval ? GetLastError() : 0;
2231
        if (!CloseHandle(hFile)) {
2232
            retval = -1;
2233
        } else if (retval) {
2234
            /* Restore last error */
2235
            SetLastError(error);
2236
        }
2237
    }
2238
2239
    return retval;
2240
}
2241
2242
static int
2243
win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
2244
                 BOOL traverse)
2245
{
2246
    FILE_STAT_BASIC_INFORMATION statInfo;
2247
    if (_Py_GetFileInformationByName(path, FileStatBasicByNameInfo,
2248
                                     &statInfo, sizeof(statInfo))) {
2249
        if (// Cannot use fast path for reparse points ...
2250
            !(statInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
2251
            // ... unless it's a name surrogate (symlink) and we're not following
2252
            || (!traverse && IsReparseTagNameSurrogate(statInfo.ReparseTag))
2253
        ) {
2254
            _Py_stat_basic_info_to_stat(&statInfo, result);
2255
            update_st_mode_from_path(path, statInfo.FileAttributes, result);
2256
            return 0;
2257
        }
2258
    } else {
2259
        switch(GetLastError()) {
2260
        case ERROR_FILE_NOT_FOUND:
2261
        case ERROR_PATH_NOT_FOUND:
2262
        case ERROR_NOT_READY:
2263
        case ERROR_BAD_NET_NAME:
2264
            /* These errors aren't worth retrying with the slow path */
2265
            return -1;
2266
        case ERROR_NOT_SUPPORTED:
2267
            /* indicates the API couldn't be loaded */
2268
            break;
2269
        }
2270
    }
2271
2272
    return win32_xstat_slow_impl(path, result, traverse);
2273
}
2274
2275
static int
2276
win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
2277
{
2278
    /* Protocol violation: we explicitly clear errno, instead of
2279
       setting it to a POSIX error. Callers should use GetLastError. */
2280
    int code = win32_xstat_impl(path, result, traverse);
2281
    errno = 0;
2282
2283
    /* ctime is only deprecated from 3.12, so we copy birthtime across */
2284
    result->st_ctime = result->st_birthtime;
2285
    result->st_ctime_nsec = result->st_birthtime_nsec;
2286
    return code;
2287
}
2288
/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
2289
2290
   In Posix, stat automatically traverses symlinks and returns the stat
2291
   structure for the target.  In Windows, the equivalent GetFileAttributes by
2292
   default does not traverse symlinks and instead returns attributes for
2293
   the symlink.
2294
2295
   Instead, we will open the file (which *does* traverse symlinks by default)
2296
   and GetFileInformationByHandle(). */
2297
2298
static int
2299
win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
2300
{
2301
    return win32_xstat(path, result, FALSE);
2302
}
2303
2304
static int
2305
win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
2306
{
2307
    return win32_xstat(path, result, TRUE);
2308
}
2309
2310
#endif /* MS_WINDOWS */
2311
2312
PyDoc_STRVAR(stat_result__doc__,
2313
"stat_result: Result from stat, fstat, or lstat.\n\n\
2314
This object may be accessed either as a tuple of\n\
2315
  (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2316
or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
2317
\n\
2318
Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
2319
or st_flags, they are available as attributes only.\n\
2320
\n\
2321
See os.stat for more information.");
2322
2323
static PyStructSequence_Field stat_result_fields[] = {
2324
    {"st_mode",    "protection bits"},
2325
    {"st_ino",     "inode"},
2326
    {"st_dev",     "device"},
2327
    {"st_nlink",   "number of hard links"},
2328
    {"st_uid",     "user ID of owner"},
2329
    {"st_gid",     "group ID of owner"},
2330
    {"st_size",    "total size, in bytes"},
2331
    /* The NULL is replaced with PyStructSequence_UnnamedField later. */
2332
    {NULL,   "integer time of last access"},
2333
    {NULL,   "integer time of last modification"},
2334
    {NULL,   "integer time of last change"},
2335
    {"st_atime",   "time of last access"},
2336
    {"st_mtime",   "time of last modification"},
2337
    {"st_ctime",   "time of last change"},
2338
    {"st_atime_ns",   "time of last access in nanoseconds"},
2339
    {"st_mtime_ns",   "time of last modification in nanoseconds"},
2340
    {"st_ctime_ns",   "time of last change in nanoseconds"},
2341
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2342
    {"st_blksize", "blocksize for filesystem I/O"},
2343
#endif
2344
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2345
    {"st_blocks",  "number of blocks allocated"},
2346
#endif
2347
#ifdef HAVE_STRUCT_STAT_ST_RDEV
2348
    {"st_rdev",    "device type (if inode device)"},
2349
#endif
2350
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
2351
    {"st_flags",   "user defined flags for file"},
2352
#endif
2353
#ifdef HAVE_STRUCT_STAT_ST_GEN
2354
    {"st_gen",    "generation number"},
2355
#endif
2356
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME) || defined(MS_WINDOWS)
2357
    {"st_birthtime",   "time of creation"},
2358
#endif
2359
#ifdef MS_WINDOWS
2360
    {"st_birthtime_ns", "time of creation in nanoseconds"},
2361
#endif
2362
#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2363
    {"st_file_attributes", "Windows file attribute bits"},
2364
#endif
2365
#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2366
    {"st_fstype",  "Type of filesystem"},
2367
#endif
2368
#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2369
    {"st_reparse_tag", "Windows reparse tag"},
2370
#endif
2371
    {0}
2372
};
2373
2374
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2375
#define ST_BLKSIZE_IDX 16
2376
#else
2377
#define ST_BLKSIZE_IDX 15
2378
#endif
2379
2380
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2381
#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
2382
#else
2383
#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
2384
#endif
2385
2386
#ifdef HAVE_STRUCT_STAT_ST_RDEV
2387
#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
2388
#else
2389
#define ST_RDEV_IDX ST_BLOCKS_IDX
2390
#endif
2391
2392
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
2393
#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
2394
#else
2395
#define ST_FLAGS_IDX ST_RDEV_IDX
2396
#endif
2397
2398
#ifdef HAVE_STRUCT_STAT_ST_GEN
2399
#define ST_GEN_IDX (ST_FLAGS_IDX+1)
2400
#else
2401
#define ST_GEN_IDX ST_FLAGS_IDX
2402
#endif
2403
2404
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME) || defined(MS_WINDOWS)
2405
#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
2406
#else
2407
#define ST_BIRTHTIME_IDX ST_GEN_IDX
2408
#endif
2409
2410
#ifdef MS_WINDOWS
2411
#define ST_BIRTHTIME_NS_IDX (ST_BIRTHTIME_IDX+1)
2412
#else
2413
#define ST_BIRTHTIME_NS_IDX ST_BIRTHTIME_IDX
2414
#endif
2415
2416
#if defined(HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES) || defined(MS_WINDOWS)
2417
#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_NS_IDX+1)
2418
#else
2419
#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_NS_IDX
2420
#endif
2421
2422
#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2423
#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2424
#else
2425
#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2426
#endif
2427
2428
#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2429
#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2430
#else
2431
#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2432
#endif
2433
2434
static PyStructSequence_Desc stat_result_desc = {
2435
    "stat_result", /* name */
2436
    stat_result__doc__, /* doc */
2437
    stat_result_fields,
2438
    10
2439
};
2440
2441
PyDoc_STRVAR(statvfs_result__doc__,
2442
"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2443
This object may be accessed either as a tuple of\n\
2444
  (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
2445
or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
2446
\n\
2447
See os.statvfs for more information.");
2448
2449
static PyStructSequence_Field statvfs_result_fields[] = {
2450
    {"f_bsize",  },
2451
    {"f_frsize", },
2452
    {"f_blocks", },
2453
    {"f_bfree",  },
2454
    {"f_bavail", },
2455
    {"f_files",  },
2456
    {"f_ffree",  },
2457
    {"f_favail", },
2458
    {"f_flag",   },
2459
    {"f_namemax",},
2460
    {"f_fsid",   },
2461
    {0}
2462
};
2463
2464
static PyStructSequence_Desc statvfs_result_desc = {
2465
    "statvfs_result", /* name */
2466
    statvfs_result__doc__, /* doc */
2467
    statvfs_result_fields,
2468
    10
2469
};
2470
2471
#if defined(HAVE_WAITID)
2472
PyDoc_STRVAR(waitid_result__doc__,
2473
"waitid_result: Result from waitid.\n\n\
2474
This object may be accessed either as a tuple of\n\
2475
  (si_pid, si_uid, si_signo, si_status, si_code),\n\
2476
or via the attributes si_pid, si_uid, and so on.\n\
2477
\n\
2478
See os.waitid for more information.");
2479
2480
static PyStructSequence_Field waitid_result_fields[] = {
2481
    {"si_pid",  },
2482
    {"si_uid", },
2483
    {"si_signo", },
2484
    {"si_status",  },
2485
    {"si_code", },
2486
    {0}
2487
};
2488
2489
static PyStructSequence_Desc waitid_result_desc = {
2490
    "waitid_result", /* name */
2491
    waitid_result__doc__, /* doc */
2492
    waitid_result_fields,
2493
    5
2494
};
2495
#endif
2496
2497
static PyObject *
2498
statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2499
0
{
2500
0
    PyStructSequence *result;
2501
0
    int i;
2502
2503
    // ht_module doesn't get set in PyStructSequence_NewType(),
2504
    // so we can't use PyType_GetModule().
2505
0
    PyObject *mod = PyImport_GetModule(MODNAME_OBJ);
2506
0
    if (mod == NULL) {
2507
0
        return NULL;
2508
0
    }
2509
0
    _posixstate *state = get_posix_state(mod);
2510
0
    Py_DECREF(mod);
2511
0
    if (state == NULL) {
2512
0
        return NULL;
2513
0
    }
2514
0
#define structseq_new state->statresult_new_orig
2515
2516
0
    result = (PyStructSequence*)structseq_new(type, args, kwds);
2517
0
    if (!result)
2518
0
        return NULL;
2519
    /* If we have been initialized from a tuple,
2520
       st_?time might be set to None. Initialize it
2521
       from the int slots.  */
2522
0
    for (i = 7; i <= 9; i++) {
2523
0
        if (result->ob_item[i+3] == Py_None) {
2524
0
            Py_DECREF(Py_None);
2525
0
            result->ob_item[i+3] = Py_NewRef(result->ob_item[i]);
2526
0
        }
2527
0
    }
2528
0
    return (PyObject*)result;
2529
0
}
2530
2531
static int
2532
_posix_clear(PyObject *module)
2533
0
{
2534
0
    _posixstate *state = get_posix_state(module);
2535
0
    Py_CLEAR(state->billion);
2536
0
    Py_CLEAR(state->DirEntryType);
2537
0
    Py_CLEAR(state->ScandirIteratorType);
2538
0
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
2539
0
    Py_CLEAR(state->SchedParamType);
2540
0
#endif
2541
0
    Py_CLEAR(state->StatResultType);
2542
0
    Py_CLEAR(state->StatVFSResultType);
2543
0
    Py_CLEAR(state->TerminalSizeType);
2544
0
    Py_CLEAR(state->TimesResultType);
2545
0
    Py_CLEAR(state->UnameResultType);
2546
0
#if defined(HAVE_WAITID)
2547
0
    Py_CLEAR(state->WaitidResultType);
2548
0
#endif
2549
0
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
2550
0
    Py_CLEAR(state->struct_rusage);
2551
0
#endif
2552
0
    Py_CLEAR(state->st_mode);
2553
0
    return 0;
2554
0
}
2555
2556
static int
2557
_posix_traverse(PyObject *module, visitproc visit, void *arg)
2558
4.44k
{
2559
4.44k
    _posixstate *state = get_posix_state(module);
2560
4.44k
    Py_VISIT(state->billion);
2561
4.44k
    Py_VISIT(state->DirEntryType);
2562
4.44k
    Py_VISIT(state->ScandirIteratorType);
2563
4.44k
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
2564
4.44k
    Py_VISIT(state->SchedParamType);
2565
4.44k
#endif
2566
4.44k
    Py_VISIT(state->StatResultType);
2567
4.44k
    Py_VISIT(state->StatVFSResultType);
2568
4.44k
    Py_VISIT(state->TerminalSizeType);
2569
4.44k
    Py_VISIT(state->TimesResultType);
2570
4.44k
    Py_VISIT(state->UnameResultType);
2571
4.44k
#if defined(HAVE_WAITID)
2572
4.44k
    Py_VISIT(state->WaitidResultType);
2573
4.44k
#endif
2574
4.44k
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
2575
4.44k
    Py_VISIT(state->struct_rusage);
2576
4.44k
#endif
2577
4.44k
    Py_VISIT(state->st_mode);
2578
4.44k
    return 0;
2579
4.44k
}
2580
2581
static void
2582
_posix_free(void *module)
2583
0
{
2584
0
   _posix_clear((PyObject *)module);
2585
0
}
2586
2587
static int
2588
fill_time(PyObject *module, PyObject *v, int s_index, int f_index, int ns_index, time_t sec, unsigned long nsec)
2589
14.0k
{
2590
14.0k
    assert(!PyErr_Occurred());
2591
2592
14.0k
    int res = -1;
2593
14.0k
    PyObject *s_in_ns = NULL;
2594
14.0k
    PyObject *ns_total = NULL;
2595
14.0k
    PyObject *float_s = NULL;
2596
2597
14.0k
    PyObject *s = _PyLong_FromTime_t(sec);
2598
14.0k
    PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2599
14.0k
    if (!(s && ns_fractional)) {
2600
0
        goto exit;
2601
0
    }
2602
2603
14.0k
    s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion);
2604
14.0k
    if (!s_in_ns) {
2605
0
        goto exit;
2606
0
    }
2607
2608
14.0k
    ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2609
14.0k
    if (!ns_total)
2610
0
        goto exit;
2611
2612
14.0k
    float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2613
14.0k
    if (!float_s) {
2614
0
        goto exit;
2615
0
    }
2616
2617
14.0k
    if (s_index >= 0) {
2618
14.0k
        PyStructSequence_SET_ITEM(v, s_index, s);
2619
14.0k
        s = NULL;
2620
14.0k
    }
2621
14.0k
    if (f_index >= 0) {
2622
14.0k
        PyStructSequence_SET_ITEM(v, f_index, float_s);
2623
14.0k
        float_s = NULL;
2624
14.0k
    }
2625
14.0k
    if (ns_index >= 0) {
2626
14.0k
        PyStructSequence_SET_ITEM(v, ns_index, ns_total);
2627
14.0k
        ns_total = NULL;
2628
14.0k
    }
2629
2630
14.0k
    assert(!PyErr_Occurred());
2631
14.0k
    res = 0;
2632
2633
14.0k
exit:
2634
14.0k
    Py_XDECREF(s);
2635
14.0k
    Py_XDECREF(ns_fractional);
2636
14.0k
    Py_XDECREF(s_in_ns);
2637
14.0k
    Py_XDECREF(ns_total);
2638
14.0k
    Py_XDECREF(float_s);
2639
14.0k
    return res;
2640
14.0k
}
2641
2642
#ifdef MS_WINDOWS
2643
static PyObject*
2644
_pystat_l128_from_l64_l64(uint64_t low, uint64_t high)
2645
{
2646
    PyObject *o_low = PyLong_FromUnsignedLongLong(low);
2647
    if (!o_low || !high) {
2648
        return o_low;
2649
    }
2650
    PyObject *o_high = PyLong_FromUnsignedLongLong(high);
2651
    PyObject *l64 = o_high ? PyLong_FromLong(64) : NULL;
2652
    if (!l64) {
2653
        Py_XDECREF(o_high);
2654
        Py_DECREF(o_low);
2655
        return NULL;
2656
    }
2657
    Py_SETREF(o_high, PyNumber_Lshift(o_high, l64));
2658
    Py_DECREF(l64);
2659
    if (!o_high) {
2660
        Py_DECREF(o_low);
2661
        return NULL;
2662
    }
2663
    Py_SETREF(o_low, PyNumber_Add(o_low, o_high));
2664
    Py_DECREF(o_high);
2665
    return o_low;
2666
}
2667
#endif
2668
2669
/* pack a system stat C structure into the Python stat tuple
2670
   (used by posix_stat() and posix_fstat()) */
2671
static PyObject*
2672
_pystat_fromstructstat(PyObject *module, STRUCT_STAT *st)
2673
4.69k
{
2674
4.69k
    assert(!PyErr_Occurred());
2675
2676
4.69k
    PyObject *StatResultType = get_posix_state(module)->StatResultType;
2677
4.69k
    PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
2678
4.69k
    if (v == NULL) {
2679
0
        return NULL;
2680
0
    }
2681
2682
4.69k
#define SET_ITEM(pos, expr) \
2683
46.9k
    do { \
2684
46.9k
        PyObject *obj = (expr); \
2685
46.9k
        if (obj == NULL) { \
2686
0
            goto error; \
2687
0
        } \
2688
46.9k
        PyStructSequence_SET_ITEM(v, (pos), obj); \
2689
46.9k
    } while (0)
2690
2691
4.69k
    SET_ITEM(0, PyLong_FromLong((long)st->st_mode));
2692
#ifdef MS_WINDOWS
2693
    SET_ITEM(1, _pystat_l128_from_l64_l64(st->st_ino, st->st_ino_high));
2694
    SET_ITEM(2, PyLong_FromUnsignedLongLong(st->st_dev));
2695
#else
2696
4.69k
    static_assert(sizeof(unsigned long long) >= sizeof(st->st_ino),
2697
4.69k
                  "stat.st_ino is larger than unsigned long long");
2698
4.69k
    SET_ITEM(1, PyLong_FromUnsignedLongLong(st->st_ino));
2699
4.69k
    SET_ITEM(2, _PyLong_FromDev(st->st_dev));
2700
4.69k
#endif
2701
4.69k
    SET_ITEM(3, PyLong_FromLong((long)st->st_nlink));
2702
#if defined(MS_WINDOWS)
2703
    SET_ITEM(4, PyLong_FromLong(0));
2704
    SET_ITEM(5, PyLong_FromLong(0));
2705
#else
2706
4.69k
    SET_ITEM(4, _PyLong_FromUid(st->st_uid));
2707
4.69k
    SET_ITEM(5, _PyLong_FromGid(st->st_gid));
2708
4.69k
#endif
2709
4.69k
    static_assert(sizeof(long long) >= sizeof(st->st_size),
2710
4.69k
                  "stat.st_size is larger than long long");
2711
4.69k
    SET_ITEM(6, PyLong_FromLongLong(st->st_size));
2712
2713
    // Set st_atime, st_mtime and st_ctime
2714
4.69k
    unsigned long ansec, mnsec, cnsec;
2715
4.69k
#if defined(HAVE_STAT_TV_NSEC)
2716
4.69k
    ansec = st->st_atim.tv_nsec;
2717
4.69k
    mnsec = st->st_mtim.tv_nsec;
2718
4.69k
    cnsec = st->st_ctim.tv_nsec;
2719
#elif defined(HAVE_STAT_TV_NSEC2)
2720
    ansec = st->st_atimespec.tv_nsec;
2721
    mnsec = st->st_mtimespec.tv_nsec;
2722
    cnsec = st->st_ctimespec.tv_nsec;
2723
#elif defined(HAVE_STAT_NSEC)
2724
    ansec = st->st_atime_nsec;
2725
    mnsec = st->st_mtime_nsec;
2726
    cnsec = st->st_ctime_nsec;
2727
#else
2728
    ansec = mnsec = cnsec = 0;
2729
#endif
2730
4.69k
    if (fill_time(module, v, 7, 10, 13, st->st_atime, ansec) < 0) {
2731
0
        goto error;
2732
0
    }
2733
4.69k
    if (fill_time(module, v, 8, 11, 14, st->st_mtime, mnsec) < 0) {
2734
0
        goto error;
2735
0
    }
2736
4.69k
    if (fill_time(module, v, 9, 12, 15, st->st_ctime, cnsec) < 0) {
2737
0
        goto error;
2738
0
    }
2739
2740
4.69k
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2741
4.69k
    SET_ITEM(ST_BLKSIZE_IDX, PyLong_FromLong((long)st->st_blksize));
2742
4.69k
#endif
2743
4.69k
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2744
4.69k
    SET_ITEM(ST_BLOCKS_IDX, PyLong_FromLong((long)st->st_blocks));
2745
4.69k
#endif
2746
4.69k
#ifdef HAVE_STRUCT_STAT_ST_RDEV
2747
4.69k
    SET_ITEM(ST_RDEV_IDX, PyLong_FromLong((long)st->st_rdev));
2748
4.69k
#endif
2749
#ifdef HAVE_STRUCT_STAT_ST_GEN
2750
    SET_ITEM(ST_GEN_IDX, PyLong_FromLong((long)st->st_gen));
2751
#endif
2752
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
2753
    {
2754
      unsigned long bsec, bnsec;
2755
      bsec = (long)st->st_birthtime;
2756
#ifdef HAVE_STAT_TV_NSEC2
2757
      bnsec = st->st_birthtimespec.tv_nsec;
2758
#else
2759
      bnsec = 0;
2760
#endif
2761
      SET_ITEM(ST_BIRTHTIME_IDX, PyFloat_FromDouble(bsec + bnsec * 1e-9));
2762
    }
2763
#elif defined(MS_WINDOWS)
2764
    if (fill_time(module, v, -1, ST_BIRTHTIME_IDX, ST_BIRTHTIME_NS_IDX,
2765
                  st->st_birthtime, st->st_birthtime_nsec) < 0) {
2766
        goto error;
2767
    }
2768
#endif
2769
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
2770
    SET_ITEM(ST_FLAGS_IDX, PyLong_FromLong((long)st->st_flags));
2771
#endif
2772
#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2773
    SET_ITEM(ST_FILE_ATTRIBUTES_IDX,
2774
             PyLong_FromUnsignedLong(st->st_file_attributes));
2775
#endif
2776
#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2777
   SET_ITEM(ST_FSTYPE_IDX, PyUnicode_FromString(st->st_fstype));
2778
#endif
2779
#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2780
    SET_ITEM(ST_REPARSE_TAG_IDX, PyLong_FromUnsignedLong(st->st_reparse_tag));
2781
#endif
2782
2783
4.69k
    assert(!PyErr_Occurred());
2784
4.69k
    return v;
2785
2786
0
error:
2787
0
    Py_DECREF(v);
2788
0
    return NULL;
2789
2790
4.69k
#undef SET_ITEM
2791
4.69k
}
2792
2793
/* POSIX methods */
2794
2795
2796
static PyObject *
2797
posix_do_stat(PyObject *module, const char *function_name, path_t *path,
2798
              int dir_fd, int follow_symlinks)
2799
5.00k
{
2800
5.00k
    STRUCT_STAT st;
2801
5.00k
    int result;
2802
2803
5.00k
#ifdef HAVE_FSTATAT
2804
5.00k
    int fstatat_unavailable = 0;
2805
5.00k
#endif
2806
2807
#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2808
    if (follow_symlinks_specified(function_name, follow_symlinks))
2809
        return NULL;
2810
#endif
2811
2812
5.00k
    if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2813
5.00k
        dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2814
5.00k
        fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2815
0
        return NULL;
2816
2817
5.00k
    Py_BEGIN_ALLOW_THREADS
2818
5.00k
    if (path->fd != -1)
2819
0
        result = FSTAT(path->fd, &st);
2820
#ifdef MS_WINDOWS
2821
    else if (follow_symlinks)
2822
        result = win32_stat(path->wide, &st);
2823
    else
2824
        result = win32_lstat(path->wide, &st);
2825
#else
2826
5.00k
    else
2827
5.00k
#if defined(HAVE_LSTAT)
2828
5.00k
    if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2829
0
        result = LSTAT(path->narrow, &st);
2830
5.00k
    else
2831
5.00k
#endif /* HAVE_LSTAT */
2832
5.00k
#ifdef HAVE_FSTATAT
2833
5.00k
    if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2834
0
        if (HAVE_FSTATAT_RUNTIME) {
2835
0
            result = fstatat(dir_fd, path->narrow, &st,
2836
0
                         follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2837
2838
0
        } else {
2839
0
            fstatat_unavailable = 1;
2840
0
        }
2841
0
    } else
2842
5.00k
#endif /* HAVE_FSTATAT */
2843
5.00k
        result = STAT(path->narrow, &st);
2844
5.00k
#endif /* MS_WINDOWS */
2845
5.00k
    Py_END_ALLOW_THREADS
2846
2847
5.00k
#ifdef HAVE_FSTATAT
2848
5.00k
    if (fstatat_unavailable) {
2849
0
        argument_unavailable_error("stat", "dir_fd");
2850
0
        return NULL;
2851
0
    }
2852
5.00k
#endif
2853
2854
5.00k
    if (result != 0) {
2855
314
        return path_error(path);
2856
314
    }
2857
2858
4.69k
    return _pystat_fromstructstat(module, &st);
2859
5.00k
}
2860
2861
/*[python input]
2862
2863
for s in """
2864
2865
FACCESSAT
2866
FCHMODAT
2867
FCHOWNAT
2868
FSTATAT
2869
LINKAT
2870
MKDIRAT
2871
MKFIFOAT
2872
MKNODAT
2873
OPENAT
2874
READLINKAT
2875
SYMLINKAT
2876
UNLINKAT
2877
2878
""".strip().split():
2879
    s = s.strip()
2880
    print("""
2881
#ifdef HAVE_{s}
2882
    #define {s}_DIR_FD_CONVERTER dir_fd_converter
2883
#else
2884
    #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
2885
#endif
2886
""".rstrip().format(s=s))
2887
2888
for s in """
2889
2890
FCHDIR
2891
FCHMOD
2892
FCHOWN
2893
FDOPENDIR
2894
FEXECVE
2895
FPATHCONF
2896
FSTATVFS
2897
FTRUNCATE
2898
2899
""".strip().split():
2900
    s = s.strip()
2901
    print("""
2902
#ifdef HAVE_{s}
2903
    #define PATH_HAVE_{s} 1
2904
#else
2905
    #define PATH_HAVE_{s} 0
2906
#endif
2907
2908
""".rstrip().format(s=s))
2909
[python start generated code]*/
2910
2911
#ifdef HAVE_FACCESSAT
2912
0
    #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2913
#else
2914
    #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2915
#endif
2916
2917
#ifdef HAVE_FCHMODAT
2918
0
    #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2919
#else
2920
    #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2921
#endif
2922
2923
#ifdef HAVE_FCHOWNAT
2924
0
    #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2925
#else
2926
    #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2927
#endif
2928
2929
#ifdef HAVE_FSTATAT
2930
0
    #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2931
#else
2932
    #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2933
#endif
2934
2935
#ifdef HAVE_LINKAT
2936
    #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2937
#else
2938
    #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2939
#endif
2940
2941
#ifdef HAVE_MKDIRAT
2942
0
    #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2943
#else
2944
    #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2945
#endif
2946
2947
#ifdef HAVE_MKFIFOAT
2948
0
    #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2949
#else
2950
    #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2951
#endif
2952
2953
#ifdef HAVE_MKNODAT
2954
0
    #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2955
#else
2956
    #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2957
#endif
2958
2959
#ifdef HAVE_OPENAT
2960
0
    #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2961
#else
2962
    #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2963
#endif
2964
2965
#ifdef HAVE_READLINKAT
2966
0
    #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2967
#else
2968
    #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2969
#endif
2970
2971
#ifdef HAVE_SYMLINKAT
2972
0
    #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2973
#else
2974
    #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2975
#endif
2976
2977
#ifdef HAVE_UNLINKAT
2978
0
    #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2979
#else
2980
    #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2981
#endif
2982
2983
#ifdef HAVE_FCHDIR
2984
    #define PATH_HAVE_FCHDIR 1
2985
#else
2986
    #define PATH_HAVE_FCHDIR 0
2987
#endif
2988
2989
#ifdef HAVE_FCHMOD
2990
    #define PATH_HAVE_FCHMOD 1
2991
#else
2992
    #define PATH_HAVE_FCHMOD 0
2993
#endif
2994
2995
#ifdef HAVE_FCHOWN
2996
    #define PATH_HAVE_FCHOWN 1
2997
#else
2998
    #define PATH_HAVE_FCHOWN 0
2999
#endif
3000
3001
#ifdef HAVE_FDOPENDIR
3002
    #define PATH_HAVE_FDOPENDIR 1
3003
#else
3004
    #define PATH_HAVE_FDOPENDIR 0
3005
#endif
3006
3007
#ifdef HAVE_FEXECVE
3008
    #define PATH_HAVE_FEXECVE 1
3009
#else
3010
    #define PATH_HAVE_FEXECVE 0
3011
#endif
3012
3013
#ifdef HAVE_FPATHCONF
3014
    #define PATH_HAVE_FPATHCONF 1
3015
#else
3016
    #define PATH_HAVE_FPATHCONF 0
3017
#endif
3018
3019
#ifdef HAVE_FSTATVFS
3020
    #define PATH_HAVE_FSTATVFS 1
3021
#else
3022
    #define PATH_HAVE_FSTATVFS 0
3023
#endif
3024
3025
#ifdef HAVE_FTRUNCATE
3026
    #define PATH_HAVE_FTRUNCATE 1
3027
#else
3028
    #define PATH_HAVE_FTRUNCATE 0
3029
#endif
3030
/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
3031
3032
#ifdef MS_WINDOWS
3033
    #undef PATH_HAVE_FTRUNCATE
3034
    #define PATH_HAVE_FTRUNCATE 1
3035
    #undef PATH_HAVE_FCHMOD
3036
    #define PATH_HAVE_FCHMOD 1
3037
#endif
3038
3039
/*[python input]
3040
3041
class path_t_converter(CConverter):
3042
3043
    type = "path_t"
3044
    impl_by_reference = True
3045
    parse_by_reference = True
3046
3047
    converter = 'path_converter'
3048
3049
    def converter_init(self, *, allow_fd=False, make_wide=None,
3050
                       nonstrict=False, nullable=False,
3051
                       suppress_value_error=False):
3052
        # right now path_t doesn't support default values.
3053
        # to support a default value, you'll need to override initialize().
3054
        if self.default not in (unspecified, None):
3055
            fail("Can't specify a default to the path_t converter!")
3056
3057
        if self.c_default not in (None, 'Py_None'):
3058
            raise RuntimeError("Can't specify a c_default to the path_t converter!")
3059
3060
        self.nullable = nullable
3061
        self.nonstrict = nonstrict
3062
        self.make_wide = make_wide
3063
        self.suppress_value_error = suppress_value_error
3064
        self.allow_fd = allow_fd
3065
3066
    def pre_render(self):
3067
        def strify(value):
3068
            if isinstance(value, str):
3069
                return value
3070
            return str(int(bool(value)))
3071
3072
        # add self.py_name here when merging with posixmodule conversion
3073
        if self.make_wide is None:
3074
            self.c_default = 'PATH_T_INITIALIZE_P("{}", "{}", {}, {}, {}, {})'.format(
3075
                self.function.name,
3076
                self.name,
3077
                strify(self.nullable),
3078
                strify(self.nonstrict),
3079
                strify(self.suppress_value_error),
3080
                strify(self.allow_fd),
3081
            )
3082
        else:
3083
            self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {}, {}, {}, {})'.format(
3084
                self.function.name,
3085
                self.name,
3086
                strify(self.nullable),
3087
                strify(self.nonstrict),
3088
                strify(self.make_wide),
3089
                strify(self.suppress_value_error),
3090
                strify(self.allow_fd),
3091
            )
3092
3093
    def cleanup(self):
3094
        return "path_cleanup(&" + self.name + ");\n"
3095
3096
3097
class dir_fd_converter(CConverter):
3098
    type = 'int'
3099
3100
    def converter_init(self, requires=None):
3101
        if self.default in (unspecified, None):
3102
            self.c_default = 'DEFAULT_DIR_FD'
3103
        if isinstance(requires, str):
3104
            self.converter = requires.upper() + '_DIR_FD_CONVERTER'
3105
        else:
3106
            self.converter = 'dir_fd_converter'
3107
3108
class uid_t_converter(CConverter):
3109
    type = "uid_t"
3110
    converter = '_Py_Uid_Converter'
3111
3112
class gid_t_converter(CConverter):
3113
    type = "gid_t"
3114
    converter = '_Py_Gid_Converter'
3115
3116
class dev_t_converter(CConverter):
3117
    type = 'dev_t'
3118
    converter = '_Py_Dev_Converter'
3119
3120
class dev_t_return_converter(unsigned_long_return_converter):
3121
    type = 'dev_t'
3122
    conversion_fn = '_PyLong_FromDev'
3123
    unsigned_cast = '(dev_t)'
3124
3125
class FSConverter_converter(CConverter):
3126
    type = 'PyObject *'
3127
    converter = 'PyUnicode_FSConverter'
3128
    def converter_init(self):
3129
        if self.default is not unspecified:
3130
            fail("FSConverter_converter does not support default values")
3131
        self.c_default = 'NULL'
3132
3133
    def cleanup(self):
3134
        return "Py_XDECREF(" + self.name + ");\n"
3135
3136
class pid_t_converter(CConverter):
3137
    type = 'pid_t'
3138
    format_unit = '" _Py_PARSE_PID "'
3139
3140
    def parse_arg(self, argname, displayname, *, limited_capi):
3141
        return self.format_code("""
3142
            {paramname} = PyLong_AsPid({argname});
3143
            if ({paramname} == (pid_t)(-1) && PyErr_Occurred()) {{{{
3144
                goto exit;
3145
            }}}}
3146
            """, argname=argname)
3147
3148
class idtype_t_converter(CConverter):
3149
    type = 'idtype_t'
3150
    converter = 'idtype_t_converter'
3151
3152
class id_t_converter(CConverter):
3153
    type = 'id_t'
3154
    format_unit = '" _Py_PARSE_PID "'
3155
3156
    def parse_arg(self, argname, displayname, *, limited_capi):
3157
        return self.format_code("""
3158
            {paramname} = (id_t)PyLong_AsPid({argname});
3159
            if ({paramname} == (id_t)(-1) && PyErr_Occurred()) {{{{
3160
                goto exit;
3161
            }}}}
3162
            """, argname=argname)
3163
3164
class intptr_t_converter(CConverter):
3165
    type = 'intptr_t'
3166
    format_unit = '" _Py_PARSE_INTPTR "'
3167
3168
    def parse_arg(self, argname, displayname, *, limited_capi):
3169
        return self.format_code("""
3170
            {paramname} = (intptr_t)PyLong_AsVoidPtr({argname});
3171
            if (!{paramname} && PyErr_Occurred()) {{{{
3172
                goto exit;
3173
            }}}}
3174
            """, argname=argname)
3175
3176
class Py_off_t_converter(CConverter):
3177
    type = 'Py_off_t'
3178
    converter = 'Py_off_t_converter'
3179
3180
class Py_off_t_return_converter(long_return_converter):
3181
    type = 'Py_off_t'
3182
    conversion_fn = 'PyLong_FromPy_off_t'
3183
3184
class confname_converter(CConverter):
3185
    type="int"
3186
    converter="conv_confname"
3187
3188
    def converter_init(self, *, table):
3189
        self.table = table
3190
3191
    def parse_arg(self, argname, displayname, *, limited_capi):
3192
        return self.format_code("""
3193
            if (!{converter}(module, {argname}, &{paramname}, "{table}")) {{{{
3194
                goto exit;
3195
            }}}}
3196
        """, argname=argname, converter=self.converter, table=self.table)
3197
3198
[python start generated code]*/
3199
/*[python end generated code: output=da39a3ee5e6b4b0d input=8189d5ae78244626]*/
3200
3201
/*[clinic input]
3202
3203
os.stat
3204
3205
    path : path_t(allow_fd=True)
3206
        Path to be examined; can be string, bytes, a path-like object or
3207
        open-file-descriptor int.
3208
3209
    *
3210
3211
    dir_fd : dir_fd(requires='fstatat') = None
3212
        If not None, it should be a file descriptor open to a directory,
3213
        and path should be a relative string; path will then be relative to
3214
        that directory.
3215
3216
    follow_symlinks: bool = True
3217
        If False, and the last element of the path is a symbolic link,
3218
        stat will examine the symbolic link itself instead of the file
3219
        the link points to.
3220
3221
Perform a stat system call on the given path.
3222
3223
dir_fd and follow_symlinks may not be implemented
3224
  on your platform.  If they are unavailable, using them will raise a
3225
  NotImplementedError.
3226
3227
It's an error to use dir_fd or follow_symlinks when specifying path as
3228
  an open file descriptor.
3229
3230
[clinic start generated code]*/
3231
3232
static PyObject *
3233
os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
3234
/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
3235
5.00k
{
3236
5.00k
    return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks);
3237
5.00k
}
3238
3239
3240
/*[clinic input]
3241
@permit_long_summary
3242
os.lstat
3243
3244
    path : path_t
3245
3246
    *
3247
3248
    dir_fd : dir_fd(requires='fstatat') = None
3249
3250
Perform a stat system call on the given path, without following symbolic links.
3251
3252
Like stat(), but do not follow symbolic links.
3253
Equivalent to stat(path, follow_symlinks=False).
3254
[clinic start generated code]*/
3255
3256
static PyObject *
3257
os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
3258
/*[clinic end generated code: output=ef82a5d35ce8ab37 input=024102124f88e743]*/
3259
0
{
3260
0
    int follow_symlinks = 0;
3261
0
    return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks);
3262
0
}
3263
3264
3265
/*[clinic input]
3266
os.access -> bool
3267
3268
    path: path_t
3269
        Path to be tested; can be string, bytes, or a path-like object.
3270
3271
    mode: int
3272
        Operating-system mode bitfield.  Can be F_OK to test existence,
3273
        or the inclusive-OR of R_OK, W_OK, and X_OK.
3274
3275
    *
3276
3277
    dir_fd : dir_fd(requires='faccessat') = None
3278
        If 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
3280
        directory.
3281
3282
    effective_ids: bool = False
3283
        If True, access will use the effective uid/gid instead of
3284
        the real uid/gid.
3285
3286
    follow_symlinks: bool = True
3287
        If False, and the last element of the path is a symbolic link,
3288
        access will examine the symbolic link itself instead of the file
3289
        the link points to.
3290
3291
Use the real uid/gid to test for access to a path.
3292
3293
{parameters}
3294
dir_fd, effective_ids, and follow_symlinks may not be implemented
3295
  on your platform.  If they are unavailable, using them will raise a
3296
  NotImplementedError.
3297
3298
Note that most operations will use the effective uid/gid, therefore this
3299
  routine can be used in a suid/sgid environment to test if the invoking
3300
  user has the specified access to the path.
3301
3302
[clinic start generated code]*/
3303
3304
static int
3305
os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
3306
               int effective_ids, int follow_symlinks)
3307
/*[clinic end generated code: output=cf84158bc90b1a77 input=c33565f7584b99e4]*/
3308
0
{
3309
0
    int return_value;
3310
3311
#ifdef MS_WINDOWS
3312
    DWORD attr;
3313
#else
3314
0
    int result;
3315
0
#endif
3316
3317
0
#ifdef HAVE_FACCESSAT
3318
0
    int faccessat_unavailable = 0;
3319
0
#endif
3320
3321
#ifndef HAVE_FACCESSAT
3322
    if (follow_symlinks_specified("access", follow_symlinks))
3323
        return -1;
3324
3325
    if (effective_ids) {
3326
        argument_unavailable_error("access", "effective_ids");
3327
        return -1;
3328
    }
3329
#endif
3330
3331
#ifdef MS_WINDOWS
3332
    Py_BEGIN_ALLOW_THREADS
3333
    attr = GetFileAttributesW(path->wide);
3334
    Py_END_ALLOW_THREADS
3335
3336
    /*
3337
     * Access is possible if
3338
     *   * we didn't get a -1, and
3339
     *     * write access wasn't requested,
3340
     *     * or the file isn't read-only,
3341
     *     * or it's a directory.
3342
     * (Directories cannot be read-only on Windows.)
3343
    */
3344
    return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
3345
            (!(mode & 2) ||
3346
            !(attr & FILE_ATTRIBUTE_READONLY) ||
3347
            (attr & FILE_ATTRIBUTE_DIRECTORY));
3348
#else
3349
3350
0
    Py_BEGIN_ALLOW_THREADS
3351
0
#ifdef HAVE_FACCESSAT
3352
0
    if ((dir_fd != DEFAULT_DIR_FD) ||
3353
0
        effective_ids ||
3354
0
        !follow_symlinks) {
3355
3356
0
        if (HAVE_FACCESSAT_RUNTIME) {
3357
0
            int flags = 0;
3358
0
            if (!follow_symlinks)
3359
0
                flags |= AT_SYMLINK_NOFOLLOW;
3360
0
            if (effective_ids)
3361
0
                flags |= AT_EACCESS;
3362
0
            result = faccessat(dir_fd, path->narrow, mode, flags);
3363
0
        } else {
3364
0
            faccessat_unavailable = 1;
3365
0
        }
3366
0
    }
3367
0
    else
3368
0
#endif
3369
0
        result = access(path->narrow, mode);
3370
0
    Py_END_ALLOW_THREADS
3371
3372
0
#ifdef HAVE_FACCESSAT
3373
0
    if (faccessat_unavailable) {
3374
0
        if (dir_fd != DEFAULT_DIR_FD) {
3375
0
            argument_unavailable_error("access", "dir_fd");
3376
0
            return -1;
3377
0
        }
3378
0
        if (follow_symlinks_specified("access", follow_symlinks))
3379
0
            return -1;
3380
3381
0
        if (effective_ids) {
3382
0
            argument_unavailable_error("access", "effective_ids");
3383
0
            return -1;
3384
0
        }
3385
        /* should be unreachable */
3386
0
        return -1;
3387
0
    }
3388
0
#endif
3389
0
    return_value = !result;
3390
0
#endif
3391
3392
0
    return return_value;
3393
0
}
3394
3395
#ifndef F_OK
3396
#define F_OK 0
3397
#endif
3398
#ifndef R_OK
3399
#define R_OK 4
3400
#endif
3401
#ifndef W_OK
3402
#define W_OK 2
3403
#endif
3404
#ifndef X_OK
3405
#define X_OK 1
3406
#endif
3407
3408
3409
#ifdef HAVE_TTYNAME_R
3410
/*[clinic input]
3411
os.ttyname
3412
3413
    fd: int
3414
        Integer file descriptor handle.
3415
3416
    /
3417
3418
Return the name of the terminal device connected to 'fd'.
3419
[clinic start generated code]*/
3420
3421
static PyObject *
3422
os_ttyname_impl(PyObject *module, int fd)
3423
/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
3424
0
{
3425
3426
0
    long size = sysconf(_SC_TTY_NAME_MAX);
3427
0
    if (size == -1) {
3428
0
        return posix_error();
3429
0
    }
3430
0
    char *buffer = (char *)PyMem_RawMalloc(size);
3431
0
    if (buffer == NULL) {
3432
0
        return PyErr_NoMemory();
3433
0
    }
3434
0
    int ret = ttyname_r(fd, buffer, size);
3435
0
    if (ret != 0) {
3436
0
        PyMem_RawFree(buffer);
3437
0
        errno = ret;
3438
0
        return posix_error();
3439
0
    }
3440
0
    PyObject *res = PyUnicode_DecodeFSDefault(buffer);
3441
0
    PyMem_RawFree(buffer);
3442
0
    return res;
3443
0
}
3444
#endif
3445
3446
#ifdef HAVE_CTERMID
3447
/*[clinic input]
3448
os.ctermid
3449
3450
Return the name of the controlling terminal for this process.
3451
[clinic start generated code]*/
3452
3453
static PyObject *
3454
os_ctermid_impl(PyObject *module)
3455
/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
3456
0
{
3457
0
    char *ret;
3458
0
    char buffer[L_ctermid];
3459
3460
#ifdef USE_CTERMID_R
3461
    ret = ctermid_r(buffer);
3462
#else
3463
0
    ret = ctermid(buffer);
3464
0
#endif
3465
0
    if (ret == NULL)
3466
0
        return posix_error();
3467
0
    return PyUnicode_DecodeFSDefault(buffer);
3468
0
}
3469
#endif /* HAVE_CTERMID */
3470
3471
3472
/*[clinic input]
3473
os.chdir
3474
3475
    path: path_t(allow_fd='PATH_HAVE_FCHDIR')
3476
3477
Change the current working directory to the specified path.
3478
3479
path may always be specified as a string.
3480
On some platforms, path may also be specified as an open file descriptor.
3481
If this functionality is unavailable, using it raises an exception.
3482
[clinic start generated code]*/
3483
3484
static PyObject *
3485
os_chdir_impl(PyObject *module, path_t *path)
3486
/*[clinic end generated code: output=3be6400eee26eaae input=a74ceab5d72adf74]*/
3487
0
{
3488
0
    int result;
3489
3490
0
    if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
3491
0
        return NULL;
3492
0
    }
3493
3494
0
    Py_BEGIN_ALLOW_THREADS
3495
#ifdef MS_WINDOWS
3496
    /* on unix, success = 0, on windows, success = !0 */
3497
    result = !win32_wchdir(path->wide);
3498
#else
3499
0
#ifdef HAVE_FCHDIR
3500
0
    if (path->fd != -1)
3501
0
        result = fchdir(path->fd);
3502
0
    else
3503
0
#endif
3504
0
        result = chdir(path->narrow);
3505
0
#endif
3506
0
    Py_END_ALLOW_THREADS
3507
3508
0
    if (result) {
3509
0
        return path_error(path);
3510
0
    }
3511
3512
0
    Py_RETURN_NONE;
3513
0
}
3514
3515
3516
#ifdef HAVE_FCHDIR
3517
/*[clinic input]
3518
os.fchdir
3519
3520
    fd: fildes
3521
3522
Change to the directory of the given file descriptor.
3523
3524
fd must be opened on a directory, not a file.
3525
Equivalent to os.chdir(fd).
3526
3527
[clinic start generated code]*/
3528
3529
static PyObject *
3530
os_fchdir_impl(PyObject *module, int fd)
3531
/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
3532
0
{
3533
0
    if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
3534
0
        return NULL;
3535
0
    }
3536
0
    return posix_fildes_fd(fd, fchdir);
3537
0
}
3538
#endif /* HAVE_FCHDIR */
3539
3540
#ifdef MS_WINDOWS
3541
# define CHMOD_DEFAULT_FOLLOW_SYMLINKS 0
3542
#else
3543
0
# define CHMOD_DEFAULT_FOLLOW_SYMLINKS 1
3544
#endif
3545
3546
#ifdef MS_WINDOWS
3547
static int
3548
win32_lchmod(LPCWSTR path, int mode)
3549
{
3550
    DWORD attr = GetFileAttributesW(path);
3551
    if (attr == INVALID_FILE_ATTRIBUTES) {
3552
        return 0;
3553
    }
3554
    if (mode & _S_IWRITE) {
3555
        attr &= ~FILE_ATTRIBUTE_READONLY;
3556
    }
3557
    else {
3558
        attr |= FILE_ATTRIBUTE_READONLY;
3559
    }
3560
    return SetFileAttributesW(path, attr);
3561
}
3562
3563
static int
3564
win32_hchmod(HANDLE hfile, int mode)
3565
{
3566
    FILE_BASIC_INFO info;
3567
    if (!GetFileInformationByHandleEx(hfile, FileBasicInfo,
3568
                                      &info, sizeof(info)))
3569
    {
3570
        return 0;
3571
    }
3572
    if (mode & _S_IWRITE) {
3573
        info.FileAttributes &= ~FILE_ATTRIBUTE_READONLY;
3574
    }
3575
    else {
3576
        info.FileAttributes |= FILE_ATTRIBUTE_READONLY;
3577
    }
3578
    return SetFileInformationByHandle(hfile, FileBasicInfo,
3579
                                      &info, sizeof(info));
3580
}
3581
3582
static int
3583
win32_fchmod(int fd, int mode)
3584
{
3585
    HANDLE hfile = _Py_get_osfhandle_noraise(fd);
3586
    if (hfile == INVALID_HANDLE_VALUE) {
3587
        SetLastError(ERROR_INVALID_HANDLE);
3588
        return 0;
3589
    }
3590
    return win32_hchmod(hfile, mode);
3591
}
3592
3593
#endif /* MS_WINDOWS */
3594
3595
/*[clinic input]
3596
os.chmod
3597
3598
    path: path_t(allow_fd='PATH_HAVE_FCHMOD')
3599
        Path to be modified.  May always be specified as a str, bytes, or a path-like object.
3600
        On some platforms, path may also be specified as an open file descriptor.
3601
        If this functionality is unavailable, using it raises an exception.
3602
3603
    mode: int
3604
        Operating-system mode bitfield.
3605
        Be careful when using number literals for *mode*. The conventional UNIX notation for
3606
        numeric modes uses an octal base, which needs to be indicated with a ``0o`` prefix in
3607
        Python.
3608
3609
    *
3610
3611
    dir_fd : dir_fd(requires='fchmodat') = None
3612
        If not None, it should be a file descriptor open to a directory,
3613
        and path should be relative; path will then be relative to that
3614
        directory.
3615
3616
    follow_symlinks: bool(c_default="CHMOD_DEFAULT_FOLLOW_SYMLINKS", \
3617
                          py_default="(os.name != 'nt')") = CHMOD_DEFAULT_FOLLOW_SYMLINKS
3618
        If False, and the last element of the path is a symbolic link,
3619
        chmod will modify the symbolic link itself instead of the file
3620
        the link points to.
3621
3622
Change the access permissions of a file.
3623
3624
It is an error to use dir_fd or follow_symlinks when specifying path as
3625
  an open file descriptor.
3626
dir_fd and follow_symlinks may not be implemented on your platform.
3627
  If they are unavailable, using them will raise a NotImplementedError.
3628
3629
[clinic start generated code]*/
3630
3631
static PyObject *
3632
os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
3633
              int follow_symlinks)
3634
/*[clinic end generated code: output=5cf6a94915cc7bff input=fcf115d174b9f3d8]*/
3635
0
{
3636
0
    int result;
3637
3638
0
#ifdef HAVE_FCHMODAT
3639
0
    int fchmodat_nofollow_unsupported = 0;
3640
0
    int fchmodat_unsupported = 0;
3641
0
#endif
3642
3643
#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD) || defined(MS_WINDOWS))
3644
    if (follow_symlinks_specified("chmod", follow_symlinks))
3645
        return NULL;
3646
#endif
3647
3648
0
    if (PySys_Audit("os.chmod", "Oii", path->object, mode,
3649
0
                    dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3650
0
        return NULL;
3651
0
    }
3652
3653
#ifdef MS_WINDOWS
3654
    result = 0;
3655
    Py_BEGIN_ALLOW_THREADS
3656
    if (path->fd != -1) {
3657
        result = win32_fchmod(path->fd, mode);
3658
    }
3659
    else if (follow_symlinks) {
3660
        HANDLE hfile = CreateFileW(path->wide,
3661
                                   FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES,
3662
                                   0, NULL,
3663
                                   OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
3664
        if (hfile != INVALID_HANDLE_VALUE) {
3665
            result = win32_hchmod(hfile, mode);
3666
            (void)CloseHandle(hfile);
3667
        }
3668
    }
3669
    else {
3670
        result = win32_lchmod(path->wide, mode);
3671
    }
3672
    Py_END_ALLOW_THREADS
3673
    if (!result) {
3674
        return path_error(path);
3675
    }
3676
#else /* MS_WINDOWS */
3677
0
    Py_BEGIN_ALLOW_THREADS
3678
0
#ifdef HAVE_FCHMOD
3679
0
    if (path->fd != -1)
3680
0
        result = fchmod(path->fd, mode);
3681
0
    else
3682
0
#endif /* HAVE_CHMOD */
3683
#ifdef HAVE_LCHMOD
3684
    if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
3685
        result = lchmod(path->narrow, mode);
3686
    else
3687
#endif /* HAVE_LCHMOD */
3688
0
#ifdef HAVE_FCHMODAT
3689
0
    if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
3690
0
        if (HAVE_FCHMODAT_RUNTIME) {
3691
            /*
3692
             * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3693
             * The documentation specifically shows how to use it,
3694
             * and then says it isn't implemented yet.
3695
             * (true on linux with glibc 2.15, and openindiana 3.x)
3696
             *
3697
             * Once it is supported, os.chmod will automatically
3698
             * support dir_fd and follow_symlinks=False.  (Hopefully.)
3699
             * Until then, we need to be careful what exception we raise.
3700
             */
3701
0
            result = fchmodat(dir_fd, path->narrow, mode,
3702
0
                              follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3703
            /*
3704
             * But wait!  We can't throw the exception without allowing threads,
3705
             * and we can't do that in this nested scope.  (Macro trickery, sigh.)
3706
             */
3707
0
            fchmodat_nofollow_unsupported =
3708
0
                             result &&
3709
0
                             ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3710
0
                             !follow_symlinks;
3711
0
        } else {
3712
0
            fchmodat_unsupported = 1;
3713
0
            fchmodat_nofollow_unsupported = 1;
3714
3715
0
            result = -1;
3716
0
        }
3717
0
    }
3718
0
    else
3719
0
#endif /* HAVE_FHCMODAT */
3720
0
    {
3721
0
#ifdef HAVE_CHMOD
3722
0
        result = chmod(path->narrow, mode);
3723
#elif defined(__wasi__)
3724
        // WASI SDK 15.0 does not support chmod.
3725
        // Ignore missing syscall for now.
3726
        result = 0;
3727
#else
3728
        result = -1;
3729
        errno = ENOSYS;
3730
#endif
3731
0
    }
3732
0
    Py_END_ALLOW_THREADS
3733
3734
0
    if (result) {
3735
0
#ifdef HAVE_FCHMODAT
3736
0
        if (fchmodat_unsupported) {
3737
0
            if (dir_fd != DEFAULT_DIR_FD) {
3738
0
                argument_unavailable_error("chmod", "dir_fd");
3739
0
                return NULL;
3740
0
            }
3741
0
        }
3742
3743
0
        if (fchmodat_nofollow_unsupported) {
3744
0
            if (dir_fd != DEFAULT_DIR_FD)
3745
0
                dir_fd_and_follow_symlinks_invalid("chmod",
3746
0
                                                   dir_fd, follow_symlinks);
3747
0
            else
3748
0
                follow_symlinks_specified("chmod", follow_symlinks);
3749
0
            return NULL;
3750
0
        }
3751
0
        else
3752
0
#endif /* HAVE_FCHMODAT */
3753
0
        return path_error(path);
3754
0
    }
3755
0
#endif /* MS_WINDOWS */
3756
3757
0
    Py_RETURN_NONE;
3758
0
}
3759
3760
3761
#if defined(HAVE_FCHMOD) || defined(MS_WINDOWS)
3762
/*[clinic input]
3763
os.fchmod
3764
3765
    fd: int
3766
        The file descriptor of the file to be modified.
3767
    mode: int
3768
        Operating-system mode bitfield.
3769
        Be careful when using number literals for *mode*. The conventional UNIX notation for
3770
        numeric modes uses an octal base, which needs to be indicated with a ``0o`` prefix in
3771
        Python.
3772
3773
Change the access permissions of the file given by file descriptor fd.
3774
3775
Equivalent to os.chmod(fd, mode).
3776
[clinic start generated code]*/
3777
3778
static PyObject *
3779
os_fchmod_impl(PyObject *module, int fd, int mode)
3780
/*[clinic end generated code: output=afd9bc05b4e426b3 input=b5594618bbbc22df]*/
3781
0
{
3782
0
    int res;
3783
3784
0
    if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
3785
0
        return NULL;
3786
0
    }
3787
3788
#ifdef MS_WINDOWS
3789
    res = 0;
3790
    Py_BEGIN_ALLOW_THREADS
3791
    res = win32_fchmod(fd, mode);
3792
    Py_END_ALLOW_THREADS
3793
    if (!res) {
3794
        return PyErr_SetFromWindowsErr(0);
3795
    }
3796
#else /* MS_WINDOWS */
3797
0
    int async_err = 0;
3798
0
    do {
3799
0
        Py_BEGIN_ALLOW_THREADS
3800
0
        res = fchmod(fd, mode);
3801
0
        Py_END_ALLOW_THREADS
3802
0
    } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3803
0
    if (res != 0)
3804
0
        return (!async_err) ? posix_error() : NULL;
3805
0
#endif /* MS_WINDOWS */
3806
3807
0
    Py_RETURN_NONE;
3808
0
}
3809
#endif /* HAVE_FCHMOD || MS_WINDOWS */
3810
3811
3812
#if defined(HAVE_LCHMOD) || defined(MS_WINDOWS)
3813
/*[clinic input]
3814
os.lchmod
3815
3816
    path: path_t
3817
    mode: int
3818
3819
Change the access permissions of a file, without following symbolic links.
3820
3821
If path is a symlink, this affects the link itself rather than the target.
3822
Equivalent to chmod(path, mode, follow_symlinks=False)."
3823
[clinic start generated code]*/
3824
3825
static PyObject *
3826
os_lchmod_impl(PyObject *module, path_t *path, int mode)
3827
/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
3828
{
3829
    int res;
3830
    if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
3831
        return NULL;
3832
    }
3833
#ifdef MS_WINDOWS
3834
    Py_BEGIN_ALLOW_THREADS
3835
    res = win32_lchmod(path->wide, mode);
3836
    Py_END_ALLOW_THREADS
3837
    if (!res) {
3838
        path_error(path);
3839
        return NULL;
3840
    }
3841
#else /* MS_WINDOWS */
3842
    Py_BEGIN_ALLOW_THREADS
3843
    res = lchmod(path->narrow, mode);
3844
    Py_END_ALLOW_THREADS
3845
    if (res < 0) {
3846
        path_error(path);
3847
        return NULL;
3848
    }
3849
#endif /* MS_WINDOWS */
3850
    Py_RETURN_NONE;
3851
}
3852
#endif /* HAVE_LCHMOD || MS_WINDOWS */
3853
3854
3855
#ifdef HAVE_CHFLAGS
3856
/*[clinic input]
3857
os.chflags
3858
3859
    path: path_t
3860
    flags: unsigned_long(bitwise=True)
3861
    follow_symlinks: bool=True
3862
3863
Set file flags.
3864
3865
If follow_symlinks is False, and the last element of the path is a symbolic
3866
  link, chflags will change flags on the symbolic link itself instead of the
3867
  file the link points to.
3868
follow_symlinks may not be implemented on your platform.  If it is
3869
unavailable, using it will raise a NotImplementedError.
3870
3871
[clinic start generated code]*/
3872
3873
static PyObject *
3874
os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
3875
                int follow_symlinks)
3876
/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
3877
{
3878
    int result;
3879
3880
#ifndef HAVE_LCHFLAGS
3881
    if (follow_symlinks_specified("chflags", follow_symlinks))
3882
        return NULL;
3883
#endif
3884
3885
    if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3886
        return NULL;
3887
    }
3888
3889
    Py_BEGIN_ALLOW_THREADS
3890
#ifdef HAVE_LCHFLAGS
3891
    if (!follow_symlinks)
3892
        result = lchflags(path->narrow, flags);
3893
    else
3894
#endif
3895
        result = chflags(path->narrow, flags);
3896
    Py_END_ALLOW_THREADS
3897
3898
    if (result)
3899
        return path_error(path);
3900
3901
    Py_RETURN_NONE;
3902
}
3903
#endif /* HAVE_CHFLAGS */
3904
3905
3906
#ifdef HAVE_LCHFLAGS
3907
/*[clinic input]
3908
os.lchflags
3909
3910
    path: path_t
3911
    flags: unsigned_long(bitwise=True)
3912
3913
Set file flags.
3914
3915
This function will not follow symbolic links.
3916
Equivalent to chflags(path, flags, follow_symlinks=False).
3917
[clinic start generated code]*/
3918
3919
static PyObject *
3920
os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3921
/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
3922
{
3923
    int res;
3924
    if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3925
        return NULL;
3926
    }
3927
    Py_BEGIN_ALLOW_THREADS
3928
    res = lchflags(path->narrow, flags);
3929
    Py_END_ALLOW_THREADS
3930
    if (res < 0) {
3931
        return path_error(path);
3932
    }
3933
    Py_RETURN_NONE;
3934
}
3935
#endif /* HAVE_LCHFLAGS */
3936
3937
3938
#ifdef HAVE_CHROOT
3939
/*[clinic input]
3940
os.chroot
3941
    path: path_t
3942
3943
Change root directory to path.
3944
3945
[clinic start generated code]*/
3946
3947
static PyObject *
3948
os_chroot_impl(PyObject *module, path_t *path)
3949
/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
3950
0
{
3951
0
    int res;
3952
0
    Py_BEGIN_ALLOW_THREADS
3953
0
    res = chroot(path->narrow);
3954
0
    Py_END_ALLOW_THREADS
3955
0
    if (res < 0)
3956
0
        return path_error(path);
3957
0
    Py_RETURN_NONE;
3958
0
}
3959
#endif /* HAVE_CHROOT */
3960
3961
3962
#ifdef HAVE_FSYNC
3963
/*[clinic input]
3964
os.fsync
3965
3966
    fd: fildes
3967
3968
Force write of fd to disk.
3969
[clinic start generated code]*/
3970
3971
static PyObject *
3972
os_fsync_impl(PyObject *module, int fd)
3973
/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
3974
0
{
3975
0
    return posix_fildes_fd(fd, fsync);
3976
0
}
3977
#endif /* HAVE_FSYNC */
3978
3979
3980
#ifdef HAVE_SYNC
3981
/*[clinic input]
3982
os.sync
3983
3984
Force write of everything to disk.
3985
[clinic start generated code]*/
3986
3987
static PyObject *
3988
os_sync_impl(PyObject *module)
3989
/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
3990
0
{
3991
0
    Py_BEGIN_ALLOW_THREADS
3992
0
    sync();
3993
0
    Py_END_ALLOW_THREADS
3994
0
    Py_RETURN_NONE;
3995
0
}
3996
#endif /* HAVE_SYNC */
3997
3998
3999
#ifdef HAVE_FDATASYNC
4000
#ifdef __hpux
4001
extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
4002
#endif
4003
4004
/*[clinic input]
4005
os.fdatasync
4006
4007
    fd: fildes
4008
4009
Force write of fd to disk without forcing update of metadata.
4010
[clinic start generated code]*/
4011
4012
static PyObject *
4013
os_fdatasync_impl(PyObject *module, int fd)
4014
/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
4015
0
{
4016
0
    return posix_fildes_fd(fd, fdatasync);
4017
0
}
4018
#endif /* HAVE_FDATASYNC */
4019
4020
4021
#ifdef HAVE_CHOWN
4022
/*[clinic input]
4023
os.chown
4024
4025
    path : path_t(allow_fd='PATH_HAVE_FCHOWN')
4026
        Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
4027
4028
    uid: uid_t
4029
4030
    gid: gid_t
4031
4032
    *
4033
4034
    dir_fd : dir_fd(requires='fchownat') = None
4035
        If not None, it should be a file descriptor open to a directory,
4036
        and path should be relative; path will then be relative to that
4037
        directory.
4038
4039
    follow_symlinks: bool = True
4040
        If False, and the last element of the path is a symbolic link,
4041
        stat will examine the symbolic link itself instead of the file
4042
        the link points to.
4043
4044
Change the owner and group id of path to the numeric uid and gid.\
4045
4046
path may always be specified as a string.
4047
On some platforms, path may also be specified as an open file descriptor.
4048
  If this functionality is unavailable, using it raises an exception.
4049
If dir_fd is not None, it should be a file descriptor open to a directory,
4050
  and path should be relative; path will then be relative to that directory.
4051
If follow_symlinks is False, and the last element of the path is a symbolic
4052
  link, chown will modify the symbolic link itself instead of the file the
4053
  link points to.
4054
It is an error to use dir_fd or follow_symlinks when specifying path as
4055
  an open file descriptor.
4056
dir_fd and follow_symlinks may not be implemented on your platform.
4057
  If they are unavailable, using them will raise a NotImplementedError.
4058
4059
[clinic start generated code]*/
4060
4061
static PyObject *
4062
os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
4063
              int dir_fd, int follow_symlinks)
4064
/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
4065
0
{
4066
0
    int result;
4067
4068
0
#if defined(HAVE_FCHOWNAT)
4069
0
    int fchownat_unsupported = 0;
4070
0
#endif
4071
4072
#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
4073
    if (follow_symlinks_specified("chown", follow_symlinks))
4074
        return NULL;
4075
#endif
4076
0
    if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
4077
0
        fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
4078
0
        return NULL;
4079
4080
0
    if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
4081
0
                    dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4082
0
        return NULL;
4083
0
    }
4084
4085
0
    Py_BEGIN_ALLOW_THREADS
4086
0
#ifdef HAVE_FCHOWN
4087
0
    if (path->fd != -1)
4088
0
        result = fchown(path->fd, uid, gid);
4089
0
    else
4090
0
#endif
4091
0
#ifdef HAVE_LCHOWN
4092
0
    if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
4093
0
        result = lchown(path->narrow, uid, gid);
4094
0
    else
4095
0
#endif
4096
0
#ifdef HAVE_FCHOWNAT
4097
0
    if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
4098
0
      if (HAVE_FCHOWNAT_RUNTIME) {
4099
0
        result = fchownat(dir_fd, path->narrow, uid, gid,
4100
0
                          follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
4101
0
      } else {
4102
0
         fchownat_unsupported = 1;
4103
0
      }
4104
0
    } else
4105
0
#endif
4106
0
        result = chown(path->narrow, uid, gid);
4107
0
    Py_END_ALLOW_THREADS
4108
4109
0
#ifdef HAVE_FCHOWNAT
4110
0
    if (fchownat_unsupported) {
4111
        /* This would be incorrect if the current platform
4112
         * doesn't support lchown.
4113
         */
4114
0
        argument_unavailable_error(NULL, "dir_fd");
4115
0
        return NULL;
4116
0
    }
4117
0
#endif
4118
4119
0
    if (result)
4120
0
        return path_error(path);
4121
4122
0
    Py_RETURN_NONE;
4123
0
}
4124
#endif /* HAVE_CHOWN */
4125
4126
4127
#ifdef HAVE_FCHOWN
4128
/*[clinic input]
4129
os.fchown
4130
4131
    fd: int
4132
    uid: uid_t
4133
    gid: gid_t
4134
4135
Change the owner and group id of the file specified by file descriptor.
4136
4137
Equivalent to os.chown(fd, uid, gid).
4138
4139
[clinic start generated code]*/
4140
4141
static PyObject *
4142
os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
4143
/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
4144
0
{
4145
0
    int res;
4146
0
    int async_err = 0;
4147
4148
0
    if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
4149
0
        return NULL;
4150
0
    }
4151
4152
0
    do {
4153
0
        Py_BEGIN_ALLOW_THREADS
4154
0
        res = fchown(fd, uid, gid);
4155
0
        Py_END_ALLOW_THREADS
4156
0
    } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
4157
0
    if (res != 0)
4158
0
        return (!async_err) ? posix_error() : NULL;
4159
4160
0
    Py_RETURN_NONE;
4161
0
}
4162
#endif /* HAVE_FCHOWN */
4163
4164
4165
#ifdef HAVE_LCHOWN
4166
/*[clinic input]
4167
os.lchown
4168
4169
    path : path_t
4170
    uid: uid_t
4171
    gid: gid_t
4172
4173
Change the owner and group id of path to the numeric uid and gid.
4174
4175
This function will not follow symbolic links.
4176
Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
4177
[clinic start generated code]*/
4178
4179
static PyObject *
4180
os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
4181
/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
4182
0
{
4183
0
    int res;
4184
0
    if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
4185
0
        return NULL;
4186
0
    }
4187
0
    Py_BEGIN_ALLOW_THREADS
4188
0
    res = lchown(path->narrow, uid, gid);
4189
0
    Py_END_ALLOW_THREADS
4190
0
    if (res < 0) {
4191
0
        return path_error(path);
4192
0
    }
4193
0
    Py_RETURN_NONE;
4194
0
}
4195
#endif /* HAVE_LCHOWN */
4196
4197
4198
static PyObject *
4199
posix_getcwd(int use_bytes)
4200
0
{
4201
#ifdef MS_WINDOWS
4202
    wchar_t wbuf[MAXPATHLEN];
4203
    wchar_t *wbuf2 = wbuf;
4204
    DWORD len;
4205
4206
    Py_BEGIN_ALLOW_THREADS
4207
    len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
4208
    /* If the buffer is large enough, len does not include the
4209
       terminating \0. If the buffer is too small, len includes
4210
       the space needed for the terminator. */
4211
    if (len >= Py_ARRAY_LENGTH(wbuf)) {
4212
        if ((Py_ssize_t)len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
4213
            wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
4214
        }
4215
        else {
4216
            wbuf2 = NULL;
4217
        }
4218
        if (wbuf2) {
4219
            len = GetCurrentDirectoryW(len, wbuf2);
4220
        }
4221
    }
4222
    Py_END_ALLOW_THREADS
4223
4224
    if (!wbuf2) {
4225
        PyErr_NoMemory();
4226
        return NULL;
4227
    }
4228
    if (!len) {
4229
        PyErr_SetFromWindowsErr(0);
4230
        if (wbuf2 != wbuf)
4231
            PyMem_RawFree(wbuf2);
4232
        return NULL;
4233
    }
4234
4235
    PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
4236
    if (wbuf2 != wbuf) {
4237
        PyMem_RawFree(wbuf2);
4238
    }
4239
4240
    if (use_bytes) {
4241
        if (resobj == NULL) {
4242
            return NULL;
4243
        }
4244
        Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
4245
    }
4246
4247
    return resobj;
4248
#else
4249
0
    const size_t chunk = 1024;
4250
4251
0
    char *buf = NULL;
4252
0
    char *cwd = NULL;
4253
0
    size_t buflen = 0;
4254
4255
0
    Py_BEGIN_ALLOW_THREADS
4256
0
    do {
4257
0
        char *newbuf;
4258
0
        if (buflen <= PY_SSIZE_T_MAX - chunk) {
4259
0
            buflen += chunk;
4260
0
            newbuf = PyMem_RawRealloc(buf, buflen);
4261
0
        }
4262
0
        else {
4263
0
            newbuf = NULL;
4264
0
        }
4265
0
        if (newbuf == NULL) {
4266
0
            PyMem_RawFree(buf);
4267
0
            buf = NULL;
4268
0
            break;
4269
0
        }
4270
0
        buf = newbuf;
4271
4272
0
        cwd = getcwd(buf, buflen);
4273
0
    } while (cwd == NULL && errno == ERANGE);
4274
0
    Py_END_ALLOW_THREADS
4275
4276
0
    if (buf == NULL) {
4277
0
        return PyErr_NoMemory();
4278
0
    }
4279
0
    if (cwd == NULL) {
4280
0
        posix_error();
4281
0
        PyMem_RawFree(buf);
4282
0
        return NULL;
4283
0
    }
4284
4285
0
    PyObject *obj;
4286
0
    if (use_bytes) {
4287
0
        obj = PyBytes_FromStringAndSize(buf, strlen(buf));
4288
0
    }
4289
0
    else {
4290
0
        obj = PyUnicode_DecodeFSDefault(buf);
4291
0
    }
4292
0
#ifdef __linux__
4293
0
    if (buf[0] != '/') {
4294
        /*
4295
         * On Linux >= 2.6.36 with glibc < 2.27, getcwd() can return a
4296
         * relative pathname starting with '(unreachable)'. We detect this
4297
         * and fail with ENOENT, matching newer glibc behaviour.
4298
         */
4299
0
        errno = ENOENT;
4300
0
        path_object_error(obj);
4301
0
        PyMem_RawFree(buf);
4302
0
        return NULL;
4303
0
    }
4304
0
#endif
4305
0
    assert(buf[0] == '/');
4306
0
    PyMem_RawFree(buf);
4307
4308
0
    return obj;
4309
0
#endif   /* !MS_WINDOWS */
4310
0
}
4311
4312
4313
/*[clinic input]
4314
os.getcwd
4315
4316
Return a unicode string representing the current working directory.
4317
[clinic start generated code]*/
4318
4319
static PyObject *
4320
os_getcwd_impl(PyObject *module)
4321
/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
4322
0
{
4323
0
    return posix_getcwd(0);
4324
0
}
4325
4326
4327
/*[clinic input]
4328
os.getcwdb
4329
4330
Return a bytes string representing the current working directory.
4331
[clinic start generated code]*/
4332
4333
static PyObject *
4334
os_getcwdb_impl(PyObject *module)
4335
/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
4336
0
{
4337
0
    return posix_getcwd(1);
4338
0
}
4339
4340
4341
#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
4342
#define HAVE_LINK 1
4343
#endif
4344
4345
#ifdef HAVE_LINK
4346
/*[clinic input]
4347
4348
@permit_long_docstring_body
4349
os.link
4350
4351
    src : path_t
4352
    dst : path_t
4353
    *
4354
    src_dir_fd : dir_fd = None
4355
    dst_dir_fd : dir_fd = None
4356
    follow_symlinks: bool(c_default="-1", py_default="(os.name != 'nt')") = PLACEHOLDER
4357
4358
Create a hard link to a file.
4359
4360
If either src_dir_fd or dst_dir_fd is not None, it should be a file
4361
  descriptor open to a directory, and the respective path string (src or dst)
4362
  should be relative; the path will then be relative to that directory.
4363
If follow_symlinks is False, and the last element of src is a symbolic
4364
  link, link will create a link to the symbolic link itself instead of the
4365
  file the link points to.
4366
src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
4367
  platform.  If they are unavailable, using them will raise a
4368
  NotImplementedError.
4369
[clinic start generated code]*/
4370
4371
static PyObject *
4372
os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4373
             int dst_dir_fd, int follow_symlinks)
4374
/*[clinic end generated code: output=7f00f6007fd5269a input=e2a50a6497050e44]*/
4375
0
{
4376
#ifdef MS_WINDOWS
4377
    BOOL result = FALSE;
4378
#else
4379
0
    int result;
4380
0
#endif
4381
4382
0
#ifdef HAVE_LINKAT
4383
0
    if (HAVE_LINKAT_RUNTIME) {
4384
0
        if (follow_symlinks < 0) {
4385
0
            follow_symlinks = 1;
4386
0
        }
4387
0
    }
4388
0
    else
4389
0
#endif
4390
0
    {
4391
0
        if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
4392
0
            argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
4393
0
            return NULL;
4394
0
        }
4395
/* See issue 85527: link() on Linux works like linkat without AT_SYMLINK_FOLLOW,
4396
   but on Mac it works like linkat *with* AT_SYMLINK_FOLLOW. */
4397
0
#if defined(MS_WINDOWS) || defined(__linux__)
4398
0
        if (follow_symlinks == 1) {
4399
0
            argument_unavailable_error("link", "follow_symlinks=True");
4400
0
            return NULL;
4401
0
        }
4402
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || (defined(__sun) && defined(__SVR4))
4403
        if (follow_symlinks == 0) {
4404
            argument_unavailable_error("link", "follow_symlinks=False");
4405
            return NULL;
4406
        }
4407
#else
4408
        if (follow_symlinks >= 0) {
4409
            argument_unavailable_error("link", "follow_symlinks");
4410
            return NULL;
4411
        }
4412
#endif
4413
0
    }
4414
4415
0
    if (PySys_Audit("os.link", "OOii", src->object, dst->object,
4416
0
                    src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
4417
0
                    dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
4418
0
        return NULL;
4419
0
    }
4420
4421
#ifdef MS_WINDOWS
4422
    Py_BEGIN_ALLOW_THREADS
4423
    result = CreateHardLinkW(dst->wide, src->wide, NULL);
4424
    Py_END_ALLOW_THREADS
4425
4426
    if (!result)
4427
        return path_error2(src, dst);
4428
#else
4429
0
    Py_BEGIN_ALLOW_THREADS
4430
0
#ifdef HAVE_LINKAT
4431
0
    if (HAVE_LINKAT_RUNTIME) {
4432
0
        result = linkat(src_dir_fd, src->narrow,
4433
0
            dst_dir_fd, dst->narrow,
4434
0
            follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
4435
0
    }
4436
0
    else
4437
0
#endif
4438
0
    {
4439
        /* linkat not available */
4440
0
        result = link(src->narrow, dst->narrow);
4441
0
    }
4442
0
    Py_END_ALLOW_THREADS
4443
4444
0
    if (result)
4445
0
        return path_error2(src, dst);
4446
0
#endif /* MS_WINDOWS */
4447
4448
0
    Py_RETURN_NONE;
4449
0
}
4450
#endif
4451
4452
4453
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
4454
static PyObject *
4455
_listdir_windows_no_opendir(path_t *path, PyObject *list)
4456
{
4457
    PyObject *v;
4458
    HANDLE hFindFile = INVALID_HANDLE_VALUE;
4459
    BOOL result, return_bytes;
4460
    wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
4461
    /* only claim to have space for MAX_PATH */
4462
    Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
4463
    wchar_t *wnamebuf = NULL;
4464
4465
    WIN32_FIND_DATAW wFileData;
4466
    const wchar_t *po_wchars;
4467
4468
    if (!path->wide) { /* Default arg: "." */
4469
        po_wchars = L".";
4470
        len = 1;
4471
        return_bytes = 0;
4472
    } else {
4473
        po_wchars = path->wide;
4474
        len = wcslen(path->wide);
4475
        return_bytes = PyBytes_Check(path->object);
4476
    }
4477
    /* The +5 is so we can append "\\*.*\0" */
4478
    wnamebuf = PyMem_New(wchar_t, len + 5);
4479
    if (!wnamebuf) {
4480
        PyErr_NoMemory();
4481
        goto exit;
4482
    }
4483
    wcscpy(wnamebuf, po_wchars);
4484
    if (len > 0) {
4485
        wchar_t wch = wnamebuf[len-1];
4486
        if (wch != SEP && wch != ALTSEP && wch != L':')
4487
            wnamebuf[len++] = SEP;
4488
        wcscpy(wnamebuf + len, L"*.*");
4489
    }
4490
    if ((list = PyList_New(0)) == NULL) {
4491
        goto exit;
4492
    }
4493
    Py_BEGIN_ALLOW_THREADS
4494
    hFindFile = FindFirstFileW(wnamebuf, &wFileData);
4495
    Py_END_ALLOW_THREADS
4496
    if (hFindFile == INVALID_HANDLE_VALUE) {
4497
        int error = GetLastError();
4498
        if (error == ERROR_FILE_NOT_FOUND)
4499
            goto exit;
4500
        path_error(path);
4501
        Py_CLEAR(list);
4502
        goto exit;
4503
    }
4504
    do {
4505
        /* Skip over . and .. */
4506
        if (wcscmp(wFileData.cFileName, L".") != 0 &&
4507
            wcscmp(wFileData.cFileName, L"..") != 0) {
4508
            v = PyUnicode_FromWideChar(wFileData.cFileName,
4509
                                       wcslen(wFileData.cFileName));
4510
            if (return_bytes && v) {
4511
                Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
4512
            }
4513
            if (v == NULL) {
4514
                Py_CLEAR(list);
4515
                break;
4516
            }
4517
            if (PyList_Append(list, v) != 0) {
4518
                Py_DECREF(v);
4519
                Py_CLEAR(list);
4520
                break;
4521
            }
4522
            Py_DECREF(v);
4523
        }
4524
        Py_BEGIN_ALLOW_THREADS
4525
        result = FindNextFileW(hFindFile, &wFileData);
4526
        Py_END_ALLOW_THREADS
4527
        /* FindNextFile sets error to ERROR_NO_MORE_FILES if
4528
           it got to the end of the directory. */
4529
        if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
4530
            path_error(path);
4531
            Py_CLEAR(list);
4532
            goto exit;
4533
        }
4534
    } while (result == TRUE);
4535
4536
exit:
4537
    if (hFindFile != INVALID_HANDLE_VALUE) {
4538
        if (FindClose(hFindFile) == FALSE) {
4539
            if (list != NULL) {
4540
                path_error(path);
4541
                Py_CLEAR(list);
4542
            }
4543
        }
4544
    }
4545
    PyMem_Free(wnamebuf);
4546
4547
    return list;
4548
}  /* end of _listdir_windows_no_opendir */
4549
4550
#else  /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
4551
4552
static PyObject *
4553
_posix_listdir(path_t *path, PyObject *list)
4554
120
{
4555
120
    PyObject *v;
4556
120
    DIR *dirp = NULL;
4557
120
    struct dirent *ep;
4558
120
    int return_str; /* if false, return bytes */
4559
120
#ifdef HAVE_FDOPENDIR
4560
120
    int fd = -1;
4561
120
#endif
4562
4563
120
    errno = 0;
4564
120
#ifdef HAVE_FDOPENDIR
4565
120
    if (path->fd != -1) {
4566
0
      if (HAVE_FDOPENDIR_RUNTIME) {
4567
        /* closedir() closes the FD, so we duplicate it */
4568
0
        fd = _Py_dup(path->fd);
4569
0
        if (fd == -1)
4570
0
            return NULL;
4571
4572
0
        return_str = 1;
4573
4574
0
        Py_BEGIN_ALLOW_THREADS
4575
0
        dirp = fdopendir(fd);
4576
0
        Py_END_ALLOW_THREADS
4577
0
      } else {
4578
0
        PyErr_SetString(PyExc_TypeError,
4579
0
            "listdir: path should be string, bytes, os.PathLike or None, not int");
4580
0
        return NULL;
4581
0
      }
4582
0
    }
4583
120
    else
4584
120
#endif
4585
120
    {
4586
120
        const char *name;
4587
120
        if (path->narrow) {
4588
120
            name = path->narrow;
4589
            /* only return bytes if they specified a bytes object */
4590
120
            return_str = !PyBytes_Check(path->object);
4591
120
        }
4592
0
        else {
4593
0
            name = ".";
4594
0
            return_str = 1;
4595
0
        }
4596
4597
120
        Py_BEGIN_ALLOW_THREADS
4598
120
        dirp = opendir(name);
4599
120
        Py_END_ALLOW_THREADS
4600
120
    }
4601
4602
120
    if (dirp == NULL) {
4603
0
        path_error(path);
4604
0
        list = NULL;
4605
0
#ifdef HAVE_FDOPENDIR
4606
0
        if (fd != -1) {
4607
0
            Py_BEGIN_ALLOW_THREADS
4608
0
            close(fd);
4609
0
            Py_END_ALLOW_THREADS
4610
0
        }
4611
0
#endif
4612
0
        goto exit;
4613
0
    }
4614
120
    if ((list = PyList_New(0)) == NULL) {
4615
0
        goto exit;
4616
0
    }
4617
7.05k
    for (;;) {
4618
7.05k
        errno = 0;
4619
7.05k
        Py_BEGIN_ALLOW_THREADS
4620
7.05k
        ep = readdir(dirp);
4621
7.05k
        Py_END_ALLOW_THREADS
4622
7.05k
        if (ep == NULL) {
4623
120
            if (errno == 0) {
4624
120
                break;
4625
120
            } else {
4626
0
                path_error(path);
4627
0
                Py_CLEAR(list);
4628
0
                goto exit;
4629
0
            }
4630
120
        }
4631
6.93k
        if (ep->d_name[0] == '.' &&
4632
6.93k
            (NAMLEN(ep) == 1 ||
4633
240
             (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
4634
240
            continue;
4635
6.69k
        if (return_str)
4636
6.69k
            v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
4637
0
        else
4638
0
            v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
4639
6.69k
        if (v == NULL) {
4640
0
            Py_CLEAR(list);
4641
0
            break;
4642
0
        }
4643
6.69k
        if (PyList_Append(list, v) != 0) {
4644
0
            Py_DECREF(v);
4645
0
            Py_CLEAR(list);
4646
0
            break;
4647
0
        }
4648
6.69k
        Py_DECREF(v);
4649
6.69k
    }
4650
4651
120
exit:
4652
120
    if (dirp != NULL) {
4653
120
        Py_BEGIN_ALLOW_THREADS
4654
120
#ifdef HAVE_FDOPENDIR
4655
120
        if (fd > -1)
4656
0
            rewinddir(dirp);
4657
120
#endif
4658
120
        closedir(dirp);
4659
120
        Py_END_ALLOW_THREADS
4660
120
    }
4661
4662
120
    return list;
4663
120
}  /* end of _posix_listdir */
4664
#endif  /* which OS */
4665
4666
4667
/*[clinic input]
4668
@permit_long_docstring_body
4669
os.listdir
4670
4671
    path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
4672
4673
Return a list containing the names of the files in the directory.
4674
4675
path can be specified as either str, bytes, or a path-like object.  If path is bytes,
4676
  the filenames returned will also be bytes; in all other circumstances
4677
  the filenames returned will be str.
4678
If path is None, uses the path='.'.
4679
On some platforms, path may also be specified as an open file descriptor;\
4680
  the file descriptor must refer to a directory.
4681
  If this functionality is unavailable, using it raises NotImplementedError.
4682
4683
The list is in arbitrary order.  It does not include the special
4684
entries '.' and '..' even if they are present in the directory.
4685
4686
4687
[clinic start generated code]*/
4688
4689
static PyObject *
4690
os_listdir_impl(PyObject *module, path_t *path)
4691
/*[clinic end generated code: output=293045673fcd1a75 input=0bd1728387391b9a]*/
4692
120
{
4693
120
    if (PySys_Audit("os.listdir", "O",
4694
120
                    path->object ? path->object : Py_None) < 0) {
4695
0
        return NULL;
4696
0
    }
4697
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
4698
    return _listdir_windows_no_opendir(path, NULL);
4699
#else
4700
120
    return _posix_listdir(path, NULL);
4701
120
#endif
4702
120
}
4703
4704
4705
#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
4706
4707
/*[clinic input]
4708
os.listdrives
4709
4710
Return a list containing the names of drives in the system.
4711
4712
A drive name typically looks like 'C:\\'.
4713
4714
[clinic start generated code]*/
4715
4716
static PyObject *
4717
os_listdrives_impl(PyObject *module)
4718
/*[clinic end generated code: output=aaece9dacdf682b5 input=1af9ccc9e583798e]*/
4719
{
4720
    /* Number of possible drives is limited, so 256 should always be enough.
4721
       On the day when it is not, listmounts() will have to be used. */
4722
    wchar_t buffer[256];
4723
    DWORD buflen = Py_ARRAY_LENGTH(buffer);
4724
    PyObject *result = NULL;
4725
    if (PySys_Audit("os.listdrives", NULL) < 0) {
4726
        return NULL;
4727
    }
4728
4729
    Py_BEGIN_ALLOW_THREADS;
4730
    buflen = GetLogicalDriveStringsW(buflen, buffer);
4731
    Py_END_ALLOW_THREADS;
4732
4733
    if (!buflen) {
4734
        PyErr_SetFromWindowsErr(0);
4735
        return NULL;
4736
    } else if (buflen >= Py_ARRAY_LENGTH(buffer)) {
4737
        PyErr_SetFromWindowsErr(ERROR_MORE_DATA);
4738
        return NULL;
4739
    }
4740
4741
    /* buflen includes a null terminator, so remove it */
4742
    PyObject *str = PyUnicode_FromWideChar(buffer, buflen - 1);
4743
    if (str) {
4744
        PyObject *nullchar = PyUnicode_FromStringAndSize("\0", 1);
4745
        if (nullchar) {
4746
            result = PyUnicode_Split(str, nullchar, -1);
4747
            Py_DECREF(nullchar);
4748
        }
4749
        Py_DECREF(str);
4750
    }
4751
    return result;
4752
}
4753
4754
#endif /* MS_WINDOWS_DESKTOP || MS_WINDOWS_SYSTEM */
4755
4756
#if defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
4757
4758
/*[clinic input]
4759
os.listvolumes
4760
4761
Return a list containing the volumes in the system.
4762
4763
Volumes are typically represented as a GUID path.
4764
4765
[clinic start generated code]*/
4766
4767
static PyObject *
4768
os_listvolumes_impl(PyObject *module)
4769
/*[clinic end generated code: output=534e10ea2bf9d386 input=f6e4e70371f11e99]*/
4770
{
4771
    PyObject *result = PyList_New(0);
4772
    HANDLE find = INVALID_HANDLE_VALUE;
4773
    wchar_t buffer[MAX_PATH + 1];
4774
    if (!result) {
4775
        return NULL;
4776
    }
4777
    if (PySys_Audit("os.listvolumes", NULL) < 0) {
4778
        Py_DECREF(result);
4779
        return NULL;
4780
    }
4781
4782
    int err = 0;
4783
    Py_BEGIN_ALLOW_THREADS;
4784
    find = FindFirstVolumeW(buffer, Py_ARRAY_LENGTH(buffer));
4785
    if (find == INVALID_HANDLE_VALUE) {
4786
        err = GetLastError();
4787
    }
4788
    Py_END_ALLOW_THREADS;
4789
4790
    while (!err) {
4791
        PyObject *s = PyUnicode_FromWideChar(buffer, -1);
4792
        if (!s || PyList_Append(result, s) < 0) {
4793
            Py_XDECREF(s);
4794
            Py_CLEAR(result);
4795
            break;
4796
        }
4797
        Py_DECREF(s);
4798
4799
        Py_BEGIN_ALLOW_THREADS;
4800
        if (!FindNextVolumeW(find, buffer, Py_ARRAY_LENGTH(buffer))) {
4801
            err = GetLastError();
4802
        }
4803
        Py_END_ALLOW_THREADS;
4804
    }
4805
4806
    if (find != INVALID_HANDLE_VALUE) {
4807
        Py_BEGIN_ALLOW_THREADS;
4808
        FindVolumeClose(find);
4809
        Py_END_ALLOW_THREADS;
4810
    }
4811
    if (err && err != ERROR_NO_MORE_FILES) {
4812
        PyErr_SetFromWindowsErr(err);
4813
        Py_XDECREF(result);
4814
        result = NULL;
4815
    }
4816
    return result;
4817
}
4818
4819
#endif /* MS_WINDOWS_APP || MS_WINDOWS_SYSTEM */
4820
4821
#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
4822
4823
/*[clinic input]
4824
os.listmounts
4825
4826
    volume: path_t
4827
4828
Return a list containing mount points for a particular volume.
4829
4830
'volume' should be a GUID path as returned from os.listvolumes.
4831
4832
[clinic start generated code]*/
4833
4834
static PyObject *
4835
os_listmounts_impl(PyObject *module, path_t *volume)
4836
/*[clinic end generated code: output=06da49679de4512e input=a8a27178e3f67845]*/
4837
{
4838
    wchar_t default_buffer[MAX_PATH + 1];
4839
    DWORD buflen = Py_ARRAY_LENGTH(default_buffer);
4840
    LPWSTR buffer = default_buffer;
4841
    DWORD attributes;
4842
    PyObject *str = NULL;
4843
    PyObject *nullchar = NULL;
4844
    PyObject *result = NULL;
4845
4846
    /* Ensure we have a valid volume path before continuing */
4847
    Py_BEGIN_ALLOW_THREADS
4848
    attributes = GetFileAttributesW(volume->wide);
4849
    Py_END_ALLOW_THREADS
4850
    if (attributes == INVALID_FILE_ATTRIBUTES &&
4851
        GetLastError() == ERROR_UNRECOGNIZED_VOLUME)
4852
    {
4853
        return PyErr_SetFromWindowsErr(ERROR_UNRECOGNIZED_VOLUME);
4854
    }
4855
4856
    if (PySys_Audit("os.listmounts", "O", volume->object) < 0) {
4857
        return NULL;
4858
    }
4859
4860
    while (1) {
4861
        BOOL success;
4862
        Py_BEGIN_ALLOW_THREADS
4863
        success = GetVolumePathNamesForVolumeNameW(volume->wide, buffer,
4864
                                                   buflen, &buflen);
4865
        Py_END_ALLOW_THREADS
4866
        if (success) {
4867
            break;
4868
        }
4869
        if (GetLastError() != ERROR_MORE_DATA) {
4870
            PyErr_SetFromWindowsErr(0);
4871
            goto exit;
4872
        }
4873
        if (buffer != default_buffer) {
4874
            PyMem_Free((void *)buffer);
4875
        }
4876
        buffer = (wchar_t*)PyMem_Malloc(sizeof(wchar_t) * buflen);
4877
        if (!buffer) {
4878
            PyErr_NoMemory();
4879
            goto exit;
4880
        }
4881
    }
4882
    if (buflen < 2) {
4883
        result = PyList_New(0);
4884
        goto exit;
4885
    }
4886
    // buflen includes two null terminators, one for the last string
4887
    // and one for the array of strings.
4888
    str = PyUnicode_FromWideChar(buffer, buflen - 2);
4889
    nullchar = PyUnicode_FromStringAndSize("\0", 1);
4890
    if (str && nullchar) {
4891
        result = PyUnicode_Split(str, nullchar, -1);
4892
    }
4893
exit:
4894
    if (buffer != default_buffer) {
4895
        PyMem_Free(buffer);
4896
    }
4897
    Py_XDECREF(nullchar);
4898
    Py_XDECREF(str);
4899
    return result;
4900
}
4901
4902
#endif /* MS_WINDOWS_DESKTOP || MS_WINDOWS_SYSTEM */
4903
4904
#ifdef MS_WINDOWS
4905
4906
/*[clinic input]
4907
os._path_isdevdrive
4908
4909
    path: path_t
4910
4911
Determines whether the specified path is on a Windows Dev Drive.
4912
4913
[clinic start generated code]*/
4914
4915
static PyObject *
4916
os__path_isdevdrive_impl(PyObject *module, path_t *path)
4917
/*[clinic end generated code: output=1f437ea6677433a2 input=ee83e4996a48e23d]*/
4918
{
4919
#ifndef PERSISTENT_VOLUME_STATE_DEV_VOLUME
4920
    /* This flag will be documented at
4921
       https://learn.microsoft.com/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_fs_persistent_volume_information
4922
       after release, and will be available in the latest WinSDK.
4923
       We include the flag to avoid a specific version dependency
4924
       on the latest WinSDK. */
4925
    const int PERSISTENT_VOLUME_STATE_DEV_VOLUME = 0x00002000;
4926
#endif
4927
    int err = 0;
4928
    PyObject *r = NULL;
4929
    wchar_t volume[MAX_PATH];
4930
4931
    Py_BEGIN_ALLOW_THREADS
4932
    if (!GetVolumePathNameW(path->wide, volume, MAX_PATH)) {
4933
        /* invalid path of some kind */
4934
        /* Note that this also includes the case where a volume is mounted
4935
           in a path longer than 260 characters. This is likely to be rare
4936
           and problematic for other reasons, so a (soft) failure in this
4937
           check seems okay. */
4938
        err = GetLastError();
4939
    } else if (GetDriveTypeW(volume) != DRIVE_FIXED) {
4940
        /* only care about local dev drives */
4941
        r = Py_False;
4942
    } else {
4943
        HANDLE hVolume = CreateFileW(
4944
            volume,
4945
            FILE_READ_ATTRIBUTES,
4946
            FILE_SHARE_READ | FILE_SHARE_WRITE,
4947
            NULL,
4948
            OPEN_EXISTING,
4949
            FILE_FLAG_BACKUP_SEMANTICS,
4950
            NULL
4951
        );
4952
        if (hVolume == INVALID_HANDLE_VALUE) {
4953
            err = GetLastError();
4954
        } else {
4955
            FILE_FS_PERSISTENT_VOLUME_INFORMATION volumeState = {0};
4956
            volumeState.Version = 1;
4957
            volumeState.FlagMask = PERSISTENT_VOLUME_STATE_DEV_VOLUME;
4958
            if (!DeviceIoControl(
4959
                hVolume,
4960
                FSCTL_QUERY_PERSISTENT_VOLUME_STATE,
4961
                &volumeState,
4962
                sizeof(volumeState),
4963
                &volumeState,
4964
                sizeof(volumeState),
4965
                NULL,
4966
                NULL
4967
            )) {
4968
                err = GetLastError();
4969
            }
4970
            CloseHandle(hVolume);
4971
            if (err == ERROR_INVALID_PARAMETER) {
4972
                /* not supported on this platform */
4973
                r = Py_False;
4974
            } else if (!err) {
4975
                r = (volumeState.VolumeFlags & PERSISTENT_VOLUME_STATE_DEV_VOLUME)
4976
                    ? Py_True : Py_False;
4977
            }
4978
        }
4979
    }
4980
    Py_END_ALLOW_THREADS
4981
4982
    if (err) {
4983
        PyErr_SetFromWindowsErr(err);
4984
        return NULL;
4985
    }
4986
4987
    if (r) {
4988
        return Py_NewRef(r);
4989
    }
4990
4991
    return NULL;
4992
}
4993
4994
4995
int
4996
_PyOS_getfullpathname(const wchar_t *path, wchar_t **abspath_p)
4997
{
4998
    wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
4999
    DWORD result;
5000
5001
    result = GetFullPathNameW(path,
5002
                              Py_ARRAY_LENGTH(woutbuf), woutbuf,
5003
                              NULL);
5004
    if (!result) {
5005
        return -1;
5006
    }
5007
5008
    if (result >= Py_ARRAY_LENGTH(woutbuf)) {
5009
        if ((size_t)result <= (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t)) {
5010
            woutbufp = PyMem_RawMalloc((size_t)result * sizeof(wchar_t));
5011
        }
5012
        else {
5013
            woutbufp = NULL;
5014
        }
5015
        if (!woutbufp) {
5016
            *abspath_p = NULL;
5017
            return 0;
5018
        }
5019
5020
        result = GetFullPathNameW(path, result, woutbufp, NULL);
5021
        if (!result) {
5022
            PyMem_RawFree(woutbufp);
5023
            return -1;
5024
        }
5025
    }
5026
5027
    if (woutbufp != woutbuf) {
5028
        *abspath_p = woutbufp;
5029
        return 0;
5030
    }
5031
5032
    *abspath_p = _PyMem_RawWcsdup(woutbufp);
5033
    return 0;
5034
}
5035
5036
5037
/* A helper function for abspath on win32 */
5038
/*[clinic input]
5039
os._getfullpathname
5040
5041
    path: path_t
5042
    /
5043
5044
[clinic start generated code]*/
5045
5046
static PyObject *
5047
os__getfullpathname_impl(PyObject *module, path_t *path)
5048
/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
5049
{
5050
    wchar_t *abspath;
5051
5052
    if (_PyOS_getfullpathname(path->wide, &abspath) < 0) {
5053
        return win32_error_object("GetFullPathNameW", path->object);
5054
    }
5055
    if (abspath == NULL) {
5056
        return PyErr_NoMemory();
5057
    }
5058
5059
    PyObject *str = PyUnicode_FromWideChar(abspath, -1);
5060
    PyMem_RawFree(abspath);
5061
    if (str == NULL) {
5062
        return NULL;
5063
    }
5064
    if (PyBytes_Check(path->object)) {
5065
        Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
5066
    }
5067
    return str;
5068
}
5069
5070
5071
/*[clinic input]
5072
os._getfinalpathname
5073
5074
    path: path_t
5075
    /
5076
5077
A helper function for samepath on windows.
5078
[clinic start generated code]*/
5079
5080
static PyObject *
5081
os__getfinalpathname_impl(PyObject *module, path_t *path)
5082
/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
5083
{
5084
    HANDLE hFile;
5085
    wchar_t buf[MAXPATHLEN], *target_path = buf;
5086
    int buf_size = Py_ARRAY_LENGTH(buf);
5087
    int result_length;
5088
    PyObject *result;
5089
5090
    Py_BEGIN_ALLOW_THREADS
5091
    hFile = CreateFileW(
5092
        path->wide,
5093
        0, /* desired access */
5094
        0, /* share mode */
5095
        NULL, /* security attributes */
5096
        OPEN_EXISTING,
5097
        /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
5098
        FILE_FLAG_BACKUP_SEMANTICS,
5099
        NULL);
5100
    Py_END_ALLOW_THREADS
5101
5102
    if (hFile == INVALID_HANDLE_VALUE) {
5103
        return win32_error_object("CreateFileW", path->object);
5104
    }
5105
5106
    /* We have a good handle to the target, use it to determine the
5107
       target path name. */
5108
    while (1) {
5109
        Py_BEGIN_ALLOW_THREADS
5110
        result_length = GetFinalPathNameByHandleW(hFile, target_path,
5111
                                                  buf_size, VOLUME_NAME_DOS);
5112
        Py_END_ALLOW_THREADS
5113
5114
        if (!result_length) {
5115
            result = win32_error_object("GetFinalPathNameByHandleW",
5116
                                         path->object);
5117
            goto cleanup;
5118
        }
5119
5120
        if (result_length < buf_size) {
5121
            break;
5122
        }
5123
5124
        wchar_t *tmp;
5125
        tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
5126
                            result_length * sizeof(*tmp));
5127
        if (!tmp) {
5128
            result = PyErr_NoMemory();
5129
            goto cleanup;
5130
        }
5131
5132
        buf_size = result_length;
5133
        target_path = tmp;
5134
    }
5135
5136
    result = PyUnicode_FromWideChar(target_path, result_length);
5137
    if (result && PyBytes_Check(path->object)) {
5138
        Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
5139
    }
5140
5141
cleanup:
5142
    if (target_path != buf) {
5143
        PyMem_Free(target_path);
5144
    }
5145
    CloseHandle(hFile);
5146
    return result;
5147
}
5148
5149
/*[clinic input]
5150
os._findfirstfile
5151
    path: path_t
5152
    /
5153
A function to get the real file name without accessing the file in Windows.
5154
[clinic start generated code]*/
5155
5156
static PyObject *
5157
os__findfirstfile_impl(PyObject *module, path_t *path)
5158
/*[clinic end generated code: output=106dd3f0779c83dd input=0734dff70f60e1a8]*/
5159
{
5160
    PyObject *result;
5161
    HANDLE hFindFile;
5162
    WIN32_FIND_DATAW wFileData;
5163
    WCHAR *wRealFileName;
5164
5165
    Py_BEGIN_ALLOW_THREADS
5166
    hFindFile = FindFirstFileW(path->wide, &wFileData);
5167
    Py_END_ALLOW_THREADS
5168
5169
    if (hFindFile == INVALID_HANDLE_VALUE) {
5170
        path_error(path);
5171
        return NULL;
5172
    }
5173
5174
    wRealFileName = wFileData.cFileName;
5175
    result = PyUnicode_FromWideChar(wRealFileName, -1);
5176
    FindClose(hFindFile);
5177
    return result;
5178
}
5179
5180
5181
/*[clinic input]
5182
os._getvolumepathname
5183
5184
    path: path_t
5185
5186
A helper function for ismount on Win32.
5187
[clinic start generated code]*/
5188
5189
static PyObject *
5190
os__getvolumepathname_impl(PyObject *module, path_t *path)
5191
/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
5192
{
5193
    PyObject *result;
5194
    wchar_t *mountpath=NULL;
5195
    size_t buflen;
5196
    BOOL ret;
5197
5198
    /* Volume path should be shorter than entire path */
5199
    buflen = Py_MAX(path->length, MAX_PATH);
5200
5201
    if (buflen > PY_DWORD_MAX) {
5202
        PyErr_SetString(PyExc_OverflowError, "path too long");
5203
        return NULL;
5204
    }
5205
5206
    mountpath = PyMem_New(wchar_t, buflen);
5207
    if (mountpath == NULL)
5208
        return PyErr_NoMemory();
5209
5210
    Py_BEGIN_ALLOW_THREADS
5211
    ret = GetVolumePathNameW(path->wide, mountpath,
5212
                             Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
5213
    Py_END_ALLOW_THREADS
5214
5215
    if (!ret) {
5216
        result = win32_error_object("_getvolumepathname", path->object);
5217
        goto exit;
5218
    }
5219
    result = PyUnicode_FromWideChar(mountpath, -1);
5220
    if (PyBytes_Check(path->object))
5221
        Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
5222
5223
exit:
5224
    PyMem_Free(mountpath);
5225
    return result;
5226
}
5227
5228
5229
/*[clinic input]
5230
os._path_splitroot
5231
5232
    path: path_t,
5233
    /
5234
5235
Removes everything after the root on Win32.
5236
[clinic start generated code]*/
5237
5238
static PyObject *
5239
os__path_splitroot_impl(PyObject *module, path_t *path)
5240
/*[clinic end generated code: output=ab7f1a88b654581c input=42831e41f8458f6d]*/
5241
{
5242
    wchar_t *buffer;
5243
    wchar_t *end;
5244
    PyObject *result = NULL;
5245
    HRESULT ret;
5246
5247
    buffer = (wchar_t*)PyMem_Malloc(sizeof(wchar_t) * (wcslen(path->wide) + 1));
5248
    if (!buffer) {
5249
        return NULL;
5250
    }
5251
    wcscpy(buffer, path->wide);
5252
    for (wchar_t *p = wcschr(buffer, L'/'); p; p = wcschr(p, L'/')) {
5253
        *p = L'\\';
5254
    }
5255
5256
    Py_BEGIN_ALLOW_THREADS
5257
    ret = PathCchSkipRoot(buffer, &end);
5258
    Py_END_ALLOW_THREADS
5259
    if (FAILED(ret)) {
5260
        result = Py_BuildValue("sO", "", path->object);
5261
    } else if (end != buffer) {
5262
        size_t rootLen = (size_t)(end - buffer);
5263
        result = Py_BuildValue("NN",
5264
            PyUnicode_FromWideChar(path->wide, rootLen),
5265
            PyUnicode_FromWideChar(path->wide + rootLen, -1)
5266
        );
5267
    } else {
5268
        result = Py_BuildValue("Os", path->object, "");
5269
    }
5270
    PyMem_Free(buffer);
5271
5272
    return result;
5273
}
5274
5275
5276
#define PY_IFREG  1 // Regular file
5277
#define PY_IFDIR  2 // Directory
5278
#define PY_IFLNK  4 // Symlink
5279
#define PY_IFMNT  8 // Mount Point (junction)
5280
#define PY_IFLRP 16 // Link Reparse Point (name-surrogate, symlink, junction)
5281
#define PY_IFRRP 32 // Regular Reparse Point
5282
5283
static inline BOOL
5284
_testInfo(DWORD attributes, DWORD reparseTag, BOOL diskDevice, int testedType)
5285
{
5286
    switch (testedType) {
5287
    case PY_IFREG:
5288
        return diskDevice && attributes &&
5289
               !(attributes & FILE_ATTRIBUTE_DIRECTORY);
5290
    case PY_IFDIR:
5291
        return attributes & FILE_ATTRIBUTE_DIRECTORY;
5292
    case PY_IFLNK:
5293
        return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
5294
               reparseTag == IO_REPARSE_TAG_SYMLINK;
5295
    case PY_IFMNT:
5296
        return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
5297
               reparseTag == IO_REPARSE_TAG_MOUNT_POINT;
5298
    case PY_IFLRP:
5299
        return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
5300
               IsReparseTagNameSurrogate(reparseTag);
5301
    case PY_IFRRP:
5302
        return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
5303
               reparseTag && !IsReparseTagNameSurrogate(reparseTag);
5304
    }
5305
5306
    return FALSE;
5307
}
5308
5309
static BOOL
5310
_testFileTypeByHandle(HANDLE hfile, int testedType, BOOL diskOnly)
5311
{
5312
    assert(testedType == PY_IFREG || testedType == PY_IFDIR ||
5313
           testedType == PY_IFLNK || testedType == PY_IFMNT ||
5314
           testedType == PY_IFLRP || testedType == PY_IFRRP);
5315
5316
    BOOL diskDevice = GetFileType(hfile) == FILE_TYPE_DISK;
5317
    if (diskOnly && !diskDevice) {
5318
        return FALSE;
5319
    }
5320
    if (testedType != PY_IFREG && testedType != PY_IFDIR) {
5321
        FILE_ATTRIBUTE_TAG_INFO info;
5322
        return GetFileInformationByHandleEx(hfile, FileAttributeTagInfo, &info,
5323
                                            sizeof(info)) &&
5324
               _testInfo(info.FileAttributes, info.ReparseTag, diskDevice,
5325
                         testedType);
5326
    }
5327
    FILE_BASIC_INFO info;
5328
    return GetFileInformationByHandleEx(hfile, FileBasicInfo, &info,
5329
                                        sizeof(info)) &&
5330
           _testInfo(info.FileAttributes, 0, diskDevice, testedType);
5331
}
5332
5333
static BOOL
5334
_testFileTypeByName(LPCWSTR path, int testedType)
5335
{
5336
    assert(testedType == PY_IFREG || testedType == PY_IFDIR ||
5337
           testedType == PY_IFLNK || testedType == PY_IFMNT ||
5338
           testedType == PY_IFLRP || testedType == PY_IFRRP);
5339
5340
    FILE_STAT_BASIC_INFORMATION info;
5341
    if (_Py_GetFileInformationByName(path, FileStatBasicByNameInfo, &info,
5342
                                     sizeof(info)))
5343
    {
5344
        BOOL diskDevice = info.DeviceType == FILE_DEVICE_DISK ||
5345
                          info.DeviceType == FILE_DEVICE_VIRTUAL_DISK ||
5346
                          info.DeviceType == FILE_DEVICE_CD_ROM;
5347
        BOOL result = _testInfo(info.FileAttributes, info.ReparseTag,
5348
                                diskDevice, testedType);
5349
        if (!result || (testedType != PY_IFREG && testedType != PY_IFDIR) ||
5350
            !(info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
5351
        {
5352
            return result;
5353
        }
5354
    }
5355
    else if (_Py_GetFileInformationByName_ErrorIsTrustworthy(
5356
                GetLastError()))
5357
    {
5358
        return FALSE;
5359
    }
5360
5361
    DWORD flags = FILE_FLAG_BACKUP_SEMANTICS;
5362
    if (testedType != PY_IFREG && testedType != PY_IFDIR) {
5363
        flags |= FILE_FLAG_OPEN_REPARSE_POINT;
5364
    }
5365
    HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
5366
                               OPEN_EXISTING, flags, NULL);
5367
    if (hfile != INVALID_HANDLE_VALUE) {
5368
        BOOL result = _testFileTypeByHandle(hfile, testedType, FALSE);
5369
        CloseHandle(hfile);
5370
        return result;
5371
    }
5372
5373
    switch (GetLastError()) {
5374
    case ERROR_ACCESS_DENIED:
5375
    case ERROR_SHARING_VIOLATION:
5376
    case ERROR_CANT_ACCESS_FILE:
5377
    case ERROR_INVALID_PARAMETER:
5378
        int rc;
5379
        STRUCT_STAT st;
5380
        if (testedType == PY_IFREG || testedType == PY_IFDIR) {
5381
            rc = STAT(path, &st);
5382
        }
5383
        else {
5384
            // PY_IFRRP is not generally supported in this case, except for
5385
            // unhandled reparse points such as IO_REPARSE_TAG_APPEXECLINK.
5386
            rc = LSTAT(path, &st);
5387
        }
5388
        if (!rc) {
5389
            return _testInfo(st.st_file_attributes, st.st_reparse_tag,
5390
                             st.st_mode & S_IFREG, testedType);
5391
        }
5392
    }
5393
5394
    return FALSE;
5395
}
5396
5397
5398
static BOOL
5399
_testFileExistsByName(LPCWSTR path, BOOL followLinks)
5400
{
5401
    FILE_STAT_BASIC_INFORMATION info;
5402
    if (_Py_GetFileInformationByName(path, FileStatBasicByNameInfo, &info,
5403
                                     sizeof(info)))
5404
    {
5405
        if (!(info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ||
5406
            (!followLinks && IsReparseTagNameSurrogate(info.ReparseTag)))
5407
        {
5408
            return TRUE;
5409
        }
5410
    }
5411
    else if (_Py_GetFileInformationByName_ErrorIsTrustworthy(
5412
                    GetLastError()))
5413
    {
5414
        return FALSE;
5415
    }
5416
5417
    DWORD flags = FILE_FLAG_BACKUP_SEMANTICS;
5418
    if (!followLinks) {
5419
        flags |= FILE_FLAG_OPEN_REPARSE_POINT;
5420
    }
5421
    HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
5422
                               OPEN_EXISTING, flags, NULL);
5423
    if (hfile != INVALID_HANDLE_VALUE) {
5424
        if (followLinks) {
5425
            CloseHandle(hfile);
5426
            return TRUE;
5427
        }
5428
        // Regular Reparse Points (PY_IFRRP) have to be traversed.
5429
        BOOL result = _testFileTypeByHandle(hfile, PY_IFRRP, FALSE);
5430
        CloseHandle(hfile);
5431
        if (!result) {
5432
            return TRUE;
5433
        }
5434
        hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
5435
                            FILE_FLAG_BACKUP_SEMANTICS, NULL);
5436
        if (hfile != INVALID_HANDLE_VALUE) {
5437
            CloseHandle(hfile);
5438
            return TRUE;
5439
        }
5440
    }
5441
5442
    switch (GetLastError()) {
5443
    case ERROR_ACCESS_DENIED:
5444
    case ERROR_SHARING_VIOLATION:
5445
    case ERROR_CANT_ACCESS_FILE:
5446
    case ERROR_INVALID_PARAMETER:
5447
        STRUCT_STAT _st;
5448
        return followLinks ? !STAT(path, &_st): !LSTAT(path, &_st);
5449
    }
5450
5451
    return FALSE;
5452
}
5453
5454
5455
static BOOL
5456
_testFileExists(path_t *path, BOOL followLinks)
5457
{
5458
    BOOL result = FALSE;
5459
    if (path->value_error) {
5460
        return FALSE;
5461
    }
5462
5463
    Py_BEGIN_ALLOW_THREADS
5464
    if (path->fd != -1) {
5465
        HANDLE hfile = _Py_get_osfhandle_noraise(path->fd);
5466
        if (hfile != INVALID_HANDLE_VALUE) {
5467
            if (GetFileType(hfile) != FILE_TYPE_UNKNOWN || !GetLastError()) {
5468
                result = TRUE;
5469
            }
5470
        }
5471
    }
5472
    else if (path->wide) {
5473
        result = _testFileExistsByName(path->wide, followLinks);
5474
    }
5475
    Py_END_ALLOW_THREADS
5476
5477
    return result;
5478
}
5479
5480
5481
static BOOL
5482
_testFileType(path_t *path, int testedType)
5483
{
5484
    BOOL result = FALSE;
5485
    if (path->value_error) {
5486
        return FALSE;
5487
    }
5488
5489
    Py_BEGIN_ALLOW_THREADS
5490
    if (path->fd != -1) {
5491
        HANDLE hfile = _Py_get_osfhandle_noraise(path->fd);
5492
        if (hfile != INVALID_HANDLE_VALUE) {
5493
            result = _testFileTypeByHandle(hfile, testedType, TRUE);
5494
        }
5495
    }
5496
    else if (path->wide) {
5497
        result = _testFileTypeByName(path->wide, testedType);
5498
    }
5499
    Py_END_ALLOW_THREADS
5500
5501
    return result;
5502
}
5503
5504
5505
/*[clinic input]
5506
os._path_exists -> bool
5507
5508
    path: path_t(allow_fd=True, suppress_value_error=True)
5509
5510
Test whether a path exists.  Returns False for broken symbolic links.
5511
5512
[clinic start generated code]*/
5513
5514
static int
5515
os__path_exists_impl(PyObject *module, path_t *path)
5516
/*[clinic end generated code: output=8da13acf666e16ba input=142beabfc66783eb]*/
5517
{
5518
    return _testFileExists(path, TRUE);
5519
}
5520
5521
5522
/*[clinic input]
5523
os._path_lexists -> bool
5524
5525
    path: path_t(allow_fd=True, suppress_value_error=True)
5526
5527
Test whether a path exists.  Returns True for broken symbolic links.
5528
5529
[clinic start generated code]*/
5530
5531
static int
5532
os__path_lexists_impl(PyObject *module, path_t *path)
5533
/*[clinic end generated code: output=e7240ed5fc45bff3 input=208205112a3cc1ed]*/
5534
{
5535
    return _testFileExists(path, FALSE);
5536
}
5537
5538
5539
/*[clinic input]
5540
os._path_isdir -> bool
5541
5542
    path: path_t(allow_fd=True, suppress_value_error=True),
5543
    /
5544
5545
Return true if the pathname refers to an existing directory.
5546
5547
[clinic start generated code]*/
5548
5549
static int
5550
os__path_isdir_impl(PyObject *module, path_t *path)
5551
/*[clinic end generated code: output=d5786196f9e2fa7a input=0d3fd790564d244b]*/
5552
{
5553
    return _testFileType(path, PY_IFDIR);
5554
}
5555
5556
5557
/*[clinic input]
5558
os._path_isfile -> bool
5559
5560
    path: path_t(allow_fd=True, suppress_value_error=True)
5561
5562
Test whether a path is a regular file
5563
5564
[clinic start generated code]*/
5565
5566
static int
5567
os__path_isfile_impl(PyObject *module, path_t *path)
5568
/*[clinic end generated code: output=5c3073bc212b9863 input=4ac1fd350b30a39e]*/
5569
{
5570
    return _testFileType(path, PY_IFREG);
5571
}
5572
5573
5574
/*[clinic input]
5575
os._path_islink -> bool
5576
5577
    path: path_t(allow_fd=True, suppress_value_error=True)
5578
5579
Test whether a path is a symbolic link
5580
5581
[clinic start generated code]*/
5582
5583
static int
5584
os__path_islink_impl(PyObject *module, path_t *path)
5585
/*[clinic end generated code: output=30da7bda8296adcc input=7510ce05b547debb]*/
5586
{
5587
    return _testFileType(path, PY_IFLNK);
5588
}
5589
5590
5591
/*[clinic input]
5592
os._path_isjunction -> bool
5593
5594
    path: path_t(allow_fd=True, suppress_value_error=True)
5595
5596
Test whether a path is a junction
5597
5598
[clinic start generated code]*/
5599
5600
static int
5601
os__path_isjunction_impl(PyObject *module, path_t *path)
5602
/*[clinic end generated code: output=e1d17a9dd18a9945 input=7dcb8bc4e972fcaf]*/
5603
{
5604
    return _testFileType(path, PY_IFMNT);
5605
}
5606
5607
#undef PY_IFREG
5608
#undef PY_IFDIR
5609
#undef PY_IFLNK
5610
#undef PY_IFMNT
5611
#undef PY_IFLRP
5612
#undef PY_IFRRP
5613
5614
#endif /* MS_WINDOWS */
5615
5616
5617
/*[clinic input]
5618
os._path_splitroot_ex
5619
5620
    path: path_t(make_wide=True, nonstrict=True),
5621
    /
5622
5623
Split a pathname into drive, root and tail.
5624
5625
The tail contains anything after the root.
5626
[clinic start generated code]*/
5627
5628
static PyObject *
5629
os__path_splitroot_ex_impl(PyObject *module, path_t *path)
5630
/*[clinic end generated code: output=4b0072b6cdf4b611 input=4ac47b394d68bd21]*/
5631
0
{
5632
0
    Py_ssize_t drvsize, rootsize;
5633
0
    PyObject *drv = NULL, *root = NULL, *tail = NULL, *result = NULL;
5634
5635
0
    const wchar_t *buffer = path->wide;
5636
0
    _Py_skiproot(buffer, path->length, &drvsize, &rootsize);
5637
0
    drv = PyUnicode_FromWideChar(buffer, drvsize);
5638
0
    if (drv == NULL) {
5639
0
        goto exit;
5640
0
    }
5641
0
    root = PyUnicode_FromWideChar(&buffer[drvsize], rootsize);
5642
0
    if (root == NULL) {
5643
0
        goto exit;
5644
0
    }
5645
0
    tail = PyUnicode_FromWideChar(&buffer[drvsize + rootsize],
5646
0
                                  path->length - drvsize - rootsize);
5647
0
    if (tail == NULL) {
5648
0
        goto exit;
5649
0
    }
5650
0
    if (PyBytes_Check(path->object)) {
5651
0
        Py_SETREF(drv, PyUnicode_EncodeFSDefault(drv));
5652
0
        if (drv == NULL) {
5653
0
            goto exit;
5654
0
        }
5655
0
        Py_SETREF(root, PyUnicode_EncodeFSDefault(root));
5656
0
        if (root == NULL) {
5657
0
            goto exit;
5658
0
        }
5659
0
        Py_SETREF(tail, PyUnicode_EncodeFSDefault(tail));
5660
0
        if (tail == NULL) {
5661
0
            goto exit;
5662
0
        }
5663
0
    }
5664
0
    result = PyTuple_Pack(3, drv, root, tail);
5665
0
exit:
5666
0
    Py_XDECREF(drv);
5667
0
    Py_XDECREF(root);
5668
0
    Py_XDECREF(tail);
5669
0
    return result;
5670
0
}
5671
5672
5673
/*[clinic input]
5674
os._path_normpath
5675
5676
    path: path_t(make_wide=True, nonstrict=True)
5677
5678
Normalize path, eliminating double slashes, etc.
5679
[clinic start generated code]*/
5680
5681
static PyObject *
5682
os__path_normpath_impl(PyObject *module, path_t *path)
5683
/*[clinic end generated code: output=d353e7ed9410c044 input=3d4ac23b06332dcb]*/
5684
80
{
5685
80
    PyObject *result;
5686
80
    Py_ssize_t norm_len;
5687
80
    wchar_t *norm_path = _Py_normpath_and_size((wchar_t *)path->wide,
5688
80
                                               path->length, &norm_len);
5689
80
    if (!norm_len) {
5690
0
        result = PyUnicode_FromOrdinal('.');
5691
0
    }
5692
80
    else {
5693
80
        result = PyUnicode_FromWideChar(norm_path, norm_len);
5694
80
    }
5695
80
    if (PyBytes_Check(path->object)) {
5696
0
        Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
5697
0
    }
5698
80
    return result;
5699
80
}
5700
5701
/*[clinic input]
5702
os.mkdir
5703
5704
    path : path_t
5705
5706
    mode: int = 0o777
5707
5708
    *
5709
5710
    dir_fd : dir_fd(requires='mkdirat') = None
5711
5712
# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
5713
5714
Create a directory.
5715
5716
If dir_fd is not None, it should be a file descriptor open to a directory,
5717
  and path should be relative; path will then be relative to that directory.
5718
dir_fd may not be implemented on your platform.
5719
  If it is unavailable, using it will raise a NotImplementedError.
5720
5721
The mode argument is ignored on Windows. Where it is used, the current umask
5722
value is first masked out.
5723
[clinic start generated code]*/
5724
5725
static PyObject *
5726
os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
5727
/*[clinic end generated code: output=a70446903abe821f input=a61722e1576fab03]*/
5728
0
{
5729
0
    int result;
5730
#ifdef MS_WINDOWS
5731
    int error = 0;
5732
    SECURITY_ATTRIBUTES secAttr = { sizeof(secAttr) };
5733
    SECURITY_ATTRIBUTES *pSecAttr = NULL;
5734
#endif
5735
0
#ifdef HAVE_MKDIRAT
5736
0
    int mkdirat_unavailable = 0;
5737
0
#endif
5738
5739
0
    if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
5740
0
                    dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
5741
0
        return NULL;
5742
0
    }
5743
5744
#ifdef MS_WINDOWS
5745
    Py_BEGIN_ALLOW_THREADS
5746
    // For API sets that don't support these APIs, we have no choice
5747
    // but to silently create a directory with default ACL.
5748
#if defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
5749
    if (mode == 0700 /* 0o700 */) {
5750
        ULONG sdSize;
5751
        pSecAttr = &secAttr;
5752
        // Set a discretionary ACL (D) that is protected (P) and includes
5753
        // inheritable (OICI) entries that allow (A) full control (FA) to
5754
        // SYSTEM (SY), Administrators (BA), and the owner (OW).
5755
        if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(
5756
            L"D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)",
5757
            SDDL_REVISION_1,
5758
            &secAttr.lpSecurityDescriptor,
5759
            &sdSize
5760
        )) {
5761
            error = GetLastError();
5762
        }
5763
    }
5764
#endif
5765
    if (!error) {
5766
        result = CreateDirectoryW(path->wide, pSecAttr);
5767
        if (secAttr.lpSecurityDescriptor &&
5768
            // uncommonly, LocalFree returns non-zero on error, but still uses
5769
            // GetLastError() to see what the error code is
5770
            LocalFree(secAttr.lpSecurityDescriptor)) {
5771
            error = GetLastError();
5772
        }
5773
    }
5774
    Py_END_ALLOW_THREADS
5775
5776
    if (error) {
5777
        return PyErr_SetFromWindowsErr(error);
5778
    }
5779
    if (!result) {
5780
        return path_error(path);
5781
    }
5782
#else
5783
0
    Py_BEGIN_ALLOW_THREADS
5784
0
#if HAVE_MKDIRAT
5785
0
    if (dir_fd != DEFAULT_DIR_FD) {
5786
0
      if (HAVE_MKDIRAT_RUNTIME) {
5787
0
        result = mkdirat(dir_fd, path->narrow, mode);
5788
5789
0
      } else {
5790
0
        mkdirat_unavailable = 1;
5791
0
      }
5792
0
    } else
5793
0
#endif
5794
#if defined(__WATCOMC__) && !defined(__QNX__)
5795
        result = mkdir(path->narrow);
5796
#else
5797
0
        result = mkdir(path->narrow, mode);
5798
0
#endif
5799
0
    Py_END_ALLOW_THREADS
5800
5801
0
#if HAVE_MKDIRAT
5802
0
    if (mkdirat_unavailable) {
5803
0
        argument_unavailable_error(NULL, "dir_fd");
5804
0
        return NULL;
5805
0
    }
5806
0
#endif
5807
5808
0
    if (result < 0)
5809
0
        return path_error(path);
5810
0
#endif /* MS_WINDOWS */
5811
0
    Py_RETURN_NONE;
5812
0
}
5813
5814
5815
/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
5816
#if defined(HAVE_SYS_RESOURCE_H)
5817
#include <sys/resource.h>
5818
#endif
5819
5820
5821
#ifdef HAVE_NICE
5822
/*[clinic input]
5823
os.nice
5824
5825
    increment: int
5826
    /
5827
5828
Add increment to the priority of process and return the new priority.
5829
[clinic start generated code]*/
5830
5831
static PyObject *
5832
os_nice_impl(PyObject *module, int increment)
5833
/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
5834
0
{
5835
0
    int value;
5836
5837
    /* There are two flavours of 'nice': one that returns the new
5838
       priority (as required by almost all standards out there) and the
5839
       Linux/FreeBSD one, which returns '0' on success and advices
5840
       the use of getpriority() to get the new priority.
5841
5842
       If we are of the nice family that returns the new priority, we
5843
       need to clear errno before the call, and check if errno is filled
5844
       before calling posix_error() on a returnvalue of -1, because the
5845
       -1 may be the actual new priority! */
5846
5847
0
    errno = 0;
5848
0
    value = nice(increment);
5849
#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
5850
    if (value == 0)
5851
        value = getpriority(PRIO_PROCESS, 0);
5852
#endif
5853
0
    if (value == -1 && errno != 0)
5854
        /* either nice() or getpriority() returned an error */
5855
0
        return posix_error();
5856
0
    return PyLong_FromLong((long) value);
5857
0
}
5858
#endif /* HAVE_NICE */
5859
5860
5861
#ifdef HAVE_GETPRIORITY
5862
/*[clinic input]
5863
os.getpriority
5864
5865
    which: int
5866
    who: int
5867
5868
Return program scheduling priority.
5869
[clinic start generated code]*/
5870
5871
static PyObject *
5872
os_getpriority_impl(PyObject *module, int which, int who)
5873
/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
5874
0
{
5875
0
    int retval;
5876
5877
0
    errno = 0;
5878
0
    retval = getpriority(which, who);
5879
0
    if (errno != 0)
5880
0
        return posix_error();
5881
0
    return PyLong_FromLong((long)retval);
5882
0
}
5883
#endif /* HAVE_GETPRIORITY */
5884
5885
5886
#ifdef HAVE_SETPRIORITY
5887
/*[clinic input]
5888
os.setpriority
5889
5890
    which: int
5891
    who: int
5892
    priority: int
5893
5894
Set program scheduling priority.
5895
[clinic start generated code]*/
5896
5897
static PyObject *
5898
os_setpriority_impl(PyObject *module, int which, int who, int priority)
5899
/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
5900
0
{
5901
0
    int retval;
5902
5903
0
    retval = setpriority(which, who, priority);
5904
0
    if (retval == -1)
5905
0
        return posix_error();
5906
0
    Py_RETURN_NONE;
5907
0
}
5908
#endif /* HAVE_SETPRIORITY */
5909
5910
5911
static PyObject *
5912
internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace)
5913
236
{
5914
236
    const char *function_name = is_replace ? "replace" : "rename";
5915
236
    int dir_fd_specified;
5916
5917
236
#ifdef HAVE_RENAMEAT
5918
236
    int renameat_unavailable = 0;
5919
236
#endif
5920
5921
#ifdef MS_WINDOWS
5922
    BOOL result;
5923
    int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
5924
#else
5925
236
    int result;
5926
236
#endif
5927
5928
236
    dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
5929
236
                       (dst_dir_fd != DEFAULT_DIR_FD);
5930
#ifndef HAVE_RENAMEAT
5931
    if (dir_fd_specified) {
5932
        argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
5933
        return NULL;
5934
    }
5935
#endif
5936
5937
236
    if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
5938
236
                    src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
5939
236
                    dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
5940
0
        return NULL;
5941
0
    }
5942
5943
#ifdef MS_WINDOWS
5944
    Py_BEGIN_ALLOW_THREADS
5945
    result = MoveFileExW(src->wide, dst->wide, flags);
5946
    Py_END_ALLOW_THREADS
5947
5948
    if (!result)
5949
        return path_error2(src, dst);
5950
5951
#else
5952
236
    Py_BEGIN_ALLOW_THREADS
5953
236
#ifdef HAVE_RENAMEAT
5954
236
    if (dir_fd_specified) {
5955
0
        if (HAVE_RENAMEAT_RUNTIME) {
5956
0
            result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
5957
0
        } else {
5958
0
            renameat_unavailable = 1;
5959
0
        }
5960
0
    } else
5961
236
#endif
5962
236
    result = rename(src->narrow, dst->narrow);
5963
236
    Py_END_ALLOW_THREADS
5964
5965
5966
236
#ifdef HAVE_RENAMEAT
5967
236
    if (renameat_unavailable) {
5968
0
        argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
5969
0
        return NULL;
5970
0
    }
5971
236
#endif
5972
5973
236
    if (result)
5974
0
        return path_error2(src, dst);
5975
236
#endif
5976
236
    Py_RETURN_NONE;
5977
236
}
5978
5979
5980
/*[clinic input]
5981
@permit_long_docstring_body
5982
os.rename
5983
5984
    src : path_t
5985
    dst : path_t
5986
    *
5987
    src_dir_fd : dir_fd = None
5988
    dst_dir_fd : dir_fd = None
5989
5990
Rename a file or directory.
5991
5992
If either src_dir_fd or dst_dir_fd is not None, it should be a file
5993
  descriptor open to a directory, and the respective path string (src or dst)
5994
  should be relative; the path will then be relative to that directory.
5995
src_dir_fd and dst_dir_fd, may not be implemented on your platform.
5996
  If they are unavailable, using them will raise a NotImplementedError.
5997
[clinic start generated code]*/
5998
5999
static PyObject *
6000
os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
6001
               int dst_dir_fd)
6002
/*[clinic end generated code: output=59e803072cf41230 input=11aae8c091162766]*/
6003
0
{
6004
0
    return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
6005
0
}
6006
6007
6008
/*[clinic input]
6009
@permit_long_docstring_body
6010
os.replace = os.rename
6011
6012
Rename a file or directory, overwriting the destination.
6013
6014
If either src_dir_fd or dst_dir_fd is not None, it should be a file
6015
  descriptor open to a directory, and the respective path string (src or dst)
6016
  should be relative; the path will then be relative to that directory.
6017
src_dir_fd and dst_dir_fd, may not be implemented on your platform.
6018
  If they are unavailable, using them will raise a NotImplementedError.
6019
[clinic start generated code]*/
6020
6021
static PyObject *
6022
os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
6023
                int dst_dir_fd)
6024
/*[clinic end generated code: output=1968c02e7857422b input=78d6c8087e90994c]*/
6025
236
{
6026
236
    return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
6027
236
}
6028
6029
6030
/*[clinic input]
6031
os.rmdir
6032
6033
    path: path_t
6034
    *
6035
    dir_fd: dir_fd(requires='unlinkat') = None
6036
6037
Remove a directory.
6038
6039
If dir_fd is not None, it should be a file descriptor open to a directory,
6040
  and path should be relative; path will then be relative to that directory.
6041
dir_fd may not be implemented on your platform.
6042
  If it is unavailable, using it will raise a NotImplementedError.
6043
[clinic start generated code]*/
6044
6045
static PyObject *
6046
os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
6047
/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
6048
0
{
6049
0
    int result;
6050
0
#ifdef HAVE_UNLINKAT
6051
0
    int unlinkat_unavailable = 0;
6052
0
#endif
6053
6054
0
    if (PySys_Audit("os.rmdir", "Oi", path->object,
6055
0
                    dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
6056
0
        return NULL;
6057
0
    }
6058
6059
0
    Py_BEGIN_ALLOW_THREADS
6060
#ifdef MS_WINDOWS
6061
    /* Windows, success=1, UNIX, success=0 */
6062
    result = !RemoveDirectoryW(path->wide);
6063
#else
6064
0
#ifdef HAVE_UNLINKAT
6065
0
    if (dir_fd != DEFAULT_DIR_FD) {
6066
0
      if (HAVE_UNLINKAT_RUNTIME) {
6067
0
        result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
6068
0
      } else {
6069
0
        unlinkat_unavailable = 1;
6070
0
        result = -1;
6071
0
      }
6072
0
    } else
6073
0
#endif
6074
0
        result = rmdir(path->narrow);
6075
0
#endif
6076
0
    Py_END_ALLOW_THREADS
6077
6078
0
#ifdef HAVE_UNLINKAT
6079
0
    if (unlinkat_unavailable) {
6080
0
        argument_unavailable_error("rmdir", "dir_fd");
6081
0
        return NULL;
6082
0
    }
6083
0
#endif
6084
6085
0
    if (result)
6086
0
        return path_error(path);
6087
6088
0
    Py_RETURN_NONE;
6089
0
}
6090
6091
6092
#ifdef HAVE_SYSTEM
6093
#ifdef MS_WINDOWS
6094
/*[clinic input]
6095
os.system -> long
6096
6097
    command: Py_UNICODE
6098
6099
Execute the command in a subshell.
6100
[clinic start generated code]*/
6101
6102
static long
6103
os_system_impl(PyObject *module, const wchar_t *command)
6104
/*[clinic end generated code: output=dd528cbd5943a679 input=303f5ce97df606b0]*/
6105
{
6106
    long result;
6107
6108
    if (PySys_Audit("os.system", "(u)", command) < 0) {
6109
        return -1;
6110
    }
6111
6112
    Py_BEGIN_ALLOW_THREADS
6113
    _Py_BEGIN_SUPPRESS_IPH
6114
    result = _wsystem(command);
6115
    _Py_END_SUPPRESS_IPH
6116
    Py_END_ALLOW_THREADS
6117
    return result;
6118
}
6119
#else /* MS_WINDOWS */
6120
/*[clinic input]
6121
os.system -> long
6122
6123
    command: FSConverter
6124
6125
Execute the command in a subshell.
6126
[clinic start generated code]*/
6127
6128
static long
6129
os_system_impl(PyObject *module, PyObject *command)
6130
/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
6131
0
{
6132
0
    long result;
6133
0
    const char *bytes = PyBytes_AsString(command);
6134
6135
0
    if (PySys_Audit("os.system", "(O)", command) < 0) {
6136
0
        return -1;
6137
0
    }
6138
6139
0
    Py_BEGIN_ALLOW_THREADS
6140
0
    result = system(bytes);
6141
0
    Py_END_ALLOW_THREADS
6142
0
    return result;
6143
0
}
6144
#endif
6145
#endif /* HAVE_SYSTEM */
6146
6147
6148
#ifdef HAVE_UMASK
6149
/*[clinic input]
6150
os.umask
6151
6152
    mask: int
6153
    /
6154
6155
Set the current numeric umask and return the previous umask.
6156
[clinic start generated code]*/
6157
6158
static PyObject *
6159
os_umask_impl(PyObject *module, int mask)
6160
/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
6161
0
{
6162
0
    int i = (int)umask(mask);
6163
0
    if (i < 0)
6164
0
        return posix_error();
6165
0
    return PyLong_FromLong((long)i);
6166
0
}
6167
#endif
6168
6169
#ifdef MS_WINDOWS
6170
6171
/* override the default DeleteFileW behavior so that directory
6172
symlinks can be removed with this function, the same as with
6173
Unix symlinks */
6174
BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
6175
{
6176
    WIN32_FILE_ATTRIBUTE_DATA info;
6177
    WIN32_FIND_DATAW find_data;
6178
    HANDLE find_data_handle;
6179
    int is_directory = 0;
6180
    int is_link = 0;
6181
6182
    if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
6183
        is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
6184
6185
        /* Get WIN32_FIND_DATA structure for the path to determine if
6186
           it is a symlink */
6187
        if(is_directory &&
6188
           info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
6189
            find_data_handle = FindFirstFileW(lpFileName, &find_data);
6190
6191
            if(find_data_handle != INVALID_HANDLE_VALUE) {
6192
                /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
6193
                   IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
6194
                is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
6195
                          find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
6196
                FindClose(find_data_handle);
6197
            }
6198
        }
6199
    }
6200
6201
    if (is_directory && is_link)
6202
        return RemoveDirectoryW(lpFileName);
6203
6204
    return DeleteFileW(lpFileName);
6205
}
6206
#endif /* MS_WINDOWS */
6207
6208
6209
/*[clinic input]
6210
os.unlink
6211
6212
    path: path_t
6213
    *
6214
    dir_fd: dir_fd(requires='unlinkat')=None
6215
6216
Remove a file (same as remove()).
6217
6218
If dir_fd is not None, it should be a file descriptor open to a directory,
6219
  and path should be relative; path will then be relative to that directory.
6220
dir_fd may not be implemented on your platform.
6221
  If it is unavailable, using it will raise a NotImplementedError.
6222
6223
[clinic start generated code]*/
6224
6225
static PyObject *
6226
os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
6227
/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
6228
0
{
6229
0
    int result;
6230
0
#ifdef HAVE_UNLINKAT
6231
0
    int unlinkat_unavailable = 0;
6232
0
#endif
6233
6234
0
    if (PySys_Audit("os.remove", "Oi", path->object,
6235
0
                    dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
6236
0
        return NULL;
6237
0
    }
6238
6239
0
    Py_BEGIN_ALLOW_THREADS
6240
0
    _Py_BEGIN_SUPPRESS_IPH
6241
#ifdef MS_WINDOWS
6242
    /* Windows, success=1, UNIX, success=0 */
6243
    result = !Py_DeleteFileW(path->wide);
6244
#else
6245
0
#ifdef HAVE_UNLINKAT
6246
0
    if (dir_fd != DEFAULT_DIR_FD) {
6247
0
      if (HAVE_UNLINKAT_RUNTIME) {
6248
6249
0
        result = unlinkat(dir_fd, path->narrow, 0);
6250
0
      } else {
6251
0
        unlinkat_unavailable = 1;
6252
0
      }
6253
0
    } else
6254
0
#endif /* HAVE_UNLINKAT */
6255
0
        result = unlink(path->narrow);
6256
0
#endif
6257
0
    _Py_END_SUPPRESS_IPH
6258
0
    Py_END_ALLOW_THREADS
6259
6260
0
#ifdef HAVE_UNLINKAT
6261
0
    if (unlinkat_unavailable) {
6262
0
        argument_unavailable_error(NULL, "dir_fd");
6263
0
        return NULL;
6264
0
    }
6265
0
#endif
6266
6267
0
    if (result)
6268
0
        return path_error(path);
6269
6270
0
    Py_RETURN_NONE;
6271
0
}
6272
6273
6274
/*[clinic input]
6275
os.remove = os.unlink
6276
6277
Remove a file (same as unlink()).
6278
6279
If dir_fd is not None, it should be a file descriptor open to a directory,
6280
  and path should be relative; path will then be relative to that directory.
6281
dir_fd may not be implemented on your platform.
6282
  If it is unavailable, using it will raise a NotImplementedError.
6283
[clinic start generated code]*/
6284
6285
static PyObject *
6286
os_remove_impl(PyObject *module, path_t *path, int dir_fd)
6287
/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
6288
0
{
6289
0
    return os_unlink_impl(module, path, dir_fd);
6290
0
}
6291
6292
6293
static PyStructSequence_Field uname_result_fields[] = {
6294
    {"sysname",    "operating system name"},
6295
    {"nodename",   "name of machine on network (implementation-defined)"},
6296
    {"release",    "operating system release"},
6297
    {"version",    "operating system version"},
6298
    {"machine",    "hardware identifier"},
6299
    {NULL}
6300
};
6301
6302
PyDoc_STRVAR(uname_result__doc__,
6303
"uname_result: Result from os.uname().\n\n\
6304
This object may be accessed either as a tuple of\n\
6305
  (sysname, nodename, release, version, machine),\n\
6306
or via the attributes sysname, nodename, release, version, and machine.\n\
6307
\n\
6308
See os.uname for more information.");
6309
6310
static PyStructSequence_Desc uname_result_desc = {
6311
    MODNAME ".uname_result", /* name */
6312
    uname_result__doc__, /* doc */
6313
    uname_result_fields,
6314
    5
6315
};
6316
6317
#ifdef HAVE_UNAME
6318
/*[clinic input]
6319
os.uname
6320
6321
Return an object identifying the current operating system.
6322
6323
The object behaves like a named tuple with the following fields:
6324
  (sysname, nodename, release, version, machine)
6325
6326
[clinic start generated code]*/
6327
6328
static PyObject *
6329
os_uname_impl(PyObject *module)
6330
/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
6331
0
{
6332
0
    struct utsname u;
6333
0
    int res;
6334
0
    PyObject *value;
6335
6336
0
    Py_BEGIN_ALLOW_THREADS
6337
0
    res = uname(&u);
6338
0
    Py_END_ALLOW_THREADS
6339
0
    if (res < 0)
6340
0
        return posix_error();
6341
6342
0
    PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
6343
0
    value = PyStructSequence_New((PyTypeObject *)UnameResultType);
6344
0
    if (value == NULL)
6345
0
        return NULL;
6346
6347
0
#define SET(i, field) \
6348
0
    { \
6349
0
    PyObject *o = PyUnicode_DecodeFSDefault(field); \
6350
0
    if (!o) { \
6351
0
        Py_DECREF(value); \
6352
0
        return NULL; \
6353
0
    } \
6354
0
    PyStructSequence_SET_ITEM(value, i, o); \
6355
0
    } \
6356
0
6357
0
    SET(0, u.sysname);
6358
0
    SET(1, u.nodename);
6359
0
    SET(2, u.release);
6360
0
    SET(3, u.version);
6361
0
    SET(4, u.machine);
6362
6363
0
#undef SET
6364
6365
0
    return value;
6366
0
}
6367
#endif /* HAVE_UNAME */
6368
6369
6370
6371
typedef struct {
6372
    int    now;
6373
    time_t atime_s;
6374
    long   atime_ns;
6375
    time_t mtime_s;
6376
    long   mtime_ns;
6377
} utime_t;
6378
6379
/*
6380
 * these macros assume that "ut" is a pointer to a utime_t
6381
 * they also intentionally leak the declaration of a pointer named "time"
6382
 */
6383
#define UTIME_TO_TIMESPEC \
6384
0
    struct timespec ts[2]; \
6385
0
    struct timespec *time; \
6386
0
    if (ut->now) \
6387
0
        time = NULL; \
6388
0
    else { \
6389
0
        ts[0].tv_sec = ut->atime_s; \
6390
0
        ts[0].tv_nsec = ut->atime_ns; \
6391
0
        ts[1].tv_sec = ut->mtime_s; \
6392
0
        ts[1].tv_nsec = ut->mtime_ns; \
6393
0
        time = ts; \
6394
0
    } \
6395
6396
#define UTIME_TO_TIMEVAL \
6397
0
    struct timeval tv[2]; \
6398
0
    struct timeval *time; \
6399
0
    if (ut->now) \
6400
0
        time = NULL; \
6401
0
    else { \
6402
0
        tv[0].tv_sec = ut->atime_s; \
6403
0
        tv[0].tv_usec = ut->atime_ns / 1000; \
6404
0
        tv[1].tv_sec = ut->mtime_s; \
6405
0
        tv[1].tv_usec = ut->mtime_ns / 1000; \
6406
0
        time = tv; \
6407
0
    } \
6408
6409
#define UTIME_TO_UTIMBUF \
6410
    struct utimbuf u; \
6411
    struct utimbuf *time; \
6412
    if (ut->now) \
6413
        time = NULL; \
6414
    else { \
6415
        u.actime = ut->atime_s; \
6416
        u.modtime = ut->mtime_s; \
6417
        time = &u; \
6418
    }
6419
6420
#define UTIME_TO_TIME_T \
6421
    time_t timet[2]; \
6422
    time_t *time; \
6423
    if (ut->now) \
6424
        time = NULL; \
6425
    else { \
6426
        timet[0] = ut->atime_s; \
6427
        timet[1] = ut->mtime_s; \
6428
        time = timet; \
6429
    } \
6430
6431
6432
#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
6433
6434
static int
6435
utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
6436
0
{
6437
#if defined(__APPLE__) &&  defined(HAVE_UTIMENSAT)
6438
    if (HAVE_UTIMENSAT_RUNTIME) {
6439
        int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
6440
        UTIME_TO_TIMESPEC;
6441
        return utimensat(dir_fd, path, time, flags);
6442
    }  else {
6443
        errno = ENOSYS;
6444
        return -1;
6445
    }
6446
#elif defined(HAVE_UTIMENSAT)
6447
0
    int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
6448
0
    UTIME_TO_TIMESPEC;
6449
0
    return utimensat(dir_fd, path, time, flags);
6450
#elif defined(HAVE_FUTIMESAT)
6451
    UTIME_TO_TIMEVAL;
6452
    /*
6453
     * follow_symlinks will never be false here;
6454
     * we only allow !follow_symlinks and dir_fd together
6455
     * if we have utimensat()
6456
     */
6457
    assert(follow_symlinks);
6458
    return futimesat(dir_fd, path, time);
6459
#endif
6460
0
}
6461
6462
0
    #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
6463
#else
6464
    #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
6465
#endif
6466
6467
#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
6468
6469
static int
6470
utime_fd(utime_t *ut, int fd)
6471
0
{
6472
0
#ifdef HAVE_FUTIMENS
6473
6474
0
    if (HAVE_FUTIMENS_RUNTIME) {
6475
6476
0
    UTIME_TO_TIMESPEC;
6477
0
    return futimens(fd, time);
6478
6479
0
    } else
6480
#ifndef HAVE_FUTIMES
6481
    {
6482
        /* Not sure if this can happen */
6483
        PyErr_SetString(
6484
            PyExc_RuntimeError,
6485
            "neither futimens nor futimes are supported"
6486
            " on this system");
6487
        return -1;
6488
    }
6489
#endif
6490
6491
0
#endif
6492
0
#ifdef HAVE_FUTIMES
6493
0
    {
6494
0
    UTIME_TO_TIMEVAL;
6495
0
    return futimes(fd, time);
6496
0
    }
6497
0
#endif
6498
0
}
6499
6500
    #define PATH_UTIME_HAVE_FD 1
6501
#else
6502
    #define PATH_UTIME_HAVE_FD 0
6503
#endif
6504
6505
#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
6506
#  define UTIME_HAVE_NOFOLLOW_SYMLINKS
6507
#endif
6508
6509
#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
6510
6511
static int
6512
utime_nofollow_symlinks(utime_t *ut, const char *path)
6513
0
{
6514
0
#ifdef HAVE_UTIMENSAT
6515
0
    if (HAVE_UTIMENSAT_RUNTIME) {
6516
0
        UTIME_TO_TIMESPEC;
6517
0
        return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
6518
0
    } else
6519
#ifndef HAVE_LUTIMES
6520
    {
6521
        /* Not sure if this can happen */
6522
        PyErr_SetString(
6523
            PyExc_RuntimeError,
6524
            "neither utimensat nor lutimes are supported"
6525
            " on this system");
6526
        return -1;
6527
    }
6528
#endif
6529
0
#endif
6530
6531
0
#ifdef HAVE_LUTIMES
6532
0
    {
6533
0
    UTIME_TO_TIMEVAL;
6534
0
    return lutimes(path, time);
6535
0
    }
6536
0
#endif
6537
0
}
6538
6539
#endif
6540
6541
#ifndef MS_WINDOWS
6542
6543
static int
6544
utime_default(utime_t *ut, const char *path)
6545
0
{
6546
#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
6547
    if (HAVE_UTIMENSAT_RUNTIME) {
6548
        UTIME_TO_TIMESPEC;
6549
        return utimensat(DEFAULT_DIR_FD, path, time, 0);
6550
    } else {
6551
        UTIME_TO_TIMEVAL;
6552
        return utimes(path, time);
6553
    }
6554
#elif defined(HAVE_UTIMENSAT)
6555
0
    UTIME_TO_TIMESPEC;
6556
0
    return utimensat(DEFAULT_DIR_FD, path, time, 0);
6557
#elif defined(HAVE_UTIMES)
6558
    UTIME_TO_TIMEVAL;
6559
    return utimes(path, time);
6560
#elif defined(HAVE_UTIME_H)
6561
    UTIME_TO_UTIMBUF;
6562
    return utime(path, time);
6563
#else
6564
    UTIME_TO_TIME_T;
6565
    return utime(path, time);
6566
#endif
6567
0
}
6568
6569
#endif
6570
6571
static int
6572
split_py_long_to_s_and_ns(PyObject *module, PyObject *py_long, time_t *s, long *ns)
6573
0
{
6574
0
    int result = 0;
6575
0
    PyObject *divmod;
6576
0
    divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion);
6577
0
    if (!divmod)
6578
0
        goto exit;
6579
0
    if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
6580
0
        PyErr_Format(PyExc_TypeError,
6581
0
                     "%.200s.__divmod__() must return a 2-tuple, not %.200s",
6582
0
                     _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
6583
0
        goto exit;
6584
0
    }
6585
0
    *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
6586
0
    if ((*s == -1) && PyErr_Occurred())
6587
0
        goto exit;
6588
0
    *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
6589
0
    if ((*ns == -1) && PyErr_Occurred())
6590
0
        goto exit;
6591
6592
0
    result = 1;
6593
0
exit:
6594
0
    Py_XDECREF(divmod);
6595
0
    return result;
6596
0
}
6597
6598
6599
/*[clinic input]
6600
os.utime
6601
6602
    path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
6603
    times: object = None
6604
    *
6605
    ns: object = NULL
6606
    dir_fd: dir_fd(requires='futimensat') = None
6607
    follow_symlinks: bool=True
6608
6609
# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
6610
6611
Set the access and modified time of path.
6612
6613
path may always be specified as a string.
6614
On some platforms, path may also be specified as an open file descriptor.
6615
  If this functionality is unavailable, using it raises an exception.
6616
6617
If times is not None, it must be a tuple (atime, mtime);
6618
    atime and mtime should be expressed as float seconds since the epoch.
6619
If ns is specified, it must be a tuple (atime_ns, mtime_ns);
6620
    atime_ns and mtime_ns should be expressed as integer nanoseconds
6621
    since the epoch.
6622
If times is None and ns is unspecified, utime uses the current time.
6623
Specifying tuples for both times and ns is an error.
6624
6625
If dir_fd is not None, it should be a file descriptor open to a directory,
6626
  and path should be relative; path will then be relative to that directory.
6627
If follow_symlinks is False, and the last element of the path is a symbolic
6628
  link, utime will modify the symbolic link itself instead of the file the
6629
  link points to.
6630
It is an error to use dir_fd or follow_symlinks when specifying path
6631
  as an open file descriptor.
6632
dir_fd and follow_symlinks may not be available on your platform.
6633
  If they are unavailable, using them will raise a NotImplementedError.
6634
6635
[clinic start generated code]*/
6636
6637
static PyObject *
6638
os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
6639
              int dir_fd, int follow_symlinks)
6640
/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
6641
0
{
6642
#ifdef MS_WINDOWS
6643
    HANDLE hFile;
6644
    FILETIME atime, mtime;
6645
#else
6646
0
    int result;
6647
0
#endif
6648
6649
0
    utime_t utime;
6650
6651
0
    memset(&utime, 0, sizeof(utime_t));
6652
6653
0
    if (times != Py_None && ns) {
6654
0
        PyErr_SetString(PyExc_ValueError,
6655
0
                     "utime: you may specify either 'times'"
6656
0
                     " or 'ns' but not both");
6657
0
        return NULL;
6658
0
    }
6659
6660
0
    if (times != Py_None) {
6661
0
        time_t a_sec, m_sec;
6662
0
        long a_nsec, m_nsec;
6663
0
        if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
6664
0
            PyErr_SetString(PyExc_TypeError,
6665
0
                         "utime: 'times' must be either"
6666
0
                         " a tuple of two ints or None");
6667
0
            return NULL;
6668
0
        }
6669
0
        utime.now = 0;
6670
0
        if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
6671
0
                                     &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
6672
0
            _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
6673
0
                                     &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
6674
0
            return NULL;
6675
0
        }
6676
0
        utime.atime_s = a_sec;
6677
0
        utime.atime_ns = a_nsec;
6678
0
        utime.mtime_s = m_sec;
6679
0
        utime.mtime_ns = m_nsec;
6680
0
    }
6681
0
    else if (ns) {
6682
0
        if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
6683
0
            PyErr_SetString(PyExc_TypeError,
6684
0
                         "utime: 'ns' must be a tuple of two ints");
6685
0
            return NULL;
6686
0
        }
6687
0
        utime.now = 0;
6688
0
        if (!split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 0),
6689
0
                                      &utime.atime_s, &utime.atime_ns) ||
6690
0
            !split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 1),
6691
0
                                       &utime.mtime_s, &utime.mtime_ns)) {
6692
0
            return NULL;
6693
0
        }
6694
0
    }
6695
0
    else {
6696
        /* times and ns are both None/unspecified. use "now". */
6697
0
        utime.now = 1;
6698
0
    }
6699
6700
#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
6701
    if (follow_symlinks_specified("utime", follow_symlinks))
6702
        return NULL;
6703
#endif
6704
6705
0
    if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
6706
0
        dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
6707
0
        fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
6708
0
        return NULL;
6709
6710
#if !defined(HAVE_UTIMENSAT)
6711
    if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
6712
        PyErr_SetString(PyExc_ValueError,
6713
                     "utime: cannot use dir_fd and follow_symlinks "
6714
                     "together on this platform");
6715
        return NULL;
6716
    }
6717
#endif
6718
6719
0
    if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
6720
0
                    dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
6721
0
        return NULL;
6722
0
    }
6723
6724
#ifdef MS_WINDOWS
6725
    Py_BEGIN_ALLOW_THREADS
6726
    hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
6727
                        NULL, OPEN_EXISTING,
6728
                        FILE_FLAG_BACKUP_SEMANTICS, NULL);
6729
    Py_END_ALLOW_THREADS
6730
    if (hFile == INVALID_HANDLE_VALUE) {
6731
        path_error(path);
6732
        return NULL;
6733
    }
6734
6735
    if (utime.now) {
6736
        GetSystemTimeAsFileTime(&mtime);
6737
        atime = mtime;
6738
    }
6739
    else {
6740
        _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
6741
        _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
6742
    }
6743
    if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
6744
        path_error(path);
6745
        CloseHandle(hFile);
6746
        return NULL;
6747
    }
6748
    CloseHandle(hFile);
6749
#else /* MS_WINDOWS */
6750
0
    Py_BEGIN_ALLOW_THREADS
6751
6752
0
#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
6753
0
    if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
6754
0
        result = utime_nofollow_symlinks(&utime, path->narrow);
6755
0
    else
6756
0
#endif
6757
6758
0
#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
6759
0
    if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
6760
0
        result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
6761
6762
0
    } else
6763
0
#endif
6764
6765
0
#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
6766
0
    if (path->fd != -1)
6767
0
        result = utime_fd(&utime, path->fd);
6768
0
    else
6769
0
#endif
6770
6771
0
    result = utime_default(&utime, path->narrow);
6772
6773
0
    Py_END_ALLOW_THREADS
6774
6775
#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
6776
    /* See utime_dir_fd implementation */
6777
    if (result == -1 && errno == ENOSYS) {
6778
        argument_unavailable_error(NULL, "dir_fd");
6779
        return NULL;
6780
    }
6781
#endif
6782
6783
0
    if (result < 0) {
6784
0
        path_error(path);
6785
0
        return NULL;
6786
0
    }
6787
6788
0
#endif /* MS_WINDOWS */
6789
6790
0
    Py_RETURN_NONE;
6791
0
}
6792
6793
/* Process operations */
6794
6795
6796
/*[clinic input]
6797
os._exit
6798
6799
    status: int
6800
6801
Exit to the system with specified status, without normal exit processing.
6802
[clinic start generated code]*/
6803
6804
static PyObject *
6805
os__exit_impl(PyObject *module, int status)
6806
/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
6807
0
{
6808
0
    _exit(status);
6809
0
    return NULL; /* Make gcc -Wall happy */
6810
0
}
6811
6812
#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
6813
#define EXECV_CHAR wchar_t
6814
#else
6815
0
#define EXECV_CHAR char
6816
#endif
6817
6818
#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
6819
static void
6820
free_string_array(EXECV_CHAR **array, Py_ssize_t count)
6821
0
{
6822
0
    Py_ssize_t i;
6823
0
    for (i = 0; i < count; i++)
6824
0
        PyMem_Free(array[i]);
6825
0
    PyMem_Free(array);
6826
0
}
6827
6828
static int
6829
fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
6830
0
{
6831
0
    Py_ssize_t size;
6832
0
    PyObject *ub;
6833
0
    int result = 0;
6834
#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
6835
    if (!PyUnicode_FSDecoder(o, &ub))
6836
        return 0;
6837
    *out = PyUnicode_AsWideCharString(ub, &size);
6838
    if (*out)
6839
        result = 1;
6840
#else
6841
0
    if (!PyUnicode_FSConverter(o, &ub))
6842
0
        return 0;
6843
0
    size = PyBytes_GET_SIZE(ub);
6844
0
    *out = PyMem_Malloc(size + 1);
6845
0
    if (*out) {
6846
0
        memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
6847
0
        result = 1;
6848
0
    } else
6849
0
        PyErr_NoMemory();
6850
0
#endif
6851
0
    Py_DECREF(ub);
6852
0
    return result;
6853
0
}
6854
#endif
6855
6856
#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
6857
static EXECV_CHAR**
6858
parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
6859
0
{
6860
0
    Py_ssize_t i, pos, envc;
6861
0
    PyObject *keys=NULL, *vals=NULL;
6862
0
    PyObject *key2, *val2, *keyval;
6863
0
    EXECV_CHAR **envlist;
6864
6865
0
    i = PyMapping_Size(env);
6866
0
    if (i < 0)
6867
0
        return NULL;
6868
0
    envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
6869
0
    if (envlist == NULL) {
6870
0
        PyErr_NoMemory();
6871
0
        return NULL;
6872
0
    }
6873
0
    envc = 0;
6874
0
    keys = PyMapping_Keys(env);
6875
0
    if (!keys)
6876
0
        goto error;
6877
0
    vals = PyMapping_Values(env);
6878
0
    if (!vals)
6879
0
        goto error;
6880
0
    if (!PyList_Check(keys) || !PyList_Check(vals)) {
6881
0
        PyErr_Format(PyExc_TypeError,
6882
0
                     "env.keys() or env.values() is not a list");
6883
0
        goto error;
6884
0
    }
6885
6886
0
    for (pos = 0; pos < i; pos++) {
6887
0
        PyObject *key = PyList_GetItem(keys, pos);  // Borrowed ref.
6888
0
        if (key == NULL) {
6889
0
            goto error;
6890
0
        }
6891
0
        PyObject *val = PyList_GetItem(vals, pos);  // Borrowed ref.
6892
0
        if (val == NULL) {
6893
0
            goto error;
6894
0
        }
6895
6896
#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
6897
        if (!PyUnicode_FSDecoder(key, &key2))
6898
            goto error;
6899
        if (!PyUnicode_FSDecoder(val, &val2)) {
6900
            Py_DECREF(key2);
6901
            goto error;
6902
        }
6903
        /* Search from index 1 because on Windows starting '=' is allowed for
6904
           defining hidden environment variables. */
6905
        if (PyUnicode_GET_LENGTH(key2) == 0 ||
6906
            PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
6907
        {
6908
            PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
6909
            Py_DECREF(key2);
6910
            Py_DECREF(val2);
6911
            goto error;
6912
        }
6913
        keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
6914
#else
6915
0
        if (!PyUnicode_FSConverter(key, &key2))
6916
0
            goto error;
6917
0
        if (!PyUnicode_FSConverter(val, &val2)) {
6918
0
            Py_DECREF(key2);
6919
0
            goto error;
6920
0
        }
6921
0
        if (PyBytes_GET_SIZE(key2) == 0 ||
6922
0
            strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
6923
0
        {
6924
0
            PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
6925
0
            Py_DECREF(key2);
6926
0
            Py_DECREF(val2);
6927
0
            goto error;
6928
0
        }
6929
0
        keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
6930
0
                                             PyBytes_AS_STRING(val2));
6931
0
#endif
6932
0
        Py_DECREF(key2);
6933
0
        Py_DECREF(val2);
6934
0
        if (!keyval)
6935
0
            goto error;
6936
6937
0
        if (!fsconvert_strdup(keyval, &envlist[envc++])) {
6938
0
            Py_DECREF(keyval);
6939
0
            goto error;
6940
0
        }
6941
6942
0
        Py_DECREF(keyval);
6943
0
    }
6944
0
    Py_DECREF(vals);
6945
0
    Py_DECREF(keys);
6946
6947
0
    envlist[envc] = 0;
6948
0
    *envc_ptr = envc;
6949
0
    return envlist;
6950
6951
0
error:
6952
0
    Py_XDECREF(keys);
6953
0
    Py_XDECREF(vals);
6954
0
    free_string_array(envlist, envc);
6955
0
    return NULL;
6956
0
}
6957
6958
static EXECV_CHAR**
6959
parse_arglist(PyObject* argv, Py_ssize_t *argc)
6960
0
{
6961
0
    int i;
6962
0
    EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
6963
0
    if (argvlist == NULL) {
6964
0
        PyErr_NoMemory();
6965
0
        return NULL;
6966
0
    }
6967
0
    for (i = 0; i < *argc; i++) {
6968
0
        PyObject* item = PySequence_ITEM(argv, i);
6969
0
        if (item == NULL)
6970
0
            goto fail;
6971
0
        if (!fsconvert_strdup(item, &argvlist[i])) {
6972
0
            Py_DECREF(item);
6973
0
            goto fail;
6974
0
        }
6975
0
        Py_DECREF(item);
6976
0
    }
6977
0
    argvlist[*argc] = NULL;
6978
0
    return argvlist;
6979
0
fail:
6980
0
    *argc = i;
6981
0
    free_string_array(argvlist, *argc);
6982
0
    return NULL;
6983
0
}
6984
6985
#endif
6986
6987
6988
#ifdef HAVE_EXECV
6989
/*[clinic input]
6990
os.execv
6991
6992
    path: path_t
6993
        Path of executable file.
6994
    argv: object
6995
        Tuple or list of strings.
6996
    /
6997
6998
Execute an executable path with arguments, replacing current process.
6999
[clinic start generated code]*/
7000
7001
static PyObject *
7002
os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
7003
/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
7004
0
{
7005
0
    EXECV_CHAR **argvlist;
7006
0
    Py_ssize_t argc;
7007
7008
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
7009
0
    if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_EXEC)) {
7010
0
        PyErr_SetString(PyExc_RuntimeError,
7011
0
                        "exec not supported for isolated subinterpreters");
7012
0
        return NULL;
7013
0
    }
7014
7015
    /* execv has two arguments: (path, argv), where
7016
       argv is a list or tuple of strings. */
7017
7018
0
    if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
7019
0
        PyErr_SetString(PyExc_TypeError,
7020
0
                        "execv() arg 2 must be a tuple or list");
7021
0
        return NULL;
7022
0
    }
7023
0
    argc = PySequence_Size(argv);
7024
0
    if (argc < 1) {
7025
0
        PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
7026
0
        return NULL;
7027
0
    }
7028
7029
0
    argvlist = parse_arglist(argv, &argc);
7030
0
    if (argvlist == NULL) {
7031
0
        return NULL;
7032
0
    }
7033
0
    if (!argvlist[0][0]) {
7034
0
        PyErr_SetString(PyExc_ValueError,
7035
0
            "execv() arg 2 first element cannot be empty");
7036
0
        free_string_array(argvlist, argc);
7037
0
        return NULL;
7038
0
    }
7039
7040
0
    if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
7041
0
        free_string_array(argvlist, argc);
7042
0
        return NULL;
7043
0
    }
7044
7045
0
    _Py_BEGIN_SUPPRESS_IPH
7046
#ifdef HAVE_WEXECV
7047
    _wexecv(path->wide, argvlist);
7048
#else
7049
0
    execv(path->narrow, argvlist);
7050
0
#endif
7051
0
    _Py_END_SUPPRESS_IPH
7052
7053
    /* If we get here it's definitely an error */
7054
7055
0
    posix_error();
7056
0
    free_string_array(argvlist, argc);
7057
0
    return NULL;
7058
0
}
7059
7060
7061
/*[clinic input]
7062
os.execve
7063
7064
    path: path_t(allow_fd='PATH_HAVE_FEXECVE')
7065
        Path of executable file.
7066
    argv: object
7067
        Tuple or list of strings.
7068
    env: object
7069
        Dictionary of strings mapping to strings.
7070
7071
Execute an executable path with arguments, replacing current process.
7072
[clinic start generated code]*/
7073
7074
static PyObject *
7075
os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
7076
/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
7077
0
{
7078
0
    EXECV_CHAR **argvlist = NULL;
7079
0
    EXECV_CHAR **envlist;
7080
0
    Py_ssize_t argc, envc;
7081
7082
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
7083
0
    if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_EXEC)) {
7084
0
        PyErr_SetString(PyExc_RuntimeError,
7085
0
                        "exec not supported for isolated subinterpreters");
7086
0
        return NULL;
7087
0
    }
7088
7089
    /* execve has three arguments: (path, argv, env), where
7090
       argv is a list or tuple of strings and env is a dictionary
7091
       like posix.environ. */
7092
7093
0
    if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
7094
0
        PyErr_SetString(PyExc_TypeError,
7095
0
                        "execve: argv must be a tuple or list");
7096
0
        goto fail_0;
7097
0
    }
7098
0
    argc = PySequence_Size(argv);
7099
0
    if (argc < 1) {
7100
0
        PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
7101
0
        return NULL;
7102
0
    }
7103
7104
0
    if (!PyMapping_Check(env)) {
7105
0
        PyErr_SetString(PyExc_TypeError,
7106
0
                        "execve: environment must be a mapping object");
7107
0
        goto fail_0;
7108
0
    }
7109
7110
0
    argvlist = parse_arglist(argv, &argc);
7111
0
    if (argvlist == NULL) {
7112
0
        goto fail_0;
7113
0
    }
7114
0
    if (!argvlist[0][0]) {
7115
0
        PyErr_SetString(PyExc_ValueError,
7116
0
            "execve: argv first element cannot be empty");
7117
0
        goto fail_0;
7118
0
    }
7119
7120
0
    envlist = parse_envlist(env, &envc);
7121
0
    if (envlist == NULL)
7122
0
        goto fail_0;
7123
7124
0
    if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
7125
0
        goto fail_1;
7126
0
    }
7127
7128
0
    _Py_BEGIN_SUPPRESS_IPH
7129
0
#ifdef HAVE_FEXECVE
7130
0
    if (path->fd > -1)
7131
0
        fexecve(path->fd, argvlist, envlist);
7132
0
    else
7133
0
#endif
7134
#ifdef HAVE_WEXECV
7135
        _wexecve(path->wide, argvlist, envlist);
7136
#else
7137
0
        execve(path->narrow, argvlist, envlist);
7138
0
#endif
7139
0
    _Py_END_SUPPRESS_IPH
7140
7141
    /* If we get here it's definitely an error */
7142
7143
0
    posix_path_error(path);
7144
0
  fail_1:
7145
0
    free_string_array(envlist, envc);
7146
0
  fail_0:
7147
0
    if (argvlist)
7148
0
        free_string_array(argvlist, argc);
7149
0
    return NULL;
7150
0
}
7151
7152
#endif /* HAVE_EXECV */
7153
7154
#ifdef HAVE_POSIX_SPAWN
7155
7156
enum posix_spawn_file_actions_identifier {
7157
    POSIX_SPAWN_OPEN,
7158
    POSIX_SPAWN_CLOSE,
7159
    POSIX_SPAWN_DUP2
7160
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
7161
    ,POSIX_SPAWN_CLOSEFROM
7162
#endif
7163
};
7164
7165
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
7166
static int
7167
convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res);
7168
#endif
7169
7170
static int
7171
parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup,
7172
                        int resetids, int setsid, PyObject *setsigmask,
7173
                        PyObject *setsigdef, PyObject *scheduler,
7174
                        posix_spawnattr_t *attrp)
7175
0
{
7176
0
    long all_flags = 0;
7177
7178
0
    errno = posix_spawnattr_init(attrp);
7179
0
    if (errno) {
7180
0
        posix_error();
7181
0
        return -1;
7182
0
    }
7183
7184
0
    if (setpgroup) {
7185
0
        pid_t pgid = PyLong_AsPid(setpgroup);
7186
0
        if (pgid == (pid_t)-1 && PyErr_Occurred()) {
7187
0
            goto fail;
7188
0
        }
7189
0
        errno = posix_spawnattr_setpgroup(attrp, pgid);
7190
0
        if (errno) {
7191
0
            posix_error();
7192
0
            goto fail;
7193
0
        }
7194
0
        all_flags |= POSIX_SPAWN_SETPGROUP;
7195
0
    }
7196
7197
0
    if (resetids) {
7198
0
        all_flags |= POSIX_SPAWN_RESETIDS;
7199
0
    }
7200
7201
0
    if (setsid) {
7202
#ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
7203
        if (HAVE_POSIX_SPAWN_SETSID_RUNTIME) {
7204
#endif
7205
0
#ifdef POSIX_SPAWN_SETSID
7206
0
        all_flags |= POSIX_SPAWN_SETSID;
7207
#elif defined(POSIX_SPAWN_SETSID_NP)
7208
        all_flags |= POSIX_SPAWN_SETSID_NP;
7209
#else
7210
        argument_unavailable_error(func_name, "setsid");
7211
        return -1;
7212
#endif
7213
7214
#ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
7215
        } else {
7216
            argument_unavailable_error(func_name, "setsid");
7217
            return -1;
7218
        }
7219
#endif /* HAVE_POSIX_SPAWN_SETSID_RUNTIME */
7220
7221
0
    }
7222
7223
0
#ifdef HAVE_SIGSET_T
7224
0
   if (setsigmask) {
7225
0
        sigset_t set;
7226
0
        if (!_Py_Sigset_Converter(setsigmask, &set)) {
7227
0
            goto fail;
7228
0
        }
7229
0
        errno = posix_spawnattr_setsigmask(attrp, &set);
7230
0
        if (errno) {
7231
0
            posix_error();
7232
0
            goto fail;
7233
0
        }
7234
0
        all_flags |= POSIX_SPAWN_SETSIGMASK;
7235
0
    }
7236
7237
0
    if (setsigdef) {
7238
0
        sigset_t set;
7239
0
        if (!_Py_Sigset_Converter(setsigdef, &set)) {
7240
0
            goto fail;
7241
0
        }
7242
0
        errno = posix_spawnattr_setsigdefault(attrp, &set);
7243
0
        if (errno) {
7244
0
            posix_error();
7245
0
            goto fail;
7246
0
        }
7247
0
        all_flags |= POSIX_SPAWN_SETSIGDEF;
7248
0
    }
7249
#else
7250
    if (setsigmask || setsigdef) {
7251
        PyErr_SetString(PyExc_NotImplementedError,
7252
                        "sigset is not supported on this platform");
7253
        goto fail;
7254
    }
7255
#endif
7256
7257
0
    if (scheduler) {
7258
0
#ifdef POSIX_SPAWN_SETSCHEDULER
7259
0
        PyObject *py_schedpolicy;
7260
0
        PyObject *schedparam_obj;
7261
0
        struct sched_param schedparam;
7262
7263
0
        if (!PyArg_ParseTuple(scheduler, "OO"
7264
0
                        ";A scheduler tuple must have two elements",
7265
0
                        &py_schedpolicy, &schedparam_obj)) {
7266
0
            goto fail;
7267
0
        }
7268
0
        if (!convert_sched_param(module, schedparam_obj, &schedparam)) {
7269
0
            goto fail;
7270
0
        }
7271
0
        if (py_schedpolicy != Py_None) {
7272
0
            int schedpolicy = PyLong_AsInt(py_schedpolicy);
7273
7274
0
            if (schedpolicy == -1 && PyErr_Occurred()) {
7275
0
                goto fail;
7276
0
            }
7277
0
            errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
7278
0
            if (errno) {
7279
0
                posix_error();
7280
0
                goto fail;
7281
0
            }
7282
0
            all_flags |= POSIX_SPAWN_SETSCHEDULER;
7283
0
        }
7284
0
        errno = posix_spawnattr_setschedparam(attrp, &schedparam);
7285
0
        if (errno) {
7286
0
            posix_error();
7287
0
            goto fail;
7288
0
        }
7289
0
        all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
7290
#else
7291
        PyErr_SetString(PyExc_NotImplementedError,
7292
                "The scheduler option is not supported in this system.");
7293
        goto fail;
7294
#endif
7295
0
    }
7296
7297
0
    errno = posix_spawnattr_setflags(attrp, all_flags);
7298
0
    if (errno) {
7299
0
        posix_error();
7300
0
        goto fail;
7301
0
    }
7302
7303
0
    return 0;
7304
7305
0
fail:
7306
0
    (void)posix_spawnattr_destroy(attrp);
7307
0
    return -1;
7308
0
}
7309
7310
static int
7311
parse_file_actions(PyObject *file_actions,
7312
                   posix_spawn_file_actions_t *file_actionsp,
7313
                   PyObject *temp_buffer)
7314
0
{
7315
0
    PyObject *seq;
7316
0
    PyObject *file_action = NULL;
7317
0
    PyObject *tag_obj;
7318
7319
0
    seq = PySequence_Fast(file_actions,
7320
0
                          "file_actions must be a sequence or None");
7321
0
    if (seq == NULL) {
7322
0
        return -1;
7323
0
    }
7324
7325
0
    errno = posix_spawn_file_actions_init(file_actionsp);
7326
0
    if (errno) {
7327
0
        posix_error();
7328
0
        Py_DECREF(seq);
7329
0
        return -1;
7330
0
    }
7331
7332
0
    for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
7333
0
        file_action = PySequence_Fast_GET_ITEM(seq, i);
7334
0
        Py_INCREF(file_action);
7335
0
        if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
7336
0
            PyErr_SetString(PyExc_TypeError,
7337
0
                "Each file_actions element must be a non-empty tuple");
7338
0
            goto fail;
7339
0
        }
7340
0
        long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
7341
0
        if (tag == -1 && PyErr_Occurred()) {
7342
0
            goto fail;
7343
0
        }
7344
7345
        /* Populate the file_actions object */
7346
0
        switch (tag) {
7347
0
            case POSIX_SPAWN_OPEN: {
7348
0
                int fd, oflag;
7349
0
                PyObject *path;
7350
0
                unsigned long mode;
7351
0
                if (!PyArg_ParseTuple(file_action, "OiO&ik"
7352
0
                        ";A open file_action tuple must have 5 elements",
7353
0
                        &tag_obj, &fd, PyUnicode_FSConverter, &path,
7354
0
                        &oflag, &mode))
7355
0
                {
7356
0
                    goto fail;
7357
0
                }
7358
0
                if (PyList_Append(temp_buffer, path)) {
7359
0
                    Py_DECREF(path);
7360
0
                    goto fail;
7361
0
                }
7362
0
                errno = posix_spawn_file_actions_addopen(file_actionsp,
7363
0
                        fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
7364
0
                if (errno) {
7365
0
                    posix_error();
7366
0
                    Py_DECREF(path);
7367
0
                    goto fail;
7368
0
                }
7369
0
                Py_DECREF(path);
7370
0
                break;
7371
0
            }
7372
0
            case POSIX_SPAWN_CLOSE: {
7373
0
                int fd;
7374
0
                if (!PyArg_ParseTuple(file_action, "Oi"
7375
0
                        ";A close file_action tuple must have 2 elements",
7376
0
                        &tag_obj, &fd))
7377
0
                {
7378
0
                    goto fail;
7379
0
                }
7380
0
                errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
7381
0
                if (errno) {
7382
0
                    posix_error();
7383
0
                    goto fail;
7384
0
                }
7385
0
                break;
7386
0
            }
7387
0
            case POSIX_SPAWN_DUP2: {
7388
0
                int fd1, fd2;
7389
0
                if (!PyArg_ParseTuple(file_action, "Oii"
7390
0
                        ";A dup2 file_action tuple must have 3 elements",
7391
0
                        &tag_obj, &fd1, &fd2))
7392
0
                {
7393
0
                    goto fail;
7394
0
                }
7395
0
                errno = posix_spawn_file_actions_adddup2(file_actionsp,
7396
0
                                                         fd1, fd2);
7397
0
                if (errno) {
7398
0
                    posix_error();
7399
0
                    goto fail;
7400
0
                }
7401
0
                break;
7402
0
            }
7403
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
7404
            case POSIX_SPAWN_CLOSEFROM: {
7405
                int fd;
7406
                if (!PyArg_ParseTuple(file_action, "Oi"
7407
                        ";A closefrom file_action tuple must have 2 elements",
7408
                        &tag_obj, &fd))
7409
                {
7410
                    goto fail;
7411
                }
7412
                errno = posix_spawn_file_actions_addclosefrom_np(file_actionsp,
7413
                                                                 fd);
7414
                if (errno) {
7415
                    posix_error();
7416
                    goto fail;
7417
                }
7418
                break;
7419
            }
7420
#endif
7421
0
            default: {
7422
0
                PyErr_SetString(PyExc_TypeError,
7423
0
                                "Unknown file_actions identifier");
7424
0
                goto fail;
7425
0
            }
7426
0
        }
7427
0
        Py_DECREF(file_action);
7428
0
    }
7429
7430
0
    Py_DECREF(seq);
7431
0
    return 0;
7432
7433
0
fail:
7434
0
    Py_DECREF(seq);
7435
0
    Py_DECREF(file_action);
7436
0
    (void)posix_spawn_file_actions_destroy(file_actionsp);
7437
0
    return -1;
7438
0
}
7439
7440
7441
static PyObject *
7442
py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
7443
               PyObject *env, PyObject *file_actions,
7444
               PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
7445
               PyObject *setsigdef, PyObject *scheduler)
7446
0
{
7447
0
    const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
7448
0
    EXECV_CHAR **argvlist = NULL;
7449
0
    EXECV_CHAR **envlist = NULL;
7450
0
    posix_spawn_file_actions_t file_actions_buf;
7451
0
    posix_spawn_file_actions_t *file_actionsp = NULL;
7452
0
    posix_spawnattr_t attr;
7453
0
    posix_spawnattr_t *attrp = NULL;
7454
0
    Py_ssize_t argc, envc;
7455
0
    PyObject *result = NULL;
7456
0
    PyObject *temp_buffer = NULL;
7457
0
    pid_t pid;
7458
0
    int err_code;
7459
7460
    /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
7461
       argv is a list or tuple of strings and env is a dictionary
7462
       like posix.environ. */
7463
7464
0
    if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
7465
0
        PyErr_Format(PyExc_TypeError,
7466
0
                     "%s: argv must be a tuple or list", func_name);
7467
0
        goto exit;
7468
0
    }
7469
0
    argc = PySequence_Size(argv);
7470
0
    if (argc < 1) {
7471
0
        PyErr_Format(PyExc_ValueError,
7472
0
                     "%s: argv must not be empty", func_name);
7473
0
        return NULL;
7474
0
    }
7475
7476
0
    if (!PyMapping_Check(env) && env != Py_None) {
7477
0
        PyErr_Format(PyExc_TypeError,
7478
0
                     "%s: environment must be a mapping object or None", func_name);
7479
0
        goto exit;
7480
0
    }
7481
7482
0
    argvlist = parse_arglist(argv, &argc);
7483
0
    if (argvlist == NULL) {
7484
0
        goto exit;
7485
0
    }
7486
0
    if (!argvlist[0][0]) {
7487
0
        PyErr_Format(PyExc_ValueError,
7488
0
                     "%s: argv first element cannot be empty", func_name);
7489
0
        goto exit;
7490
0
    }
7491
7492
#ifdef USE_DARWIN_NS_GET_ENVIRON
7493
    // There is no environ global in this situation.
7494
    char **environ = NULL;
7495
#endif
7496
7497
0
    if (env == Py_None) {
7498
#ifdef USE_DARWIN_NS_GET_ENVIRON
7499
        environ = *_NSGetEnviron();
7500
#endif
7501
0
        envlist = environ;
7502
0
    } else {
7503
0
        envlist = parse_envlist(env, &envc);
7504
0
        if (envlist == NULL) {
7505
0
            goto exit;
7506
0
        }
7507
0
    }
7508
7509
0
    if (file_actions != NULL && file_actions != Py_None) {
7510
        /* There is a bug in old versions of glibc that makes some of the
7511
         * helper functions for manipulating file actions not copy the provided
7512
         * buffers. The problem is that posix_spawn_file_actions_addopen does not
7513
         * copy the value of path for some old versions of glibc (<2.20).
7514
         * The use of temp_buffer here is a workaround that keeps the
7515
         * python objects that own the buffers alive until posix_spawn gets called.
7516
         * Check https://bugs.python.org/issue33630 and
7517
         * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
7518
0
        temp_buffer = PyList_New(0);
7519
0
        if (!temp_buffer) {
7520
0
            goto exit;
7521
0
        }
7522
0
        if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
7523
0
            goto exit;
7524
0
        }
7525
0
        file_actionsp = &file_actions_buf;
7526
0
    }
7527
7528
0
    if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid,
7529
0
                                setsigmask, setsigdef, scheduler, &attr)) {
7530
0
        goto exit;
7531
0
    }
7532
0
    attrp = &attr;
7533
7534
0
    if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
7535
0
        goto exit;
7536
0
    }
7537
7538
0
    _Py_BEGIN_SUPPRESS_IPH
7539
0
#ifdef HAVE_POSIX_SPAWNP
7540
0
    if (use_posix_spawnp) {
7541
0
        err_code = posix_spawnp(&pid, path->narrow,
7542
0
                                file_actionsp, attrp, argvlist, envlist);
7543
0
    }
7544
0
    else
7545
0
#endif /* HAVE_POSIX_SPAWNP */
7546
0
    {
7547
0
        err_code = posix_spawn(&pid, path->narrow,
7548
0
                               file_actionsp, attrp, argvlist, envlist);
7549
0
    }
7550
0
    _Py_END_SUPPRESS_IPH
7551
7552
0
    if (err_code) {
7553
0
        errno = err_code;
7554
0
        PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
7555
0
        goto exit;
7556
0
    }
7557
#ifdef _Py_MEMORY_SANITIZER
7558
    __msan_unpoison(&pid, sizeof(pid));
7559
#endif
7560
0
    result = PyLong_FromPid(pid);
7561
7562
0
exit:
7563
0
    if (file_actionsp) {
7564
0
        (void)posix_spawn_file_actions_destroy(file_actionsp);
7565
0
    }
7566
0
    if (attrp) {
7567
0
        (void)posix_spawnattr_destroy(attrp);
7568
0
    }
7569
0
    if (envlist && envlist != environ) {
7570
0
        free_string_array(envlist, envc);
7571
0
    }
7572
0
    if (argvlist) {
7573
0
        free_string_array(argvlist, argc);
7574
0
    }
7575
0
    Py_XDECREF(temp_buffer);
7576
0
    return result;
7577
0
}
7578
7579
7580
/*[clinic input]
7581
7582
os.posix_spawn
7583
    path: path_t
7584
        Path of executable file.
7585
    argv: object
7586
        Tuple or list of strings.
7587
    env: object
7588
        Dictionary of strings mapping to strings.
7589
    /
7590
    *
7591
    file_actions: object(c_default='NULL') = ()
7592
        A sequence of file action tuples.
7593
    setpgroup: object = NULL
7594
        The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
7595
    resetids: bool = False
7596
        If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
7597
    setsid: bool = False
7598
        If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
7599
    setsigmask: object(c_default='NULL') = ()
7600
        The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
7601
    setsigdef: object(c_default='NULL') = ()
7602
        The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
7603
    scheduler: object = NULL
7604
        A tuple with the scheduler policy (optional) and parameters.
7605
7606
Execute the program specified by path in a new process.
7607
[clinic start generated code]*/
7608
7609
static PyObject *
7610
os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
7611
                    PyObject *env, PyObject *file_actions,
7612
                    PyObject *setpgroup, int resetids, int setsid,
7613
                    PyObject *setsigmask, PyObject *setsigdef,
7614
                    PyObject *scheduler)
7615
/*[clinic end generated code: output=14a1098c566bc675 input=808aed1090d84e33]*/
7616
0
{
7617
0
    return py_posix_spawn(0, module, path, argv, env, file_actions,
7618
0
                          setpgroup, resetids, setsid, setsigmask, setsigdef,
7619
0
                          scheduler);
7620
0
}
7621
 #endif /* HAVE_POSIX_SPAWN */
7622
7623
7624
7625
#ifdef HAVE_POSIX_SPAWNP
7626
/*[clinic input]
7627
7628
os.posix_spawnp
7629
    path: path_t
7630
        Path of executable file.
7631
    argv: object
7632
        Tuple or list of strings.
7633
    env: object
7634
        Dictionary of strings mapping to strings.
7635
    /
7636
    *
7637
    file_actions: object(c_default='NULL') = ()
7638
        A sequence of file action tuples.
7639
    setpgroup: object = NULL
7640
        The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
7641
    resetids: bool = False
7642
        If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
7643
    setsid: bool = False
7644
        If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
7645
    setsigmask: object(c_default='NULL') = ()
7646
        The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
7647
    setsigdef: object(c_default='NULL') = ()
7648
        The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
7649
    scheduler: object = NULL
7650
        A tuple with the scheduler policy (optional) and parameters.
7651
7652
Execute the program specified by path in a new process.
7653
[clinic start generated code]*/
7654
7655
static PyObject *
7656
os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
7657
                     PyObject *env, PyObject *file_actions,
7658
                     PyObject *setpgroup, int resetids, int setsid,
7659
                     PyObject *setsigmask, PyObject *setsigdef,
7660
                     PyObject *scheduler)
7661
/*[clinic end generated code: output=7b9aaefe3031238d input=9e89e616116752a1]*/
7662
0
{
7663
0
    return py_posix_spawn(1, module, path, argv, env, file_actions,
7664
0
                          setpgroup, resetids, setsid, setsigmask, setsigdef,
7665
0
                          scheduler);
7666
0
}
7667
#endif /* HAVE_POSIX_SPAWNP */
7668
7669
#ifdef HAVE_RTPSPAWN
7670
static intptr_t
7671
_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
7672
               const char  *envp[])
7673
{
7674
     RTP_ID rtpid;
7675
     int status;
7676
     pid_t res;
7677
     int async_err = 0;
7678
7679
     /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
7680
        uStackSize=0 cannot be used, the default stack size is too small for
7681
        Python. */
7682
     if (envp) {
7683
         rtpid = rtpSpawn(rtpFileName, argv, envp,
7684
                          100, 0x1000000, 0, VX_FP_TASK);
7685
     }
7686
     else {
7687
         rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
7688
                          100, 0x1000000, 0, VX_FP_TASK);
7689
     }
7690
     if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
7691
         do {
7692
             res = waitpid((pid_t)rtpid, &status, 0);
7693
         } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7694
7695
         if (res < 0)
7696
             return RTP_ID_ERROR;
7697
         return ((intptr_t)status);
7698
     }
7699
     return ((intptr_t)rtpid);
7700
}
7701
#endif
7702
7703
#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
7704
/*[clinic input]
7705
os.spawnv
7706
7707
    mode: int
7708
        Mode of process creation.
7709
    path: path_t
7710
        Path of executable file.
7711
    argv: object
7712
        Tuple or list of strings.
7713
    /
7714
7715
Execute the program specified by path in a new process.
7716
[clinic start generated code]*/
7717
7718
static PyObject *
7719
os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
7720
/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
7721
{
7722
    EXECV_CHAR **argvlist;
7723
    int i;
7724
    Py_ssize_t argc;
7725
    intptr_t spawnval;
7726
    PyObject *(*getitem)(PyObject *, Py_ssize_t);
7727
7728
    /* spawnv has three arguments: (mode, path, argv), where
7729
       argv is a list or tuple of strings. */
7730
7731
    if (PyList_Check(argv)) {
7732
        argc = PyList_Size(argv);
7733
        getitem = PyList_GetItem;
7734
    }
7735
    else if (PyTuple_Check(argv)) {
7736
        argc = PyTuple_Size(argv);
7737
        getitem = PyTuple_GetItem;
7738
    }
7739
    else {
7740
        PyErr_SetString(PyExc_TypeError,
7741
                        "spawnv() arg 2 must be a tuple or list");
7742
        return NULL;
7743
    }
7744
    if (argc == 0) {
7745
        PyErr_SetString(PyExc_ValueError,
7746
            "spawnv() arg 2 cannot be empty");
7747
        return NULL;
7748
    }
7749
7750
    argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
7751
    if (argvlist == NULL) {
7752
        return PyErr_NoMemory();
7753
    }
7754
    for (i = 0; i < argc; i++) {
7755
        if (!fsconvert_strdup((*getitem)(argv, i),
7756
                              &argvlist[i])) {
7757
            free_string_array(argvlist, i);
7758
            PyErr_SetString(
7759
                PyExc_TypeError,
7760
                "spawnv() arg 2 must contain only strings");
7761
            return NULL;
7762
        }
7763
        if (i == 0 && !argvlist[0][0]) {
7764
            free_string_array(argvlist, i + 1);
7765
            PyErr_SetString(
7766
                PyExc_ValueError,
7767
                "spawnv() arg 2 first element cannot be empty");
7768
            return NULL;
7769
        }
7770
    }
7771
    argvlist[argc] = NULL;
7772
7773
#if !defined(HAVE_RTPSPAWN)
7774
    if (mode == _OLD_P_OVERLAY)
7775
        mode = _P_OVERLAY;
7776
#endif
7777
7778
    if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
7779
                    Py_None) < 0) {
7780
        free_string_array(argvlist, argc);
7781
        return NULL;
7782
    }
7783
7784
    Py_BEGIN_ALLOW_THREADS
7785
    _Py_BEGIN_SUPPRESS_IPH
7786
#ifdef HAVE_WSPAWNV
7787
    spawnval = _wspawnv(mode, path->wide, argvlist);
7788
#elif defined(HAVE_RTPSPAWN)
7789
    spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
7790
#else
7791
    spawnval = _spawnv(mode, path->narrow, argvlist);
7792
#endif
7793
    _Py_END_SUPPRESS_IPH
7794
    Py_END_ALLOW_THREADS
7795
7796
    int saved_errno = errno;
7797
    free_string_array(argvlist, argc);
7798
7799
    if (spawnval == -1) {
7800
        errno = saved_errno;
7801
        posix_error();
7802
        return NULL;
7803
    }
7804
    return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
7805
}
7806
7807
/*[clinic input]
7808
os.spawnve
7809
7810
    mode: int
7811
        Mode of process creation.
7812
    path: path_t
7813
        Path of executable file.
7814
    argv: object
7815
        Tuple or list of strings.
7816
    env: object
7817
        Dictionary of strings mapping to strings.
7818
    /
7819
7820
Execute the program specified by path in a new process.
7821
[clinic start generated code]*/
7822
7823
static PyObject *
7824
os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
7825
                PyObject *env)
7826
/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
7827
{
7828
    EXECV_CHAR **argvlist;
7829
    EXECV_CHAR **envlist;
7830
    PyObject *res = NULL;
7831
    Py_ssize_t argc, i, envc;
7832
    intptr_t spawnval;
7833
    PyObject *(*getitem)(PyObject *, Py_ssize_t);
7834
    Py_ssize_t lastarg = 0;
7835
7836
    /* spawnve has four arguments: (mode, path, argv, env), where
7837
       argv is a list or tuple of strings and env is a dictionary
7838
       like posix.environ. */
7839
7840
    if (PyList_Check(argv)) {
7841
        argc = PyList_Size(argv);
7842
        getitem = PyList_GetItem;
7843
    }
7844
    else if (PyTuple_Check(argv)) {
7845
        argc = PyTuple_Size(argv);
7846
        getitem = PyTuple_GetItem;
7847
    }
7848
    else {
7849
        PyErr_SetString(PyExc_TypeError,
7850
                        "spawnve() arg 2 must be a tuple or list");
7851
        goto fail_0;
7852
    }
7853
    if (argc == 0) {
7854
        PyErr_SetString(PyExc_ValueError,
7855
            "spawnve() arg 2 cannot be empty");
7856
        goto fail_0;
7857
    }
7858
    if (!PyMapping_Check(env)) {
7859
        PyErr_SetString(PyExc_TypeError,
7860
                        "spawnve() arg 3 must be a mapping object");
7861
        goto fail_0;
7862
    }
7863
7864
    argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
7865
    if (argvlist == NULL) {
7866
        PyErr_NoMemory();
7867
        goto fail_0;
7868
    }
7869
    for (i = 0; i < argc; i++) {
7870
        if (!fsconvert_strdup((*getitem)(argv, i),
7871
                              &argvlist[i]))
7872
        {
7873
            lastarg = i;
7874
            goto fail_1;
7875
        }
7876
        if (i == 0 && !argvlist[0][0]) {
7877
            lastarg = i + 1;
7878
            PyErr_SetString(
7879
                PyExc_ValueError,
7880
                "spawnv() arg 2 first element cannot be empty");
7881
            goto fail_1;
7882
        }
7883
    }
7884
    lastarg = argc;
7885
    argvlist[argc] = NULL;
7886
7887
    envlist = parse_envlist(env, &envc);
7888
    if (envlist == NULL)
7889
        goto fail_1;
7890
7891
#if !defined(HAVE_RTPSPAWN)
7892
    if (mode == _OLD_P_OVERLAY)
7893
        mode = _P_OVERLAY;
7894
#endif
7895
7896
    if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
7897
        goto fail_2;
7898
    }
7899
7900
    Py_BEGIN_ALLOW_THREADS
7901
    _Py_BEGIN_SUPPRESS_IPH
7902
#ifdef HAVE_WSPAWNV
7903
    spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
7904
#elif defined(HAVE_RTPSPAWN)
7905
    spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
7906
                           (const char **)envlist);
7907
#else
7908
    spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
7909
#endif
7910
    _Py_END_SUPPRESS_IPH
7911
    Py_END_ALLOW_THREADS
7912
7913
    if (spawnval == -1)
7914
        (void) posix_error();
7915
    else
7916
        res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
7917
7918
  fail_2:
7919
    while (--envc >= 0) {
7920
        PyMem_Free(envlist[envc]);
7921
    }
7922
    PyMem_Free(envlist);
7923
  fail_1:
7924
    free_string_array(argvlist, lastarg);
7925
  fail_0:
7926
    return res;
7927
}
7928
7929
#endif /* HAVE_SPAWNV */
7930
7931
#ifdef HAVE_FORK
7932
7933
/* Helper function to validate arguments.
7934
   Returns 0 on success.  non-zero on failure with a TypeError raised.
7935
   If obj is non-NULL it must be callable.  */
7936
static int
7937
check_null_or_callable(PyObject *obj, const char* obj_name)
7938
6
{
7939
6
    if (obj && !PyCallable_Check(obj)) {
7940
0
        PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
7941
0
                     obj_name, _PyType_Name(Py_TYPE(obj)));
7942
0
        return -1;
7943
0
    }
7944
6
    return 0;
7945
6
}
7946
7947
/*[clinic input]
7948
os.register_at_fork
7949
7950
    *
7951
    before: object=NULL
7952
        A callable to be called in the parent before the fork() syscall.
7953
    after_in_child: object=NULL
7954
        A callable to be called in the child after fork().
7955
    after_in_parent: object=NULL
7956
        A callable to be called in the parent after fork().
7957
7958
Register callables to be called when forking a new process.
7959
7960
'before' callbacks are called in reverse order.
7961
'after_in_child' and 'after_in_parent' callbacks are called in order.
7962
7963
[clinic start generated code]*/
7964
7965
static PyObject *
7966
os_register_at_fork_impl(PyObject *module, PyObject *before,
7967
                         PyObject *after_in_child, PyObject *after_in_parent)
7968
/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
7969
2
{
7970
2
    PyInterpreterState *interp;
7971
7972
2
    if (!before && !after_in_child && !after_in_parent) {
7973
0
        PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
7974
0
        return NULL;
7975
0
    }
7976
2
    if (check_null_or_callable(before, "before") ||
7977
2
        check_null_or_callable(after_in_child, "after_in_child") ||
7978
2
        check_null_or_callable(after_in_parent, "after_in_parent")) {
7979
0
        return NULL;
7980
0
    }
7981
2
    interp = _PyInterpreterState_GET();
7982
7983
2
    if (register_at_forker(&interp->before_forkers, before)) {
7984
0
        return NULL;
7985
0
    }
7986
2
    if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
7987
0
        return NULL;
7988
0
    }
7989
2
    if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
7990
0
        return NULL;
7991
0
    }
7992
2
    Py_RETURN_NONE;
7993
2
}
7994
#endif /* HAVE_FORK */
7995
7996
#if defined(HAVE_FORK1) || defined(HAVE_FORKPTY) || defined(HAVE_FORK)
7997
// Common code to raise a warning if we detect there is more than one thread
7998
// running in the process. Best effort, silent if unable to count threads.
7999
// Constraint: Quick. Never overcounts. Never leaves an error set.
8000
//
8001
// This should only be called from the parent process after
8002
// PyOS_AfterFork_Parent().
8003
static void
8004
warn_about_fork_with_threads(const char* name)
8005
0
{
8006
    // It's not safe to issue the warning while the world is stopped, because
8007
    // other threads might be holding locks that we need, which would deadlock.
8008
0
    assert(!_PyRuntime.stoptheworld.world_stopped);
8009
8010
    // TODO: Consider making an `os` module API to return the current number
8011
    // of threads in the process. That'd presumably use this platform code but
8012
    // raise an error rather than using the inaccurate fallback.
8013
0
    Py_ssize_t num_python_threads = 0;
8014
#if defined(__APPLE__) && defined(HAVE_GETPID)
8015
    mach_port_t macos_self = mach_task_self();
8016
    mach_port_t macos_task;
8017
    if (task_for_pid(macos_self, getpid(), &macos_task) == KERN_SUCCESS) {
8018
        thread_array_t macos_threads;
8019
        mach_msg_type_number_t macos_n_threads;
8020
        if (task_threads(macos_task, &macos_threads,
8021
                         &macos_n_threads) == KERN_SUCCESS) {
8022
            num_python_threads = macos_n_threads;
8023
        }
8024
    }
8025
#elif defined(__linux__)
8026
    // Linux /proc/self/stat 20th field is the number of threads.
8027
0
    FILE* proc_stat = fopen("/proc/self/stat", "r");
8028
0
    if (proc_stat) {
8029
0
        size_t n;
8030
        // Size chosen arbitrarily. ~60% more bytes than a 20th column index
8031
        // observed on the author's workstation.
8032
0
        char stat_line[160];
8033
0
        n = fread(&stat_line, 1, 159, proc_stat);
8034
0
        stat_line[n] = '\0';
8035
0
        fclose(proc_stat);
8036
8037
0
        char *saveptr = NULL;
8038
0
        char *field = strtok_r(stat_line, " ", &saveptr);
8039
0
        unsigned int idx;
8040
0
        for (idx = 19; idx && field; --idx) {
8041
0
            field = strtok_r(NULL, " ", &saveptr);
8042
0
        }
8043
0
        if (idx == 0 && field) {  // found the 20th field
8044
0
            num_python_threads = atoi(field);  // 0 on error
8045
0
        }
8046
0
    }
8047
0
#endif
8048
0
    if (num_python_threads <= 0) {
8049
        // Fall back to just the number our threading module knows about.
8050
        // An incomplete view of the world, but better than nothing.
8051
0
        PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
8052
0
        if (!threading) {
8053
0
            PyErr_Clear();
8054
0
            return;
8055
0
        }
8056
0
        PyObject *threading_active =
8057
0
                PyObject_GetAttr(threading, &_Py_ID(_active));
8058
0
        if (!threading_active) {
8059
0
            PyErr_Clear();
8060
0
            Py_DECREF(threading);
8061
0
            return;
8062
0
        }
8063
0
        PyObject *threading_limbo =
8064
0
                PyObject_GetAttr(threading, &_Py_ID(_limbo));
8065
0
        if (!threading_limbo) {
8066
0
            PyErr_Clear();
8067
0
            Py_DECREF(threading);
8068
0
            Py_DECREF(threading_active);
8069
0
            return;
8070
0
        }
8071
0
        Py_DECREF(threading);
8072
        // Duplicating what threading.active_count() does but without holding
8073
        // threading._active_limbo_lock so our count could be inaccurate if
8074
        // these dicts are mid-update from another thread.  Not a big deal.
8075
        // Worst case if someone replaced threading._active or threading._limbo
8076
        // with non-dicts, we get -1 from *Length() below and undercount.
8077
        // Nobody should, but we're best effort so we clear errors and move on.
8078
0
        num_python_threads = (PyMapping_Length(threading_active)
8079
0
                              + PyMapping_Length(threading_limbo));
8080
0
        PyErr_Clear();
8081
0
        Py_DECREF(threading_active);
8082
0
        Py_DECREF(threading_limbo);
8083
0
    }
8084
0
    if (num_python_threads > 1) {
8085
0
        PyErr_WarnFormat(
8086
0
                PyExc_DeprecationWarning, 1,
8087
0
#ifdef HAVE_GETPID
8088
0
                "This process (pid=%d) is multi-threaded, "
8089
#else
8090
                "This process is multi-threaded, "
8091
#endif
8092
0
                "use of %s() may lead to deadlocks in the child.",
8093
0
#ifdef HAVE_GETPID
8094
0
                getpid(),
8095
0
#endif
8096
0
                name);
8097
0
        PyErr_Clear();
8098
0
    }
8099
0
}
8100
#endif  // HAVE_FORK1 || HAVE_FORKPTY || HAVE_FORK
8101
8102
#ifdef HAVE_FORK1
8103
/*[clinic input]
8104
os.fork1
8105
8106
Fork a child process with a single multiplexed (i.e., not bound) thread.
8107
8108
Return 0 to child process and PID of child to parent process.
8109
[clinic start generated code]*/
8110
8111
static PyObject *
8112
os_fork1_impl(PyObject *module)
8113
/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
8114
{
8115
    pid_t pid;
8116
8117
    PyInterpreterState *interp = _PyInterpreterState_GET();
8118
    if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
8119
        PyErr_SetString(PyExc_PythonFinalizationError,
8120
                        "can't fork at interpreter shutdown");
8121
        return NULL;
8122
    }
8123
    if (!_Py_IsMainInterpreter(interp)) {
8124
        PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
8125
        return NULL;
8126
    }
8127
    PyOS_BeforeFork();
8128
    pid = fork1();
8129
    int saved_errno = errno;
8130
    if (pid == 0) {
8131
        /* child: this clobbers and resets the import lock. */
8132
        PyOS_AfterFork_Child();
8133
    } else {
8134
        /* parent: release the import lock. */
8135
        PyOS_AfterFork_Parent();
8136
        // After PyOS_AfterFork_Parent() starts the world to avoid deadlock.
8137
        warn_about_fork_with_threads("fork1");
8138
    }
8139
    if (pid == -1) {
8140
        errno = saved_errno;
8141
        return posix_error();
8142
    }
8143
    return PyLong_FromPid(pid);
8144
}
8145
#endif /* HAVE_FORK1 */
8146
8147
8148
#ifdef HAVE_FORK
8149
/*[clinic input]
8150
os.fork
8151
8152
Fork a child process.
8153
8154
Return 0 to child process and PID of child to parent process.
8155
[clinic start generated code]*/
8156
8157
static PyObject *
8158
os_fork_impl(PyObject *module)
8159
/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
8160
0
{
8161
0
    pid_t pid;
8162
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
8163
0
    if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
8164
0
        PyErr_SetString(PyExc_PythonFinalizationError,
8165
0
                        "can't fork at interpreter shutdown");
8166
0
        return NULL;
8167
0
    }
8168
0
    if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_FORK)) {
8169
0
        PyErr_SetString(PyExc_RuntimeError,
8170
0
                        "fork not supported for isolated subinterpreters");
8171
0
        return NULL;
8172
0
    }
8173
0
    if (PySys_Audit("os.fork", NULL) < 0) {
8174
0
        return NULL;
8175
0
    }
8176
0
    PyOS_BeforeFork();
8177
0
    pid = fork();
8178
0
    int saved_errno = errno;
8179
0
    if (pid == 0) {
8180
        /* child: this clobbers and resets the import lock. */
8181
0
        PyOS_AfterFork_Child();
8182
0
    } else {
8183
        /* parent: release the import lock. */
8184
0
        PyOS_AfterFork_Parent();
8185
        // After PyOS_AfterFork_Parent() starts the world to avoid deadlock.
8186
0
        warn_about_fork_with_threads("fork");
8187
0
    }
8188
0
    if (pid == -1) {
8189
0
        errno = saved_errno;
8190
0
        return posix_error();
8191
0
    }
8192
0
    return PyLong_FromPid(pid);
8193
0
}
8194
#endif /* HAVE_FORK */
8195
8196
8197
#ifdef HAVE_SCHED_H
8198
#ifdef HAVE_SCHED_GET_PRIORITY_MAX
8199
/*[clinic input]
8200
os.sched_get_priority_max
8201
8202
    policy: int
8203
8204
Get the maximum scheduling priority for policy.
8205
[clinic start generated code]*/
8206
8207
static PyObject *
8208
os_sched_get_priority_max_impl(PyObject *module, int policy)
8209
/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
8210
0
{
8211
0
    int max;
8212
8213
0
    max = sched_get_priority_max(policy);
8214
0
    if (max < 0)
8215
0
        return posix_error();
8216
0
    return PyLong_FromLong(max);
8217
0
}
8218
8219
8220
/*[clinic input]
8221
os.sched_get_priority_min
8222
8223
    policy: int
8224
8225
Get the minimum scheduling priority for policy.
8226
[clinic start generated code]*/
8227
8228
static PyObject *
8229
os_sched_get_priority_min_impl(PyObject *module, int policy)
8230
/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
8231
0
{
8232
0
    int min = sched_get_priority_min(policy);
8233
0
    if (min < 0)
8234
0
        return posix_error();
8235
0
    return PyLong_FromLong(min);
8236
0
}
8237
#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
8238
8239
8240
#ifdef HAVE_SCHED_SETSCHEDULER
8241
/*[clinic input]
8242
os.sched_getscheduler
8243
    pid: pid_t
8244
    /
8245
8246
Get the scheduling policy for the process identified by pid.
8247
8248
Passing 0 for pid returns the scheduling policy for the calling process.
8249
[clinic start generated code]*/
8250
8251
static PyObject *
8252
os_sched_getscheduler_impl(PyObject *module, pid_t pid)
8253
/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
8254
0
{
8255
0
    int policy;
8256
8257
0
    policy = sched_getscheduler(pid);
8258
0
    if (policy < 0)
8259
0
        return posix_error();
8260
0
    return PyLong_FromLong(policy);
8261
0
}
8262
#endif /* HAVE_SCHED_SETSCHEDULER */
8263
8264
8265
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
8266
/*[clinic input]
8267
class os.sched_param "PyObject *" "SchedParamType"
8268
8269
@classmethod
8270
os.sched_param.__new__
8271
8272
    sched_priority: object
8273
        A scheduling parameter.
8274
8275
Currently has only one field: sched_priority
8276
[clinic start generated code]*/
8277
8278
static PyObject *
8279
os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
8280
/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
8281
0
{
8282
0
    PyObject *res;
8283
8284
0
    res = PyStructSequence_New(type);
8285
0
    if (!res)
8286
0
        return NULL;
8287
0
    PyStructSequence_SET_ITEM(res, 0, Py_NewRef(sched_priority));
8288
0
    return res;
8289
0
}
8290
8291
static PyObject *
8292
os_sched_param_reduce(PyObject *self, PyObject *Py_UNUSED(dummy))
8293
0
{
8294
0
    return Py_BuildValue("(O(N))", Py_TYPE(self), PyStructSequence_GetItem(self, 0));
8295
0
}
8296
8297
static PyMethodDef os_sched_param_reduce_method = {
8298
    "__reduce__", os_sched_param_reduce, METH_NOARGS | METH_COEXIST, NULL,
8299
};
8300
8301
PyDoc_VAR(os_sched_param__doc__);
8302
8303
static PyStructSequence_Field sched_param_fields[] = {
8304
    {"sched_priority", "the scheduling priority"},
8305
    {0}
8306
};
8307
8308
static PyStructSequence_Desc sched_param_desc = {
8309
    "sched_param", /* name */
8310
    os_sched_param__doc__, /* doc */
8311
    sched_param_fields,
8312
    1
8313
};
8314
8315
static int
8316
convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res)
8317
0
{
8318
0
    long priority;
8319
8320
0
    if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) {
8321
0
        PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
8322
0
        return 0;
8323
0
    }
8324
0
    priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
8325
0
    if (priority == -1 && PyErr_Occurred())
8326
0
        return 0;
8327
0
    if (priority > INT_MAX || priority < INT_MIN) {
8328
0
        PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
8329
0
        return 0;
8330
0
    }
8331
0
    res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
8332
0
    return 1;
8333
0
}
8334
#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
8335
8336
8337
#ifdef HAVE_SCHED_SETSCHEDULER
8338
/*[clinic input]
8339
os.sched_setscheduler
8340
8341
    pid: pid_t
8342
    policy: int
8343
    param as param_obj: object
8344
    /
8345
8346
Set the scheduling policy for the process identified by pid.
8347
8348
If pid is 0, the calling process is changed.
8349
param is an instance of sched_param.
8350
[clinic start generated code]*/
8351
8352
static PyObject *
8353
os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
8354
                           PyObject *param_obj)
8355
/*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/
8356
0
{
8357
0
    struct sched_param param;
8358
0
    if (!convert_sched_param(module, param_obj, &param)) {
8359
0
        return NULL;
8360
0
    }
8361
8362
    /*
8363
    ** sched_setscheduler() returns 0 in Linux, but the previous
8364
    ** scheduling policy under Solaris/Illumos, and others.
8365
    ** On error, -1 is returned in all Operating Systems.
8366
    */
8367
0
    if (sched_setscheduler(pid, policy, &param) == -1)
8368
0
        return posix_error();
8369
0
    Py_RETURN_NONE;
8370
0
}
8371
#endif  /* HAVE_SCHED_SETSCHEDULER*/
8372
8373
8374
#ifdef HAVE_SCHED_SETPARAM
8375
/*[clinic input]
8376
os.sched_getparam
8377
    pid: pid_t
8378
    /
8379
8380
Returns scheduling parameters for the process identified by pid.
8381
8382
If pid is 0, returns parameters for the calling process.
8383
Return value is an instance of sched_param.
8384
[clinic start generated code]*/
8385
8386
static PyObject *
8387
os_sched_getparam_impl(PyObject *module, pid_t pid)
8388
/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
8389
0
{
8390
0
    struct sched_param param;
8391
0
    PyObject *result;
8392
0
    PyObject *priority;
8393
8394
0
    if (sched_getparam(pid, &param))
8395
0
        return posix_error();
8396
0
    PyObject *SchedParamType = get_posix_state(module)->SchedParamType;
8397
0
    result = PyStructSequence_New((PyTypeObject *)SchedParamType);
8398
0
    if (!result)
8399
0
        return NULL;
8400
0
    priority = PyLong_FromLong(param.sched_priority);
8401
0
    if (!priority) {
8402
0
        Py_DECREF(result);
8403
0
        return NULL;
8404
0
    }
8405
0
    PyStructSequence_SET_ITEM(result, 0, priority);
8406
0
    return result;
8407
0
}
8408
8409
8410
/*[clinic input]
8411
os.sched_setparam
8412
    pid: pid_t
8413
    param as param_obj: object
8414
    /
8415
8416
Set scheduling parameters for the process identified by pid.
8417
8418
If pid is 0, sets parameters for the calling process.
8419
param should be an instance of sched_param.
8420
[clinic start generated code]*/
8421
8422
static PyObject *
8423
os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj)
8424
/*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/
8425
0
{
8426
0
    struct sched_param param;
8427
0
    if (!convert_sched_param(module, param_obj, &param)) {
8428
0
        return NULL;
8429
0
    }
8430
8431
0
    if (sched_setparam(pid, &param))
8432
0
        return posix_error();
8433
0
    Py_RETURN_NONE;
8434
0
}
8435
#endif /* HAVE_SCHED_SETPARAM */
8436
8437
8438
#ifdef HAVE_SCHED_RR_GET_INTERVAL
8439
/*[clinic input]
8440
@permit_long_summary
8441
os.sched_rr_get_interval -> double
8442
    pid: pid_t
8443
    /
8444
8445
Return the round-robin quantum for the process identified by pid, in seconds.
8446
8447
Value returned is a float.
8448
[clinic start generated code]*/
8449
8450
static double
8451
os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
8452
/*[clinic end generated code: output=7e2d935833ab47dc input=cab0b83586776b10]*/
8453
0
{
8454
0
    struct timespec interval;
8455
0
    if (sched_rr_get_interval(pid, &interval)) {
8456
0
        posix_error();
8457
0
        return -1.0;
8458
0
    }
8459
#ifdef _Py_MEMORY_SANITIZER
8460
    __msan_unpoison(&interval, sizeof(interval));
8461
#endif
8462
0
    return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
8463
0
}
8464
#endif /* HAVE_SCHED_RR_GET_INTERVAL */
8465
8466
8467
/*[clinic input]
8468
os.sched_yield
8469
8470
Voluntarily relinquish the CPU.
8471
[clinic start generated code]*/
8472
8473
static PyObject *
8474
os_sched_yield_impl(PyObject *module)
8475
/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
8476
0
{
8477
0
    int result;
8478
0
    Py_BEGIN_ALLOW_THREADS
8479
0
    result = sched_yield();
8480
0
    Py_END_ALLOW_THREADS
8481
0
    if (result < 0) {
8482
0
        return posix_error();
8483
0
    }
8484
0
    Py_RETURN_NONE;
8485
0
}
8486
8487
#ifdef HAVE_SCHED_SETAFFINITY
8488
/* The minimum number of CPUs allocated in a cpu_set_t */
8489
static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
8490
8491
/*[clinic input]
8492
os.sched_setaffinity
8493
    pid: pid_t
8494
    mask : object
8495
    /
8496
8497
Set the CPU affinity of the process identified by pid to mask.
8498
8499
mask should be an iterable of integers identifying CPUs.
8500
[clinic start generated code]*/
8501
8502
static PyObject *
8503
os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
8504
/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
8505
0
{
8506
0
    int ncpus;
8507
0
    size_t setsize;
8508
0
    cpu_set_t *cpu_set = NULL;
8509
0
    PyObject *iterator = NULL, *item;
8510
8511
0
    iterator = PyObject_GetIter(mask);
8512
0
    if (iterator == NULL)
8513
0
        return NULL;
8514
8515
0
    ncpus = NCPUS_START;
8516
0
    setsize = CPU_ALLOC_SIZE(ncpus);
8517
0
    cpu_set = CPU_ALLOC(ncpus);
8518
0
    if (cpu_set == NULL) {
8519
0
        PyErr_NoMemory();
8520
0
        goto error;
8521
0
    }
8522
0
    CPU_ZERO_S(setsize, cpu_set);
8523
8524
0
    while ((item = PyIter_Next(iterator))) {
8525
0
        long cpu;
8526
0
        if (!PyIndex_Check(item)) {
8527
0
            PyErr_Format(PyExc_TypeError,
8528
0
                        "expected an iterator of ints, "
8529
0
                        "but iterator yielded %R",
8530
0
                        Py_TYPE(item));
8531
0
            Py_DECREF(item);
8532
0
            goto error;
8533
0
        }
8534
0
        cpu = PyLong_AsLong(item);
8535
0
        Py_DECREF(item);
8536
0
        if (cpu < 0) {
8537
0
            if (!PyErr_Occurred())
8538
0
                PyErr_SetString(PyExc_ValueError, "negative CPU number");
8539
0
            goto error;
8540
0
        }
8541
0
        if (cpu > INT_MAX - 1) {
8542
0
            PyErr_SetString(PyExc_OverflowError, "CPU number too large");
8543
0
            goto error;
8544
0
        }
8545
0
        if (cpu >= ncpus) {
8546
            /* Grow CPU mask to fit the CPU number */
8547
0
            int newncpus = ncpus;
8548
0
            cpu_set_t *newmask;
8549
0
            size_t newsetsize;
8550
0
            while (newncpus <= cpu) {
8551
0
                if (newncpus > INT_MAX / 2)
8552
0
                    newncpus = cpu + 1;
8553
0
                else
8554
0
                    newncpus = newncpus * 2;
8555
0
            }
8556
0
            newmask = CPU_ALLOC(newncpus);
8557
0
            if (newmask == NULL) {
8558
0
                PyErr_NoMemory();
8559
0
                goto error;
8560
0
            }
8561
0
            newsetsize = CPU_ALLOC_SIZE(newncpus);
8562
0
            CPU_ZERO_S(newsetsize, newmask);
8563
0
            memcpy(newmask, cpu_set, setsize);
8564
0
            CPU_FREE(cpu_set);
8565
0
            setsize = newsetsize;
8566
0
            cpu_set = newmask;
8567
0
            ncpus = newncpus;
8568
0
        }
8569
0
        CPU_SET_S(cpu, setsize, cpu_set);
8570
0
    }
8571
0
    if (PyErr_Occurred()) {
8572
0
        goto error;
8573
0
    }
8574
0
    Py_CLEAR(iterator);
8575
8576
0
    if (sched_setaffinity(pid, setsize, cpu_set)) {
8577
0
        posix_error();
8578
0
        goto error;
8579
0
    }
8580
0
    CPU_FREE(cpu_set);
8581
0
    Py_RETURN_NONE;
8582
8583
0
error:
8584
0
    if (cpu_set)
8585
0
        CPU_FREE(cpu_set);
8586
0
    Py_XDECREF(iterator);
8587
0
    return NULL;
8588
0
}
8589
8590
8591
/*[clinic input]
8592
@permit_long_summary
8593
os.sched_getaffinity
8594
    pid: pid_t
8595
    /
8596
8597
Return the affinity of the process identified by pid (or the current process if zero).
8598
8599
The affinity is returned as a set of CPU identifiers.
8600
[clinic start generated code]*/
8601
8602
static PyObject *
8603
os_sched_getaffinity_impl(PyObject *module, pid_t pid)
8604
/*[clinic end generated code: output=f726f2c193c17a4f input=cb79ff13579ef091]*/
8605
0
{
8606
0
    int ncpus = NCPUS_START;
8607
0
    size_t setsize;
8608
0
    cpu_set_t *mask;
8609
8610
0
    while (1) {
8611
0
        setsize = CPU_ALLOC_SIZE(ncpus);
8612
0
        mask = CPU_ALLOC(ncpus);
8613
0
        if (mask == NULL) {
8614
0
            return PyErr_NoMemory();
8615
0
        }
8616
0
        if (sched_getaffinity(pid, setsize, mask) == 0) {
8617
0
            break;
8618
0
        }
8619
0
        CPU_FREE(mask);
8620
0
        if (errno != EINVAL) {
8621
0
            return posix_error();
8622
0
        }
8623
0
        if (ncpus > INT_MAX / 2) {
8624
0
            PyErr_SetString(PyExc_OverflowError,
8625
0
                            "could not allocate a large enough CPU set");
8626
0
            return NULL;
8627
0
        }
8628
0
        ncpus *= 2;
8629
0
    }
8630
8631
0
    PyObject *res = PySet_New(NULL);
8632
0
    if (res == NULL) {
8633
0
        goto error;
8634
0
    }
8635
8636
0
    int cpu = 0;
8637
0
    int count = CPU_COUNT_S(setsize, mask);
8638
0
    for (; count; cpu++) {
8639
0
        if (CPU_ISSET_S(cpu, setsize, mask)) {
8640
0
            PyObject *cpu_num = PyLong_FromLong(cpu);
8641
0
            --count;
8642
0
            if (cpu_num == NULL) {
8643
0
                goto error;
8644
0
            }
8645
0
            if (PySet_Add(res, cpu_num)) {
8646
0
                Py_DECREF(cpu_num);
8647
0
                goto error;
8648
0
            }
8649
0
            Py_DECREF(cpu_num);
8650
0
        }
8651
0
    }
8652
0
    CPU_FREE(mask);
8653
0
    return res;
8654
8655
0
error:
8656
0
    if (mask) {
8657
0
        CPU_FREE(mask);
8658
0
    }
8659
0
    Py_XDECREF(res);
8660
0
    return NULL;
8661
0
}
8662
#endif /* HAVE_SCHED_SETAFFINITY */
8663
8664
#endif /* HAVE_SCHED_H */
8665
8666
8667
#ifdef HAVE_POSIX_OPENPT
8668
/*[clinic input]
8669
os.posix_openpt -> int
8670
8671
    oflag: int
8672
    /
8673
8674
Open and return a file descriptor for a master pseudo-terminal device.
8675
8676
Performs a posix_openpt() C function call. The oflag argument is used to
8677
set file status flags and file access modes as specified in the manual page
8678
of posix_openpt() of your system.
8679
[clinic start generated code]*/
8680
8681
static int
8682
os_posix_openpt_impl(PyObject *module, int oflag)
8683
/*[clinic end generated code: output=ee0bc2624305fc79 input=0de33d0e29693caa]*/
8684
0
{
8685
0
    int fd;
8686
8687
0
#if defined(O_CLOEXEC)
8688
0
    oflag |= O_CLOEXEC;
8689
0
#endif
8690
8691
0
    fd = posix_openpt(oflag);
8692
0
    if (fd == -1) {
8693
0
        posix_error();
8694
0
        return -1;
8695
0
    }
8696
8697
    // Just in case, likely a no-op given O_CLOEXEC above.
8698
0
    if (_Py_set_inheritable(fd, 0, NULL) < 0) {
8699
0
        close(fd);
8700
0
        return -1;
8701
0
    }
8702
8703
0
    return fd;
8704
0
}
8705
#endif /* HAVE_POSIX_OPENPT */
8706
8707
#ifdef HAVE_GRANTPT
8708
/*[clinic input]
8709
os.grantpt
8710
8711
    fd: fildes
8712
        File descriptor of a master pseudo-terminal device.
8713
    /
8714
8715
Grant access to the slave pseudo-terminal device.
8716
8717
Performs a grantpt() C function call.
8718
[clinic start generated code]*/
8719
8720
static PyObject *
8721
os_grantpt_impl(PyObject *module, int fd)
8722
/*[clinic end generated code: output=dfd580015cf548ab input=0668e3b96760e849]*/
8723
0
{
8724
0
    int ret;
8725
0
    int saved_errno;
8726
0
    PyOS_sighandler_t sig_saved;
8727
8728
0
    sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
8729
8730
0
    ret = grantpt(fd);
8731
0
    if (ret == -1)
8732
0
        saved_errno = errno;
8733
8734
0
    PyOS_setsig(SIGCHLD, sig_saved);
8735
8736
0
    if (ret == -1) {
8737
0
        errno = saved_errno;
8738
0
        return posix_error();
8739
0
    }
8740
8741
0
    Py_RETURN_NONE;
8742
0
}
8743
#endif /* HAVE_GRANTPT */
8744
8745
#ifdef HAVE_UNLOCKPT
8746
/*[clinic input]
8747
os.unlockpt
8748
8749
    fd: fildes
8750
        File descriptor of a master pseudo-terminal device.
8751
    /
8752
8753
Unlock a pseudo-terminal master/slave pair.
8754
8755
Performs an unlockpt() C function call.
8756
[clinic start generated code]*/
8757
8758
static PyObject *
8759
os_unlockpt_impl(PyObject *module, int fd)
8760
/*[clinic end generated code: output=e08d354dec12d30c input=de7ab1f59f69a2b4]*/
8761
0
{
8762
0
    if (unlockpt(fd) == -1)
8763
0
        return posix_error();
8764
8765
0
    Py_RETURN_NONE;
8766
0
}
8767
#endif /* HAVE_UNLOCKPT */
8768
8769
#if defined(HAVE_PTSNAME) || defined(HAVE_PTSNAME_R)
8770
static PyObject *
8771
py_ptsname(int fd)
8772
0
{
8773
0
    // POSIX manpage: Upon failure, ptsname() shall return a null pointer
8774
0
    // and may set errno. Always initialize errno to avoid undefined behavior.
8775
0
    errno = 0;
8776
0
    char *name = ptsname(fd);
8777
0
    if (name == NULL) {
8778
0
        return posix_error();
8779
0
    }
8780
0
    return PyUnicode_DecodeFSDefault(name);
8781
0
}
8782
8783
/*[clinic input]
8784
os.ptsname
8785
8786
    fd: fildes
8787
        File descriptor of a master pseudo-terminal device.
8788
    /
8789
8790
Return the name of the slave pseudo-terminal device.
8791
8792
If the ptsname_r() C function is available, it is called;
8793
otherwise, performs a ptsname() C function call.
8794
[clinic start generated code]*/
8795
8796
static PyObject *
8797
os_ptsname_impl(PyObject *module, int fd)
8798
/*[clinic end generated code: output=ef300fadc5675872 input=1369ccc0546f3130]*/
8799
0
{
8800
0
#ifdef HAVE_PTSNAME_R
8801
0
    int ret;
8802
0
    char name[MAXPATHLEN+1];
8803
8804
0
    if (HAVE_PTSNAME_R_RUNTIME) {
8805
0
        ret = ptsname_r(fd, name, sizeof(name));
8806
0
    }
8807
0
    else {
8808
        // fallback to ptsname() if ptsname_r() is not available in runtime.
8809
0
        return py_ptsname(fd);
8810
0
    }
8811
0
    if (ret != 0) {
8812
0
        errno = ret;
8813
0
        return posix_error();
8814
0
    }
8815
8816
0
    return PyUnicode_DecodeFSDefault(name);
8817
#else
8818
    return py_ptsname(fd);
8819
#endif /* HAVE_PTSNAME_R */
8820
0
}
8821
#endif /* defined(HAVE_PTSNAME) || defined(HAVE_PTSNAME_R) */
8822
8823
/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
8824
#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
8825
#  define DEV_PTY_FILE "/dev/ptc"
8826
#  define HAVE_DEV_PTMX
8827
#else
8828
#  define DEV_PTY_FILE "/dev/ptmx"
8829
#endif
8830
8831
#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_LOGIN_TTY) || defined(HAVE_DEV_PTMX)
8832
#ifdef HAVE_PTY_H
8833
#include <pty.h>
8834
#elif defined(HAVE_LIBUTIL_H)
8835
#include <libutil.h>
8836
#elif defined(HAVE_UTIL_H)
8837
#include <util.h>
8838
#endif /* HAVE_PTY_H */
8839
#ifdef HAVE_UTMP_H
8840
#include <utmp.h>
8841
#endif /* HAVE_UTMP_H */
8842
#ifdef HAVE_STROPTS_H
8843
#include <stropts.h>
8844
#endif
8845
#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_LOGIN_TTY) || defined(HAVE_DEV_PTMX) */
8846
8847
8848
#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
8849
/*[clinic input]
8850
os.openpty
8851
8852
Open a pseudo-terminal.
8853
8854
Return a tuple of (master_fd, slave_fd) containing open file descriptors
8855
for both the master and slave ends.
8856
[clinic start generated code]*/
8857
8858
static PyObject *
8859
os_openpty_impl(PyObject *module)
8860
/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
8861
0
{
8862
0
    int master_fd = -1, slave_fd = -1;
8863
#ifndef HAVE_OPENPTY
8864
    char * slave_name;
8865
#endif
8866
#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
8867
    PyOS_sighandler_t sig_saved;
8868
#if defined(__sun) && defined(__SVR4)
8869
    extern char *ptsname(int fildes);
8870
#endif
8871
#endif
8872
8873
0
#ifdef HAVE_OPENPTY
8874
0
    if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
8875
0
        goto posix_error;
8876
8877
0
    if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
8878
0
        goto error;
8879
0
    if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
8880
0
        goto error;
8881
8882
#elif defined(HAVE__GETPTY)
8883
    slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
8884
    if (slave_name == NULL)
8885
        goto posix_error;
8886
    if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
8887
        goto error;
8888
8889
    slave_fd = _Py_open(slave_name, O_RDWR);
8890
    if (slave_fd < 0)
8891
        goto error;
8892
8893
#else
8894
    master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
8895
    if (master_fd < 0)
8896
        goto posix_error;
8897
8898
    sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
8899
8900
    /* change permission of slave */
8901
    if (grantpt(master_fd) < 0) {
8902
        int saved_errno = errno;
8903
        PyOS_setsig(SIGCHLD, sig_saved);
8904
        errno = saved_errno;
8905
        goto posix_error;
8906
    }
8907
8908
    /* unlock slave */
8909
    if (unlockpt(master_fd) < 0) {
8910
        int saved_errno = errno;
8911
        PyOS_setsig(SIGCHLD, sig_saved);
8912
        errno = saved_errno;
8913
        goto posix_error;
8914
    }
8915
8916
    PyOS_setsig(SIGCHLD, sig_saved);
8917
8918
    slave_name = ptsname(master_fd); /* get name of slave */
8919
    if (slave_name == NULL)
8920
        goto posix_error;
8921
8922
    slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
8923
    if (slave_fd == -1)
8924
        goto error;
8925
8926
    if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
8927
        goto posix_error;
8928
8929
#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
8930
    ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
8931
    ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
8932
#ifndef __hpux
8933
    ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
8934
#endif /* __hpux */
8935
#endif /* HAVE_CYGWIN */
8936
#endif /* HAVE_OPENPTY */
8937
8938
0
    return Py_BuildValue("(ii)", master_fd, slave_fd);
8939
8940
0
posix_error:
8941
0
    posix_error();
8942
0
error:
8943
0
    if (master_fd != -1)
8944
0
        close(master_fd);
8945
0
    if (slave_fd != -1)
8946
0
        close(slave_fd);
8947
0
    return NULL;
8948
0
}
8949
#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
8950
8951
8952
#if defined(HAVE_SETSID) && defined(TIOCSCTTY)
8953
#define HAVE_FALLBACK_LOGIN_TTY 1
8954
#endif /* defined(HAVE_SETSID) && defined(TIOCSCTTY) */
8955
8956
#if defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY)
8957
/*[clinic input]
8958
os.login_tty
8959
8960
    fd: fildes
8961
    /
8962
8963
Prepare the tty of which fd is a file descriptor for a new login session.
8964
8965
Make the calling process a session leader; make the tty the
8966
controlling tty, the stdin, the stdout, and the stderr of the
8967
calling process; close fd.
8968
[clinic start generated code]*/
8969
8970
static PyObject *
8971
os_login_tty_impl(PyObject *module, int fd)
8972
/*[clinic end generated code: output=495a79911b4cc1bc input=5f298565099903a2]*/
8973
0
{
8974
0
#ifdef HAVE_LOGIN_TTY
8975
0
    if (login_tty(fd) == -1) {
8976
0
        return posix_error();
8977
0
    }
8978
#else /* defined(HAVE_FALLBACK_LOGIN_TTY) */
8979
    /* Establish a new session. */
8980
    if (setsid() == -1) {
8981
        return posix_error();
8982
    }
8983
8984
    /* The tty becomes the controlling terminal. */
8985
    if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1) {
8986
        return posix_error();
8987
    }
8988
8989
    /* The tty becomes stdin/stdout/stderr */
8990
    if (dup2(fd, 0) == -1 || dup2(fd, 1) == -1 || dup2(fd, 2) == -1) {
8991
        return posix_error();
8992
    }
8993
    if (fd > 2) {
8994
        close(fd);
8995
    }
8996
#endif /* HAVE_LOGIN_TTY */
8997
0
    Py_RETURN_NONE;
8998
0
}
8999
#endif /* defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY) */
9000
9001
9002
#ifdef HAVE_FORKPTY
9003
/*[clinic input]
9004
os.forkpty
9005
9006
Fork a new process with a new pseudo-terminal as controlling tty.
9007
9008
Returns a tuple of (pid, master_fd).
9009
Like fork(), return pid of 0 to the child process,
9010
and pid of child to the parent process.
9011
To both, return fd of newly opened pseudo-terminal.
9012
[clinic start generated code]*/
9013
9014
static PyObject *
9015
os_forkpty_impl(PyObject *module)
9016
/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
9017
0
{
9018
0
    int master_fd = -1;
9019
0
    pid_t pid;
9020
9021
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
9022
0
    if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
9023
0
        PyErr_SetString(PyExc_PythonFinalizationError,
9024
0
                        "can't fork at interpreter shutdown");
9025
0
        return NULL;
9026
0
    }
9027
0
    if (!_Py_IsMainInterpreter(interp)) {
9028
0
        PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
9029
0
        return NULL;
9030
0
    }
9031
0
    if (PySys_Audit("os.forkpty", NULL) < 0) {
9032
0
        return NULL;
9033
0
    }
9034
0
    PyOS_BeforeFork();
9035
0
    pid = forkpty(&master_fd, NULL, NULL, NULL);
9036
0
    if (pid == 0) {
9037
        /* child: this clobbers and resets the import lock. */
9038
0
        PyOS_AfterFork_Child();
9039
0
    } else {
9040
        /* parent: release the import lock. */
9041
0
        PyOS_AfterFork_Parent();
9042
        // After PyOS_AfterFork_Parent() starts the world to avoid deadlock.
9043
0
        warn_about_fork_with_threads("forkpty");
9044
0
    }
9045
0
    if (pid == -1) {
9046
0
        return posix_error();
9047
0
    }
9048
0
    return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
9049
0
}
9050
#endif /* HAVE_FORKPTY */
9051
9052
9053
#ifdef HAVE_GETEGID
9054
/*[clinic input]
9055
os.getegid
9056
9057
Return the current process's effective group id.
9058
[clinic start generated code]*/
9059
9060
static PyObject *
9061
os_getegid_impl(PyObject *module)
9062
/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
9063
16
{
9064
16
    return _PyLong_FromGid(getegid());
9065
16
}
9066
#endif /* HAVE_GETEGID */
9067
9068
9069
#ifdef HAVE_GETEUID
9070
/*[clinic input]
9071
os.geteuid
9072
9073
Return the current process's effective user id.
9074
[clinic start generated code]*/
9075
9076
static PyObject *
9077
os_geteuid_impl(PyObject *module)
9078
/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
9079
16
{
9080
16
    return _PyLong_FromUid(geteuid());
9081
16
}
9082
#endif /* HAVE_GETEUID */
9083
9084
9085
#ifdef HAVE_GETGID
9086
/*[clinic input]
9087
os.getgid
9088
9089
Return the current process's group id.
9090
[clinic start generated code]*/
9091
9092
static PyObject *
9093
os_getgid_impl(PyObject *module)
9094
/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
9095
16
{
9096
16
    return _PyLong_FromGid(getgid());
9097
16
}
9098
#endif /* HAVE_GETGID */
9099
9100
9101
#if defined(HAVE_GETPID)
9102
/*[clinic input]
9103
os.getpid
9104
9105
Return the current process id.
9106
[clinic start generated code]*/
9107
9108
static PyObject *
9109
os_getpid_impl(PyObject *module)
9110
/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
9111
0
{
9112
0
#if !defined(MS_WINDOWS) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
9113
0
    return PyLong_FromPid(getpid());
9114
#else
9115
    return PyLong_FromUnsignedLong(GetCurrentProcessId());
9116
#endif
9117
0
}
9118
#endif /* defined(HAVE_GETPID) */
9119
9120
#ifdef NGROUPS_MAX
9121
0
#define MAX_GROUPS NGROUPS_MAX
9122
#else
9123
    /* defined to be 16 on Solaris7, so this should be a small number */
9124
#define MAX_GROUPS 64
9125
#endif
9126
9127
#ifdef HAVE_GETGROUPLIST
9128
9129
#ifdef __APPLE__
9130
/*[clinic input]
9131
os.getgrouplist
9132
9133
    user: str
9134
        username to lookup
9135
    group as basegid: int
9136
        base group id of the user
9137
    /
9138
9139
Returns a list of groups to which a user belongs.
9140
[clinic start generated code]*/
9141
9142
static PyObject *
9143
os_getgrouplist_impl(PyObject *module, const char *user, int basegid)
9144
/*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/
9145
#else
9146
/*[clinic input]
9147
os.getgrouplist
9148
9149
    user: str
9150
        username to lookup
9151
    group as basegid: gid_t
9152
        base group id of the user
9153
    /
9154
9155
Returns a list of groups to which a user belongs.
9156
[clinic start generated code]*/
9157
9158
static PyObject *
9159
os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid)
9160
/*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/
9161
#endif
9162
0
{
9163
0
    int i, ngroups;
9164
0
    PyObject *list;
9165
#ifdef __APPLE__
9166
    int *groups;
9167
#else
9168
0
    gid_t *groups;
9169
0
#endif
9170
9171
    /*
9172
     * NGROUPS_MAX is defined by POSIX.1 as the maximum
9173
     * number of supplemental groups a users can belong to.
9174
     * We have to increment it by one because
9175
     * getgrouplist() returns both the supplemental groups
9176
     * and the primary group, i.e. all of the groups the
9177
     * user belongs to.
9178
     */
9179
0
    ngroups = 1 + MAX_GROUPS;
9180
9181
0
    while (1) {
9182
#ifdef __APPLE__
9183
        groups = PyMem_New(int, ngroups);
9184
#else
9185
0
        groups = PyMem_New(gid_t, ngroups);
9186
0
#endif
9187
0
        if (groups == NULL) {
9188
0
            return PyErr_NoMemory();
9189
0
        }
9190
9191
0
        int old_ngroups = ngroups;
9192
0
        if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
9193
            /* Success */
9194
0
            break;
9195
0
        }
9196
9197
        /* getgrouplist() fails if the group list is too small */
9198
0
        PyMem_Free(groups);
9199
9200
0
        if (ngroups > old_ngroups) {
9201
            /* If the group list is too small, the glibc implementation of
9202
               getgrouplist() sets ngroups to the total number of groups and
9203
               returns -1. */
9204
0
        }
9205
0
        else {
9206
            /* Double the group list size */
9207
0
            if (ngroups > INT_MAX / 2) {
9208
0
                return PyErr_NoMemory();
9209
0
            }
9210
0
            ngroups *= 2;
9211
0
        }
9212
9213
        /* Retry getgrouplist() with a larger group list */
9214
0
    }
9215
9216
#ifdef _Py_MEMORY_SANITIZER
9217
    /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
9218
    __msan_unpoison(&ngroups, sizeof(ngroups));
9219
    __msan_unpoison(groups, ngroups*sizeof(*groups));
9220
#endif
9221
9222
0
    list = PyList_New(ngroups);
9223
0
    if (list == NULL) {
9224
0
        PyMem_Free(groups);
9225
0
        return NULL;
9226
0
    }
9227
9228
0
    for (i = 0; i < ngroups; i++) {
9229
#ifdef __APPLE__
9230
        PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
9231
#else
9232
0
        PyObject *o = _PyLong_FromGid(groups[i]);
9233
0
#endif
9234
0
        if (o == NULL) {
9235
0
            Py_DECREF(list);
9236
0
            PyMem_Free(groups);
9237
0
            return NULL;
9238
0
        }
9239
0
        PyList_SET_ITEM(list, i, o);
9240
0
    }
9241
9242
0
    PyMem_Free(groups);
9243
9244
0
    return list;
9245
0
}
9246
#endif /* HAVE_GETGROUPLIST */
9247
9248
9249
#ifdef HAVE_GETGROUPS
9250
/*[clinic input]
9251
os.getgroups
9252
9253
Return list of supplemental group IDs for the process.
9254
[clinic start generated code]*/
9255
9256
static PyObject *
9257
os_getgroups_impl(PyObject *module)
9258
/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
9259
0
{
9260
    // Call getgroups with length 0 to get the actual number of groups
9261
0
    int n = getgroups(0, NULL);
9262
0
    if (n < 0) {
9263
0
        return posix_error();
9264
0
    }
9265
9266
0
    if (n == 0) {
9267
0
        return PyList_New(0);
9268
0
    }
9269
9270
0
    gid_t *grouplist = PyMem_New(gid_t, n);
9271
0
    if (grouplist == NULL) {
9272
0
        return PyErr_NoMemory();
9273
0
    }
9274
9275
0
    n = getgroups(n, grouplist);
9276
0
    if (n == -1) {
9277
0
        posix_error();
9278
0
        PyMem_Free(grouplist);
9279
0
        return NULL;
9280
0
    }
9281
9282
0
    PyObject *result = PyList_New(n);
9283
0
    if (result == NULL) {
9284
0
        goto error;
9285
0
    }
9286
9287
0
    for (int i = 0; i < n; ++i) {
9288
0
        PyObject *group = _PyLong_FromGid(grouplist[i]);
9289
0
        if (group == NULL) {
9290
0
            goto error;
9291
0
        }
9292
0
        PyList_SET_ITEM(result, i, group);
9293
0
    }
9294
0
    PyMem_Free(grouplist);
9295
9296
0
    return result;
9297
9298
0
error:
9299
0
    PyMem_Free(grouplist);
9300
0
    Py_XDECREF(result);
9301
0
    return NULL;
9302
0
}
9303
#endif /* HAVE_GETGROUPS */
9304
9305
#ifdef HAVE_INITGROUPS
9306
#ifdef __APPLE__
9307
/*[clinic input]
9308
os.initgroups
9309
9310
    username as oname: FSConverter
9311
    gid: int
9312
    /
9313
9314
Initialize the group access list.
9315
9316
Call the system initgroups() to initialize the group access list with all of
9317
the groups of which the specified username is a member, plus the specified
9318
group id.
9319
[clinic start generated code]*/
9320
9321
static PyObject *
9322
os_initgroups_impl(PyObject *module, PyObject *oname, int gid)
9323
/*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/
9324
#else
9325
/*[clinic input]
9326
os.initgroups
9327
9328
    username as oname: FSConverter
9329
    gid: gid_t
9330
    /
9331
9332
Initialize the group access list.
9333
9334
Call the system initgroups() to initialize the group access list with all of
9335
the groups of which the specified username is a member, plus the specified
9336
group id.
9337
[clinic start generated code]*/
9338
9339
static PyObject *
9340
os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid)
9341
/*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/
9342
#endif
9343
0
{
9344
0
    const char *username = PyBytes_AS_STRING(oname);
9345
9346
0
    if (initgroups(username, gid) == -1)
9347
0
        return PyErr_SetFromErrno(PyExc_OSError);
9348
9349
0
    Py_RETURN_NONE;
9350
0
}
9351
#endif /* HAVE_INITGROUPS */
9352
9353
9354
#ifdef HAVE_GETPGID
9355
/*[clinic input]
9356
os.getpgid
9357
9358
    pid: pid_t
9359
9360
Call the system call getpgid(), and return the result.
9361
[clinic start generated code]*/
9362
9363
static PyObject *
9364
os_getpgid_impl(PyObject *module, pid_t pid)
9365
/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
9366
0
{
9367
0
    pid_t pgid = getpgid(pid);
9368
0
    if (pgid < 0)
9369
0
        return posix_error();
9370
0
    return PyLong_FromPid(pgid);
9371
0
}
9372
#endif /* HAVE_GETPGID */
9373
9374
9375
#ifdef HAVE_GETPGRP
9376
/*[clinic input]
9377
os.getpgrp
9378
9379
Return the current process group id.
9380
[clinic start generated code]*/
9381
9382
static PyObject *
9383
os_getpgrp_impl(PyObject *module)
9384
/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
9385
0
{
9386
#ifdef GETPGRP_HAVE_ARG
9387
    return PyLong_FromPid(getpgrp(0));
9388
#else /* GETPGRP_HAVE_ARG */
9389
0
    return PyLong_FromPid(getpgrp());
9390
0
#endif /* GETPGRP_HAVE_ARG */
9391
0
}
9392
#endif /* HAVE_GETPGRP */
9393
9394
9395
#ifdef HAVE_SETPGRP
9396
/*[clinic input]
9397
os.setpgrp
9398
9399
Make the current process the leader of its process group.
9400
[clinic start generated code]*/
9401
9402
static PyObject *
9403
os_setpgrp_impl(PyObject *module)
9404
/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
9405
0
{
9406
#ifdef SETPGRP_HAVE_ARG
9407
    if (setpgrp(0, 0) < 0)
9408
#else /* SETPGRP_HAVE_ARG */
9409
0
    if (setpgrp() < 0)
9410
0
#endif /* SETPGRP_HAVE_ARG */
9411
0
        return posix_error();
9412
0
    Py_RETURN_NONE;
9413
0
}
9414
#endif /* HAVE_SETPGRP */
9415
9416
#ifdef HAVE_GETPPID
9417
9418
#ifdef MS_WINDOWS
9419
#include <winternl.h>
9420
#include <ProcessSnapshot.h>
9421
9422
// The structure definition in winternl.h may be incomplete.
9423
// This structure is the full version from the MSDN documentation.
9424
typedef struct _PROCESS_BASIC_INFORMATION_FULL {
9425
    NTSTATUS ExitStatus;
9426
    PVOID PebBaseAddress;
9427
    ULONG_PTR AffinityMask;
9428
    LONG BasePriority;
9429
    ULONG_PTR UniqueProcessId;
9430
    ULONG_PTR InheritedFromUniqueProcessId;
9431
} PROCESS_BASIC_INFORMATION_FULL;
9432
9433
typedef NTSTATUS (NTAPI *PNT_QUERY_INFORMATION_PROCESS) (
9434
    IN    HANDLE           ProcessHandle,
9435
    IN    PROCESSINFOCLASS ProcessInformationClass,
9436
    OUT   PVOID            ProcessInformation,
9437
    IN    ULONG            ProcessInformationLength,
9438
    OUT   PULONG           ReturnLength OPTIONAL);
9439
9440
// This function returns the process ID of the parent process.
9441
// Returns 0 on failure.
9442
static ULONG
9443
win32_getppid_fast(void)
9444
{
9445
    NTSTATUS status;
9446
    HMODULE ntdll;
9447
    PNT_QUERY_INFORMATION_PROCESS pNtQueryInformationProcess;
9448
    PROCESS_BASIC_INFORMATION_FULL basic_information;
9449
    static ULONG cached_ppid = 0;
9450
9451
    if (cached_ppid) {
9452
        // No need to query the kernel again.
9453
        return cached_ppid;
9454
    }
9455
9456
    ntdll = GetModuleHandleW(L"ntdll.dll");
9457
    if (!ntdll) {
9458
        return 0;
9459
    }
9460
9461
    pNtQueryInformationProcess = (PNT_QUERY_INFORMATION_PROCESS) GetProcAddress(ntdll, "NtQueryInformationProcess");
9462
    if (!pNtQueryInformationProcess) {
9463
        return 0;
9464
    }
9465
9466
    status = pNtQueryInformationProcess(GetCurrentProcess(),
9467
                                        ProcessBasicInformation,
9468
                                        &basic_information,
9469
                                        sizeof(basic_information),
9470
                                        NULL);
9471
9472
    if (!NT_SUCCESS(status)) {
9473
        return 0;
9474
    }
9475
9476
    // Perform sanity check on the parent process ID we received from NtQueryInformationProcess.
9477
    // The check covers values which exceed the 32-bit range (if running on x64) as well as
9478
    // zero and (ULONG) -1.
9479
9480
    if (basic_information.InheritedFromUniqueProcessId == 0 ||
9481
        basic_information.InheritedFromUniqueProcessId >= ULONG_MAX)
9482
    {
9483
        return 0;
9484
    }
9485
9486
    // Now that we have reached this point, the BasicInformation.InheritedFromUniqueProcessId
9487
    // structure member contains a ULONG_PTR which represents the process ID of our parent
9488
    // process. This process ID will be correctly returned even if the parent process has
9489
    // exited or been terminated.
9490
9491
    cached_ppid = (ULONG) basic_information.InheritedFromUniqueProcessId;
9492
    return cached_ppid;
9493
}
9494
9495
static PyObject*
9496
win32_getppid(void)
9497
{
9498
    DWORD error;
9499
    PyObject* result = NULL;
9500
    HANDLE process = GetCurrentProcess();
9501
    HPSS snapshot = NULL;
9502
    ULONG pid;
9503
9504
    pid = win32_getppid_fast();
9505
    if (pid != 0) {
9506
        return PyLong_FromUnsignedLong(pid);
9507
    }
9508
9509
    // If failure occurs in win32_getppid_fast(), fall back to using the PSS API.
9510
9511
    error = PssCaptureSnapshot(process, PSS_CAPTURE_NONE, 0, &snapshot);
9512
    if (error != ERROR_SUCCESS) {
9513
        return PyErr_SetFromWindowsErr(error);
9514
    }
9515
9516
    PSS_PROCESS_INFORMATION info;
9517
    error = PssQuerySnapshot(snapshot, PSS_QUERY_PROCESS_INFORMATION, &info,
9518
                             sizeof(info));
9519
    if (error == ERROR_SUCCESS) {
9520
        result = PyLong_FromUnsignedLong(info.ParentProcessId);
9521
    }
9522
    else {
9523
        result = PyErr_SetFromWindowsErr(error);
9524
    }
9525
9526
    PssFreeSnapshot(process, snapshot);
9527
    return result;
9528
}
9529
#endif /*MS_WINDOWS*/
9530
9531
9532
/*[clinic input]
9533
os.getppid
9534
9535
Return the parent's process id.
9536
9537
If the parent process has already exited, Windows machines will still
9538
return its id; others systems will return the id of the 'init' process (1).
9539
[clinic start generated code]*/
9540
9541
static PyObject *
9542
os_getppid_impl(PyObject *module)
9543
/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
9544
0
{
9545
#ifdef MS_WINDOWS
9546
    return win32_getppid();
9547
#else
9548
0
    return PyLong_FromPid(getppid());
9549
0
#endif
9550
0
}
9551
#endif /* HAVE_GETPPID */
9552
9553
9554
#ifdef HAVE_GETLOGIN
9555
/*[clinic input]
9556
os.getlogin
9557
9558
Return the actual login name.
9559
[clinic start generated code]*/
9560
9561
static PyObject *
9562
os_getlogin_impl(PyObject *module)
9563
/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
9564
0
{
9565
0
    PyObject *result = NULL;
9566
#ifdef MS_WINDOWS
9567
    wchar_t user_name[UNLEN + 1];
9568
    DWORD num_chars = Py_ARRAY_LENGTH(user_name);
9569
9570
    if (GetUserNameW(user_name, &num_chars)) {
9571
        /* num_chars is the number of unicode chars plus null terminator */
9572
        result = PyUnicode_FromWideChar(user_name, num_chars - 1);
9573
    }
9574
    else
9575
        result = PyErr_SetFromWindowsErr(GetLastError());
9576
#elif defined (HAVE_GETLOGIN_R)
9577
# if defined (HAVE_MAXLOGNAME)
9578
    char name[MAXLOGNAME + 1];
9579
# elif defined (HAVE_UT_NAMESIZE)
9580
    char name[UT_NAMESIZE + 1];
9581
# else
9582
    char name[256];
9583
# endif
9584
0
    int err = getlogin_r(name, sizeof(name));
9585
0
    if (err) {
9586
0
        int old_errno = errno;
9587
0
        errno = -err;
9588
0
        posix_error();
9589
0
        errno = old_errno;
9590
0
    }
9591
0
    else {
9592
0
        result = PyUnicode_DecodeFSDefault(name);
9593
0
    }
9594
#else
9595
    char *name;
9596
    int old_errno = errno;
9597
9598
    errno = 0;
9599
    name = getlogin();
9600
    if (name == NULL) {
9601
        if (errno)
9602
            posix_error();
9603
        else
9604
            PyErr_SetString(PyExc_OSError, "unable to determine login name");
9605
    }
9606
    else
9607
        result = PyUnicode_DecodeFSDefault(name);
9608
    errno = old_errno;
9609
#endif
9610
0
    return result;
9611
0
}
9612
#endif /* HAVE_GETLOGIN */
9613
9614
9615
#ifdef HAVE_GETUID
9616
/*[clinic input]
9617
os.getuid
9618
9619
Return the current process's user id.
9620
[clinic start generated code]*/
9621
9622
static PyObject *
9623
os_getuid_impl(PyObject *module)
9624
/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
9625
16
{
9626
16
    return _PyLong_FromUid(getuid());
9627
16
}
9628
#endif /* HAVE_GETUID */
9629
9630
9631
#ifdef MS_WINDOWS
9632
#define HAVE_KILL
9633
#endif /* MS_WINDOWS */
9634
9635
#ifdef HAVE_KILL
9636
/*[clinic input]
9637
os.kill
9638
9639
    pid: pid_t
9640
    signal: Py_ssize_t
9641
    /
9642
9643
Kill a process with a signal.
9644
[clinic start generated code]*/
9645
9646
static PyObject *
9647
os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
9648
/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
9649
0
{
9650
0
    if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
9651
0
        return NULL;
9652
0
    }
9653
0
#ifndef MS_WINDOWS
9654
0
    if (kill(pid, (int)signal) == -1) {
9655
0
        return posix_error();
9656
0
    }
9657
9658
    // Check immediately if the signal was sent to the current process.
9659
    // Don't micro-optimize pid == getpid(), since PyErr_SetString() check
9660
    // is cheap.
9661
0
    if (PyErr_CheckSignals()) {
9662
0
        return NULL;
9663
0
    }
9664
9665
0
    Py_RETURN_NONE;
9666
#else /* !MS_WINDOWS */
9667
    DWORD sig = (DWORD)signal;
9668
9669
#ifdef HAVE_WINDOWS_CONSOLE_IO
9670
    /* Console processes which share a common console can be sent CTRL+C or
9671
       CTRL+BREAK events, provided they handle said events. */
9672
    if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
9673
        if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
9674
            return PyErr_SetFromWindowsErr(0);
9675
        }
9676
        Py_RETURN_NONE;
9677
    }
9678
#endif /* HAVE_WINDOWS_CONSOLE_IO */
9679
9680
    /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
9681
       attempt to open and terminate the process. */
9682
    HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
9683
    if (handle == NULL) {
9684
        return PyErr_SetFromWindowsErr(0);
9685
    }
9686
9687
    BOOL res = TerminateProcess(handle, sig);
9688
    CloseHandle(handle);
9689
    if (res == 0) {
9690
        return PyErr_SetFromWindowsErr(0);
9691
    }
9692
9693
    Py_RETURN_NONE;
9694
#endif /* !MS_WINDOWS */
9695
0
}
9696
#endif /* HAVE_KILL */
9697
9698
9699
#ifdef HAVE_KILLPG
9700
/*[clinic input]
9701
os.killpg
9702
9703
    pgid: pid_t
9704
    signal: int
9705
    /
9706
9707
Kill a process group with a signal.
9708
[clinic start generated code]*/
9709
9710
static PyObject *
9711
os_killpg_impl(PyObject *module, pid_t pgid, int signal)
9712
/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
9713
0
{
9714
0
    if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
9715
0
        return NULL;
9716
0
    }
9717
    /* XXX some man pages make the `pgid` parameter an int, others
9718
       a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
9719
       take the same type. Moreover, pid_t is always at least as wide as
9720
       int (else compilation of this module fails), which is safe. */
9721
0
    if (killpg(pgid, signal) == -1)
9722
0
        return posix_error();
9723
0
    Py_RETURN_NONE;
9724
0
}
9725
#endif /* HAVE_KILLPG */
9726
9727
9728
#ifdef HAVE_PLOCK
9729
#ifdef HAVE_SYS_LOCK_H
9730
#include <sys/lock.h>
9731
#endif
9732
9733
/*[clinic input]
9734
os.plock
9735
    op: int
9736
    /
9737
9738
Lock program segments into memory.");
9739
[clinic start generated code]*/
9740
9741
static PyObject *
9742
os_plock_impl(PyObject *module, int op)
9743
/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
9744
{
9745
    if (plock(op) == -1)
9746
        return posix_error();
9747
    Py_RETURN_NONE;
9748
}
9749
#endif /* HAVE_PLOCK */
9750
9751
9752
#ifdef HAVE_SETUID
9753
/*[clinic input]
9754
os.setuid
9755
9756
    uid: uid_t
9757
    /
9758
9759
Set the current process's user id.
9760
[clinic start generated code]*/
9761
9762
static PyObject *
9763
os_setuid_impl(PyObject *module, uid_t uid)
9764
/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
9765
0
{
9766
0
    if (setuid(uid) < 0)
9767
0
        return posix_error();
9768
0
    Py_RETURN_NONE;
9769
0
}
9770
#endif /* HAVE_SETUID */
9771
9772
9773
#ifdef HAVE_SETEUID
9774
/*[clinic input]
9775
os.seteuid
9776
9777
    euid: uid_t
9778
    /
9779
9780
Set the current process's effective user id.
9781
[clinic start generated code]*/
9782
9783
static PyObject *
9784
os_seteuid_impl(PyObject *module, uid_t euid)
9785
/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
9786
0
{
9787
0
    if (seteuid(euid) < 0)
9788
0
        return posix_error();
9789
0
    Py_RETURN_NONE;
9790
0
}
9791
#endif /* HAVE_SETEUID */
9792
9793
9794
#ifdef HAVE_SETEGID
9795
/*[clinic input]
9796
os.setegid
9797
9798
    egid: gid_t
9799
    /
9800
9801
Set the current process's effective group id.
9802
[clinic start generated code]*/
9803
9804
static PyObject *
9805
os_setegid_impl(PyObject *module, gid_t egid)
9806
/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
9807
0
{
9808
0
    if (setegid(egid) < 0)
9809
0
        return posix_error();
9810
0
    Py_RETURN_NONE;
9811
0
}
9812
#endif /* HAVE_SETEGID */
9813
9814
9815
#ifdef HAVE_SETREUID
9816
/*[clinic input]
9817
os.setreuid
9818
9819
    ruid: uid_t
9820
    euid: uid_t
9821
    /
9822
9823
Set the current process's real and effective user ids.
9824
[clinic start generated code]*/
9825
9826
static PyObject *
9827
os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
9828
/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
9829
0
{
9830
0
    if (setreuid(ruid, euid) < 0) {
9831
0
        return posix_error();
9832
0
    } else {
9833
0
        Py_RETURN_NONE;
9834
0
    }
9835
0
}
9836
#endif /* HAVE_SETREUID */
9837
9838
9839
#ifdef HAVE_SETREGID
9840
/*[clinic input]
9841
os.setregid
9842
9843
    rgid: gid_t
9844
    egid: gid_t
9845
    /
9846
9847
Set the current process's real and effective group ids.
9848
[clinic start generated code]*/
9849
9850
static PyObject *
9851
os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
9852
/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
9853
0
{
9854
0
    if (setregid(rgid, egid) < 0)
9855
0
        return posix_error();
9856
0
    Py_RETURN_NONE;
9857
0
}
9858
#endif /* HAVE_SETREGID */
9859
9860
9861
#ifdef HAVE_SETGID
9862
/*[clinic input]
9863
os.setgid
9864
    gid: gid_t
9865
    /
9866
9867
Set the current process's group id.
9868
[clinic start generated code]*/
9869
9870
static PyObject *
9871
os_setgid_impl(PyObject *module, gid_t gid)
9872
/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
9873
0
{
9874
0
    if (setgid(gid) < 0)
9875
0
        return posix_error();
9876
0
    Py_RETURN_NONE;
9877
0
}
9878
#endif /* HAVE_SETGID */
9879
9880
9881
#ifdef HAVE_SETGROUPS
9882
/*[clinic input]
9883
os.setgroups
9884
9885
    groups: object
9886
    /
9887
9888
Set the groups of the current process to list.
9889
[clinic start generated code]*/
9890
9891
static PyObject *
9892
os_setgroups(PyObject *module, PyObject *groups)
9893
/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
9894
0
{
9895
0
    if (!PySequence_Check(groups)) {
9896
0
        PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
9897
0
        return NULL;
9898
0
    }
9899
0
    Py_ssize_t len = PySequence_Size(groups);
9900
0
    if (len < 0) {
9901
0
        return NULL;
9902
0
    }
9903
0
    if (len > MAX_GROUPS) {
9904
0
        PyErr_SetString(PyExc_ValueError, "too many groups");
9905
0
        return NULL;
9906
0
    }
9907
9908
0
    gid_t *grouplist = PyMem_New(gid_t, len);
9909
0
    if (grouplist == NULL) {
9910
0
        PyErr_NoMemory();
9911
0
        return NULL;
9912
0
    }
9913
0
    for (Py_ssize_t i = 0; i < len; i++) {
9914
0
        PyObject *elem;
9915
0
        elem = PySequence_GetItem(groups, i);
9916
0
        if (!elem) {
9917
0
            PyMem_Free(grouplist);
9918
0
            return NULL;
9919
0
        }
9920
0
        if (!PyIndex_Check(elem)) {
9921
0
            PyErr_SetString(PyExc_TypeError,
9922
0
                            "groups must be integers");
9923
0
            Py_DECREF(elem);
9924
0
            PyMem_Free(grouplist);
9925
0
            return NULL;
9926
0
        } else {
9927
0
            if (!_Py_Gid_Converter(elem, &grouplist[i])) {
9928
0
                Py_DECREF(elem);
9929
0
                PyMem_Free(grouplist);
9930
0
                return NULL;
9931
0
            }
9932
0
        }
9933
0
        Py_DECREF(elem);
9934
0
    }
9935
9936
0
    if (setgroups(len, grouplist) < 0) {
9937
0
        posix_error();
9938
0
        PyMem_Free(grouplist);
9939
0
        return NULL;
9940
0
    }
9941
0
    PyMem_Free(grouplist);
9942
0
    Py_RETURN_NONE;
9943
0
}
9944
#endif /* HAVE_SETGROUPS */
9945
9946
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
9947
static PyObject *
9948
wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru)
9949
0
{
9950
0
    PyObject *result;
9951
0
    PyObject *struct_rusage;
9952
9953
0
    if (pid == -1)
9954
0
        return posix_error();
9955
9956
    // If wait succeeded but no child was ready to report status, ru will not
9957
    // have been populated.
9958
0
    if (pid == 0) {
9959
0
        memset(ru, 0, sizeof(*ru));
9960
0
    }
9961
9962
0
    struct_rusage = PyImport_ImportModuleAttrString("resource", "struct_rusage");
9963
0
    if (struct_rusage == NULL)
9964
0
        return NULL;
9965
9966
    /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
9967
0
    result = PyStructSequence_New((PyTypeObject*) struct_rusage);
9968
0
    Py_DECREF(struct_rusage);
9969
0
    if (!result)
9970
0
        return NULL;
9971
9972
0
    int pos = 0;
9973
9974
0
#ifndef doubletime
9975
0
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
9976
0
#endif
9977
9978
0
#define SET_RESULT(CALL)                                     \
9979
0
    do {                                                     \
9980
0
        PyObject *item = (CALL);                             \
9981
0
        if (item == NULL) {                                  \
9982
0
            Py_DECREF(result);                               \
9983
0
            return NULL;                                     \
9984
0
        }                                                    \
9985
0
        PyStructSequence_SET_ITEM(result, pos++, item);      \
9986
0
    } while(0)
9987
9988
0
    SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_utime)));
9989
0
    SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_stime)));
9990
0
    SET_RESULT(PyLong_FromLong(ru->ru_maxrss));
9991
0
    SET_RESULT(PyLong_FromLong(ru->ru_ixrss));
9992
0
    SET_RESULT(PyLong_FromLong(ru->ru_idrss));
9993
0
    SET_RESULT(PyLong_FromLong(ru->ru_isrss));
9994
0
    SET_RESULT(PyLong_FromLong(ru->ru_minflt));
9995
0
    SET_RESULT(PyLong_FromLong(ru->ru_majflt));
9996
0
    SET_RESULT(PyLong_FromLong(ru->ru_nswap));
9997
0
    SET_RESULT(PyLong_FromLong(ru->ru_inblock));
9998
0
    SET_RESULT(PyLong_FromLong(ru->ru_oublock));
9999
0
    SET_RESULT(PyLong_FromLong(ru->ru_msgsnd));
10000
0
    SET_RESULT(PyLong_FromLong(ru->ru_msgrcv));
10001
0
    SET_RESULT(PyLong_FromLong(ru->ru_nsignals));
10002
0
    SET_RESULT(PyLong_FromLong(ru->ru_nvcsw));
10003
0
    SET_RESULT(PyLong_FromLong(ru->ru_nivcsw));
10004
0
#undef SET_RESULT
10005
10006
0
    return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
10007
0
}
10008
#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
10009
10010
10011
#ifdef HAVE_WAIT3
10012
/*[clinic input]
10013
os.wait3
10014
10015
    options: int
10016
Wait for completion of a child process.
10017
10018
Returns a tuple of information about the child process:
10019
  (pid, status, rusage)
10020
[clinic start generated code]*/
10021
10022
static PyObject *
10023
os_wait3_impl(PyObject *module, int options)
10024
/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
10025
0
{
10026
0
    pid_t pid;
10027
0
    struct rusage ru;
10028
0
    int async_err = 0;
10029
0
    WAIT_TYPE status;
10030
0
    WAIT_STATUS_INT(status) = 0;
10031
10032
0
    do {
10033
0
        Py_BEGIN_ALLOW_THREADS
10034
0
        pid = wait3(&status, options, &ru);
10035
0
        Py_END_ALLOW_THREADS
10036
0
    } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10037
0
    if (pid < 0)
10038
0
        return (!async_err) ? posix_error() : NULL;
10039
10040
0
    return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru);
10041
0
}
10042
#endif /* HAVE_WAIT3 */
10043
10044
10045
#ifdef HAVE_WAIT4
10046
/*[clinic input]
10047
10048
os.wait4
10049
10050
    pid: pid_t
10051
    options: int
10052
10053
Wait for completion of a specific child process.
10054
10055
Returns a tuple of information about the child process:
10056
  (pid, status, rusage)
10057
[clinic start generated code]*/
10058
10059
static PyObject *
10060
os_wait4_impl(PyObject *module, pid_t pid, int options)
10061
/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
10062
0
{
10063
0
    pid_t res;
10064
0
    struct rusage ru;
10065
0
    int async_err = 0;
10066
0
    WAIT_TYPE status;
10067
0
    WAIT_STATUS_INT(status) = 0;
10068
10069
0
    do {
10070
0
        Py_BEGIN_ALLOW_THREADS
10071
0
        res = wait4(pid, &status, options, &ru);
10072
0
        Py_END_ALLOW_THREADS
10073
0
    } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10074
0
    if (res < 0)
10075
0
        return (!async_err) ? posix_error() : NULL;
10076
10077
0
    return wait_helper(module, res, WAIT_STATUS_INT(status), &ru);
10078
0
}
10079
#endif /* HAVE_WAIT4 */
10080
10081
10082
#if defined(HAVE_WAITID)
10083
/*[clinic input]
10084
os.waitid
10085
10086
    idtype: idtype_t
10087
        Must be one of be P_PID, P_PGID or P_ALL.
10088
    id: id_t
10089
        The id to wait on.
10090
    options: int
10091
        Constructed from the ORing of one or more of WEXITED, WSTOPPED
10092
        or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
10093
    /
10094
10095
Returns the result of waiting for a process or processes.
10096
10097
Returns either waitid_result or None if WNOHANG is specified and there are
10098
no children in a waitable state.
10099
[clinic start generated code]*/
10100
10101
static PyObject *
10102
os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
10103
/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
10104
0
{
10105
0
    PyObject *result;
10106
0
    int res;
10107
0
    int async_err = 0;
10108
0
    siginfo_t si;
10109
0
    si.si_pid = 0;
10110
10111
0
    do {
10112
0
        Py_BEGIN_ALLOW_THREADS
10113
0
        res = waitid(idtype, id, &si, options);
10114
0
        Py_END_ALLOW_THREADS
10115
0
    } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10116
0
    if (res < 0)
10117
0
        return (!async_err) ? posix_error() : NULL;
10118
10119
0
    if (si.si_pid == 0)
10120
0
        Py_RETURN_NONE;
10121
10122
0
    PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
10123
0
    result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
10124
0
    if (!result)
10125
0
        return NULL;
10126
10127
0
    int pos = 0;
10128
10129
0
#define SET_RESULT(CALL)                                     \
10130
0
    do {                                                     \
10131
0
        PyObject *item = (CALL);                             \
10132
0
        if (item == NULL) {                                  \
10133
0
            Py_DECREF(result);                               \
10134
0
            return NULL;                                     \
10135
0
        }                                                    \
10136
0
        PyStructSequence_SET_ITEM(result, pos++, item);      \
10137
0
    } while(0)
10138
10139
0
    SET_RESULT(PyLong_FromPid(si.si_pid));
10140
0
    SET_RESULT(_PyLong_FromUid(si.si_uid));
10141
0
    SET_RESULT(PyLong_FromLong((long)(si.si_signo)));
10142
0
    SET_RESULT(PyLong_FromLong((long)(si.si_status)));
10143
0
    SET_RESULT(PyLong_FromLong((long)(si.si_code)));
10144
10145
0
#undef SET_RESULT
10146
10147
0
    return result;
10148
0
}
10149
#endif /* defined(HAVE_WAITID) */
10150
10151
10152
#if defined(HAVE_WAITPID)
10153
/*[clinic input]
10154
os.waitpid
10155
    pid: pid_t
10156
    options: int
10157
    /
10158
10159
Wait for completion of a given child process.
10160
10161
Returns a tuple of information regarding the child process:
10162
    (pid, status)
10163
10164
The options argument is ignored on Windows.
10165
[clinic start generated code]*/
10166
10167
static PyObject *
10168
os_waitpid_impl(PyObject *module, pid_t pid, int options)
10169
/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
10170
0
{
10171
0
    pid_t res;
10172
0
    int async_err = 0;
10173
0
    WAIT_TYPE status;
10174
0
    WAIT_STATUS_INT(status) = 0;
10175
10176
0
    do {
10177
0
        Py_BEGIN_ALLOW_THREADS
10178
0
        res = waitpid(pid, &status, options);
10179
0
        Py_END_ALLOW_THREADS
10180
0
    } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10181
0
    if (res < 0)
10182
0
        return (!async_err) ? posix_error() : NULL;
10183
10184
0
    return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
10185
0
}
10186
#elif defined(HAVE_CWAIT)
10187
/* MS C has a variant of waitpid() that's usable for most purposes. */
10188
/*[clinic input]
10189
os.waitpid
10190
    pid: intptr_t
10191
    options: int
10192
    /
10193
10194
Wait for completion of a given process.
10195
10196
Returns a tuple of information regarding the process:
10197
    (pid, status << 8)
10198
10199
The options argument is ignored on Windows.
10200
[clinic start generated code]*/
10201
10202
static PyObject *
10203
os_waitpid_impl(PyObject *module, intptr_t pid, int options)
10204
/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
10205
{
10206
    int status;
10207
    intptr_t res;
10208
    int async_err = 0;
10209
10210
    do {
10211
        Py_BEGIN_ALLOW_THREADS
10212
        _Py_BEGIN_SUPPRESS_IPH
10213
        res = _cwait(&status, pid, options);
10214
        _Py_END_SUPPRESS_IPH
10215
        Py_END_ALLOW_THREADS
10216
    } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10217
    if (res < 0)
10218
        return (!async_err) ? posix_error() : NULL;
10219
10220
    unsigned long long ustatus = (unsigned int)status;
10221
10222
    /* shift the status left a byte so this is more like the POSIX waitpid */
10223
    return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8);
10224
}
10225
#endif
10226
10227
10228
#ifdef HAVE_WAIT
10229
/*[clinic input]
10230
os.wait
10231
10232
Wait for completion of a child process.
10233
10234
Returns a tuple of information about the child process:
10235
    (pid, status)
10236
[clinic start generated code]*/
10237
10238
static PyObject *
10239
os_wait_impl(PyObject *module)
10240
/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
10241
0
{
10242
0
    pid_t pid;
10243
0
    int async_err = 0;
10244
0
    WAIT_TYPE status;
10245
0
    WAIT_STATUS_INT(status) = 0;
10246
10247
0
    do {
10248
0
        Py_BEGIN_ALLOW_THREADS
10249
0
        pid = wait(&status);
10250
0
        Py_END_ALLOW_THREADS
10251
0
    } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10252
0
    if (pid < 0)
10253
0
        return (!async_err) ? posix_error() : NULL;
10254
10255
0
    return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
10256
0
}
10257
#endif /* HAVE_WAIT */
10258
10259
10260
// This system call always crashes on older Android versions.
10261
#if defined(__linux__) && defined(__NR_pidfd_open) && \
10262
    !(defined(__ANDROID__) && __ANDROID_API__ < 31)
10263
/*[clinic input]
10264
os.pidfd_open
10265
  pid: pid_t
10266
  flags: unsigned_int = 0
10267
10268
Return a file descriptor referring to the process *pid*.
10269
10270
The descriptor can be used to perform process management without races and
10271
signals.
10272
[clinic start generated code]*/
10273
10274
static PyObject *
10275
os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
10276
/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
10277
0
{
10278
0
    int fd = syscall(__NR_pidfd_open, pid, flags);
10279
0
    if (fd < 0) {
10280
0
        return posix_error();
10281
0
    }
10282
0
    return PyLong_FromLong(fd);
10283
0
}
10284
#endif
10285
10286
10287
#ifdef HAVE_SETNS
10288
/*[clinic input]
10289
os.setns
10290
  fd: fildes
10291
    A file descriptor to a namespace.
10292
  nstype: int = 0
10293
    Type of namespace.
10294
10295
Move the calling thread into different namespaces.
10296
[clinic start generated code]*/
10297
10298
static PyObject *
10299
os_setns_impl(PyObject *module, int fd, int nstype)
10300
/*[clinic end generated code: output=5dbd055bfb66ecd0 input=42787871226bf3ee]*/
10301
0
{
10302
0
    int res;
10303
10304
0
    Py_BEGIN_ALLOW_THREADS
10305
0
    res = setns(fd, nstype);
10306
0
    Py_END_ALLOW_THREADS
10307
10308
0
    if (res != 0) {
10309
0
        return posix_error();
10310
0
    }
10311
10312
0
    Py_RETURN_NONE;
10313
0
}
10314
#endif
10315
10316
10317
#ifdef HAVE_UNSHARE
10318
/*[clinic input]
10319
os.unshare
10320
  flags: int
10321
    Namespaces to be unshared.
10322
10323
Disassociate parts of a process (or thread) execution context.
10324
[clinic start generated code]*/
10325
10326
static PyObject *
10327
os_unshare_impl(PyObject *module, int flags)
10328
/*[clinic end generated code: output=1b3177906dd237ee input=9e065db3232b8b1b]*/
10329
0
{
10330
0
    int res;
10331
10332
0
    Py_BEGIN_ALLOW_THREADS
10333
0
    res = unshare(flags);
10334
0
    Py_END_ALLOW_THREADS
10335
10336
0
    if (res != 0) {
10337
0
        return posix_error();
10338
0
    }
10339
10340
0
    Py_RETURN_NONE;
10341
0
}
10342
#endif
10343
10344
10345
#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
10346
/*[clinic input]
10347
os.readlink
10348
10349
    path: path_t
10350
    *
10351
    dir_fd: dir_fd(requires='readlinkat') = None
10352
10353
Return a string representing the path to which the symbolic link points.
10354
10355
If dir_fd is not None, it should be a file descriptor open to a directory,
10356
and path should be relative; path will then be relative to that directory.
10357
10358
dir_fd may not be implemented on your platform.  If it is unavailable,
10359
using it will raise a NotImplementedError.
10360
[clinic start generated code]*/
10361
10362
static PyObject *
10363
os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
10364
/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
10365
0
{
10366
0
#if defined(HAVE_READLINK)
10367
0
    char buffer[MAXPATHLEN+1];
10368
0
    ssize_t length;
10369
0
#ifdef HAVE_READLINKAT
10370
0
    int readlinkat_unavailable = 0;
10371
0
#endif
10372
10373
0
    Py_BEGIN_ALLOW_THREADS
10374
0
#ifdef HAVE_READLINKAT
10375
0
    if (dir_fd != DEFAULT_DIR_FD) {
10376
0
        if (HAVE_READLINKAT_RUNTIME) {
10377
0
            length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
10378
0
        } else {
10379
0
            readlinkat_unavailable = 1;
10380
0
        }
10381
0
    } else
10382
0
#endif
10383
0
        length = readlink(path->narrow, buffer, MAXPATHLEN);
10384
0
    Py_END_ALLOW_THREADS
10385
10386
0
#ifdef HAVE_READLINKAT
10387
0
    if (readlinkat_unavailable) {
10388
0
        argument_unavailable_error(NULL, "dir_fd");
10389
0
        return NULL;
10390
0
    }
10391
0
#endif
10392
10393
0
    if (length < 0) {
10394
0
        return path_error(path);
10395
0
    }
10396
0
    buffer[length] = '\0';
10397
10398
0
    if (PyUnicode_Check(path->object))
10399
0
        return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
10400
0
    else
10401
0
        return PyBytes_FromStringAndSize(buffer, length);
10402
#elif defined(MS_WINDOWS)
10403
    DWORD n_bytes_returned;
10404
    DWORD io_result = 0;
10405
    HANDLE reparse_point_handle;
10406
    char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
10407
    _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
10408
    PyObject *result = NULL;
10409
10410
    /* First get a handle to the reparse point */
10411
    Py_BEGIN_ALLOW_THREADS
10412
    reparse_point_handle = CreateFileW(
10413
        path->wide,
10414
        0,
10415
        0,
10416
        0,
10417
        OPEN_EXISTING,
10418
        FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
10419
        0);
10420
    if (reparse_point_handle != INVALID_HANDLE_VALUE) {
10421
        /* New call DeviceIoControl to read the reparse point */
10422
        io_result = DeviceIoControl(
10423
            reparse_point_handle,
10424
            FSCTL_GET_REPARSE_POINT,
10425
            0, 0, /* in buffer */
10426
            target_buffer, sizeof(target_buffer),
10427
            &n_bytes_returned,
10428
            0 /* we're not using OVERLAPPED_IO */
10429
            );
10430
        CloseHandle(reparse_point_handle);
10431
    }
10432
    Py_END_ALLOW_THREADS
10433
10434
    if (io_result == 0) {
10435
        return path_error(path);
10436
    }
10437
10438
    wchar_t *name = NULL;
10439
    Py_ssize_t nameLen = 0;
10440
    if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
10441
    {
10442
        name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
10443
                           rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
10444
        nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
10445
    }
10446
    else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
10447
    {
10448
        name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
10449
                           rdb->MountPointReparseBuffer.SubstituteNameOffset);
10450
        nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
10451
    }
10452
    else
10453
    {
10454
        PyErr_SetString(PyExc_ValueError, "not a symbolic link");
10455
    }
10456
    if (name) {
10457
        if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
10458
            /* Our buffer is mutable, so this is okay */
10459
            name[1] = L'\\';
10460
        }
10461
        result = PyUnicode_FromWideChar(name, nameLen);
10462
        if (result && PyBytes_Check(path->object)) {
10463
            Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
10464
        }
10465
    }
10466
    return result;
10467
#endif
10468
0
}
10469
#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
10470
10471
#if defined(MS_WINDOWS)
10472
10473
/* Remove the last portion of the path - return 0 on success */
10474
static int
10475
_dirnameW(WCHAR *path)
10476
{
10477
    WCHAR *ptr;
10478
    size_t length = wcsnlen_s(path, MAX_PATH);
10479
    if (length == MAX_PATH) {
10480
        return -1;
10481
    }
10482
10483
    /* walk the path from the end until a backslash is encountered */
10484
    for(ptr = path + length; ptr != path; ptr--) {
10485
        if (*ptr == L'\\' || *ptr == L'/') {
10486
            break;
10487
        }
10488
    }
10489
    *ptr = 0;
10490
    return 0;
10491
}
10492
10493
#endif
10494
10495
#ifdef HAVE_SYMLINK
10496
10497
#if defined(MS_WINDOWS)
10498
10499
/* Is this path absolute? */
10500
static int
10501
_is_absW(const WCHAR *path)
10502
{
10503
    return path[0] == L'\\' || path[0] == L'/' ||
10504
        (path[0] && path[1] == L':');
10505
}
10506
10507
/* join root and rest with a backslash - return 0 on success */
10508
static int
10509
_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
10510
{
10511
    if (_is_absW(rest)) {
10512
        return wcscpy_s(dest_path, MAX_PATH, rest);
10513
    }
10514
10515
    if (wcscpy_s(dest_path, MAX_PATH, root)) {
10516
        return -1;
10517
    }
10518
10519
    if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
10520
        return -1;
10521
    }
10522
10523
    return wcscat_s(dest_path, MAX_PATH, rest);
10524
}
10525
10526
/* Return True if the path at src relative to dest is a directory */
10527
static int
10528
_check_dirW(LPCWSTR src, LPCWSTR dest)
10529
{
10530
    WIN32_FILE_ATTRIBUTE_DATA src_info;
10531
    WCHAR dest_parent[MAX_PATH];
10532
    WCHAR src_resolved[MAX_PATH] = L"";
10533
10534
    /* dest_parent = os.path.dirname(dest) */
10535
    if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
10536
        _dirnameW(dest_parent)) {
10537
        return 0;
10538
    }
10539
    /* src_resolved = os.path.join(dest_parent, src) */
10540
    if (_joinW(src_resolved, dest_parent, src)) {
10541
        return 0;
10542
    }
10543
    return (
10544
        GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
10545
        && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
10546
    );
10547
}
10548
#endif
10549
10550
10551
/*[clinic input]
10552
os.symlink
10553
    src: path_t
10554
    dst: path_t
10555
    target_is_directory: bool = False
10556
    *
10557
    dir_fd: dir_fd(requires='symlinkat')=None
10558
10559
# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
10560
10561
Create a symbolic link pointing to src named dst.
10562
10563
target_is_directory is required on Windows if the target is to be
10564
  interpreted as a directory.  (On Windows, symlink requires
10565
  Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
10566
  target_is_directory is ignored on non-Windows platforms.
10567
10568
If dir_fd is not None, it should be a file descriptor open to a directory,
10569
  and path should be relative; path will then be relative to that directory.
10570
dir_fd may not be implemented on your platform.
10571
  If it is unavailable, using it will raise a NotImplementedError.
10572
10573
[clinic start generated code]*/
10574
10575
static PyObject *
10576
os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
10577
                int target_is_directory, int dir_fd)
10578
/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
10579
0
{
10580
#ifdef MS_WINDOWS
10581
    DWORD result;
10582
    DWORD flags = 0;
10583
10584
    /* Assumed true, set to false if detected to not be available. */
10585
    static int windows_has_symlink_unprivileged_flag = TRUE;
10586
#else
10587
0
    int result;
10588
0
#ifdef HAVE_SYMLINKAT
10589
0
    int symlinkat_unavailable = 0;
10590
0
#endif
10591
0
#endif
10592
10593
0
    if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
10594
0
                    dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
10595
0
        return NULL;
10596
0
    }
10597
10598
#ifdef MS_WINDOWS
10599
10600
    if (windows_has_symlink_unprivileged_flag) {
10601
        /* Allow non-admin symlinks if system allows it. */
10602
        flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
10603
    }
10604
10605
    Py_BEGIN_ALLOW_THREADS
10606
    _Py_BEGIN_SUPPRESS_IPH
10607
    /* if src is a directory, ensure flags==1 (target_is_directory bit) */
10608
    if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
10609
        flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
10610
    }
10611
10612
    result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
10613
    _Py_END_SUPPRESS_IPH
10614
    Py_END_ALLOW_THREADS
10615
10616
    if (windows_has_symlink_unprivileged_flag && !result &&
10617
        ERROR_INVALID_PARAMETER == GetLastError()) {
10618
10619
        Py_BEGIN_ALLOW_THREADS
10620
        _Py_BEGIN_SUPPRESS_IPH
10621
        /* This error might be caused by
10622
        SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
10623
        Try again, and update windows_has_symlink_unprivileged_flag if we
10624
        are successful this time.
10625
10626
        NOTE: There is a risk of a race condition here if there are other
10627
        conditions than the flag causing ERROR_INVALID_PARAMETER, and
10628
        another process (or thread) changes that condition in between our
10629
        calls to CreateSymbolicLink.
10630
        */
10631
        flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
10632
        result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
10633
        _Py_END_SUPPRESS_IPH
10634
        Py_END_ALLOW_THREADS
10635
10636
        if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
10637
            windows_has_symlink_unprivileged_flag = FALSE;
10638
        }
10639
    }
10640
10641
    if (!result)
10642
        return path_error2(src, dst);
10643
10644
#else
10645
10646
0
    Py_BEGIN_ALLOW_THREADS
10647
0
#ifdef HAVE_SYMLINKAT
10648
0
    if (dir_fd != DEFAULT_DIR_FD) {
10649
0
        if (HAVE_SYMLINKAT_RUNTIME) {
10650
0
            result = symlinkat(src->narrow, dir_fd, dst->narrow);
10651
0
        } else {
10652
0
            symlinkat_unavailable = 1;
10653
0
        }
10654
0
    } else
10655
0
#endif
10656
0
        result = symlink(src->narrow, dst->narrow);
10657
0
    Py_END_ALLOW_THREADS
10658
10659
0
#ifdef HAVE_SYMLINKAT
10660
0
    if (symlinkat_unavailable) {
10661
0
          argument_unavailable_error(NULL, "dir_fd");
10662
0
          return NULL;
10663
0
    }
10664
0
#endif
10665
10666
0
    if (result)
10667
0
        return path_error2(src, dst);
10668
0
#endif
10669
10670
0
    Py_RETURN_NONE;
10671
0
}
10672
#endif /* HAVE_SYMLINK */
10673
10674
10675
static PyStructSequence_Field times_result_fields[] = {
10676
    {"user",    "user time"},
10677
    {"system",   "system time"},
10678
    {"children_user",    "user time of children"},
10679
    {"children_system",    "system time of children"},
10680
    {"elapsed",    "elapsed time since an arbitrary point in the past"},
10681
    {NULL}
10682
};
10683
10684
PyDoc_STRVAR(times_result__doc__,
10685
"times_result: Result from os.times().\n\n\
10686
This object may be accessed either as a tuple of\n\
10687
  (user, system, children_user, children_system, elapsed),\n\
10688
or via the attributes user, system, children_user, children_system,\n\
10689
and elapsed.\n\
10690
\n\
10691
See os.times for more information.");
10692
10693
static PyStructSequence_Desc times_result_desc = {
10694
    "times_result", /* name */
10695
    times_result__doc__, /* doc */
10696
    times_result_fields,
10697
    5
10698
};
10699
10700
static PyObject *
10701
build_times_result(PyObject *module, double user, double system,
10702
    double children_user, double children_system,
10703
    double elapsed)
10704
0
{
10705
0
    PyObject *TimesResultType = get_posix_state(module)->TimesResultType;
10706
0
    PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
10707
0
    if (value == NULL)
10708
0
        return NULL;
10709
10710
0
#define SET(i, field) \
10711
0
    { \
10712
0
    PyObject *o = PyFloat_FromDouble(field); \
10713
0
    if (!o) { \
10714
0
        Py_DECREF(value); \
10715
0
        return NULL; \
10716
0
    } \
10717
0
    PyStructSequence_SET_ITEM(value, i, o); \
10718
0
    } \
10719
0
10720
0
    SET(0, user);
10721
0
    SET(1, system);
10722
0
    SET(2, children_user);
10723
0
    SET(3, children_system);
10724
0
    SET(4, elapsed);
10725
10726
0
#undef SET
10727
10728
0
    return value;
10729
0
}
10730
10731
10732
/*[clinic input]
10733
os.times
10734
10735
Return a collection containing process timing information.
10736
10737
The object returned behaves like a named tuple with these fields:
10738
  (utime, stime, cutime, cstime, elapsed_time)
10739
All fields are floating-point numbers.
10740
[clinic start generated code]*/
10741
10742
static PyObject *
10743
os_times_impl(PyObject *module)
10744
/*[clinic end generated code: output=35f640503557d32a input=8dbfe33a2dcc3df3]*/
10745
0
{
10746
#ifdef MS_WINDOWS
10747
    FILETIME create, exit, kernel, user;
10748
    HANDLE hProc;
10749
    hProc = GetCurrentProcess();
10750
    GetProcessTimes(hProc, &create, &exit, &kernel, &user);
10751
    /* The fields of a FILETIME structure are the hi and lo part
10752
       of a 64-bit value expressed in 100 nanosecond units.
10753
       1e7 is one second in such units; 1e-7 the inverse.
10754
       429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
10755
    */
10756
    return build_times_result(module,
10757
        (double)(user.dwHighDateTime*429.4967296 +
10758
                 user.dwLowDateTime*1e-7),
10759
        (double)(kernel.dwHighDateTime*429.4967296 +
10760
                 kernel.dwLowDateTime*1e-7),
10761
        (double)0,
10762
        (double)0,
10763
        (double)0);
10764
#else /* MS_WINDOWS */
10765
0
    _posixstate *state = get_posix_state(module);
10766
0
    long ticks_per_second = state->ticks_per_second;
10767
10768
0
    struct tms process;
10769
0
    clock_t elapsed;
10770
0
    errno = 0;
10771
0
    elapsed = times(&process);
10772
0
    if (elapsed == (clock_t) -1) {
10773
0
        return posix_error();
10774
0
    }
10775
10776
0
    return build_times_result(module,
10777
0
        (double)process.tms_utime / ticks_per_second,
10778
0
        (double)process.tms_stime / ticks_per_second,
10779
0
        (double)process.tms_cutime / ticks_per_second,
10780
0
        (double)process.tms_cstime / ticks_per_second,
10781
0
        (double)elapsed / ticks_per_second);
10782
0
#endif /* MS_WINDOWS */
10783
0
}
10784
10785
10786
#if defined(HAVE_TIMERFD_CREATE)
10787
#define ONE_SECOND_IN_NS (1000 * 1000 * 1000)
10788
#define EXTRACT_NSEC(value)  (long)( ( (double)(value) - (time_t)(value) ) * 1e9)
10789
0
#define CONVERT_SEC_AND_NSEC_TO_DOUBLE(sec, nsec) ( (double)(sec) + (double)(nsec) * 1e-9 )
10790
10791
static PyObject *
10792
build_itimerspec(const struct itimerspec* curr_value)
10793
0
{
10794
0
    double _value = CONVERT_SEC_AND_NSEC_TO_DOUBLE(curr_value->it_value.tv_sec,
10795
0
                                                          curr_value->it_value.tv_nsec);
10796
0
    PyObject *value = PyFloat_FromDouble(_value);
10797
0
    if (value == NULL) {
10798
0
        return NULL;
10799
0
    }
10800
0
    double _interval = CONVERT_SEC_AND_NSEC_TO_DOUBLE(curr_value->it_interval.tv_sec,
10801
0
                                                   curr_value->it_interval.tv_nsec);
10802
0
    PyObject *interval = PyFloat_FromDouble(_interval);
10803
0
    if (interval == NULL) {
10804
0
        Py_DECREF(value);
10805
0
        return NULL;
10806
0
    }
10807
0
    PyObject *tuple = PyTuple_Pack(2, value, interval);
10808
0
    Py_DECREF(interval);
10809
0
    Py_DECREF(value);
10810
0
    return tuple;
10811
0
}
10812
10813
static PyObject *
10814
build_itimerspec_ns(const struct itimerspec* curr_value)
10815
0
{
10816
0
    PyTime_t value, interval;
10817
0
    if (_PyTime_FromTimespec(&value, &curr_value->it_value) < 0) {
10818
0
        return NULL;
10819
0
    }
10820
0
    if (_PyTime_FromTimespec(&interval, &curr_value->it_interval) < 0) {
10821
0
        return NULL;
10822
0
    }
10823
0
    return Py_BuildValue("LL", value, interval);
10824
0
}
10825
10826
/*[clinic input]
10827
os.timerfd_create
10828
10829
    clockid: int
10830
        A valid clock ID constant as timer file descriptor.
10831
10832
        time.CLOCK_REALTIME
10833
        time.CLOCK_MONOTONIC
10834
        time.CLOCK_BOOTTIME
10835
    /
10836
    *
10837
    flags: int = 0
10838
        0 or a bit mask of os.TFD_NONBLOCK or os.TFD_CLOEXEC.
10839
10840
        os.TFD_NONBLOCK
10841
            If *TFD_NONBLOCK* is set as a flag, read doesn't blocks.
10842
            If *TFD_NONBLOCK* is not set as a flag, read block until the timer fires.
10843
10844
        os.TFD_CLOEXEC
10845
            If *TFD_CLOEXEC* is set as a flag, enable the close-on-exec flag
10846
10847
Create and return a timer file descriptor.
10848
[clinic start generated code]*/
10849
10850
static PyObject *
10851
os_timerfd_create_impl(PyObject *module, int clockid, int flags)
10852
/*[clinic end generated code: output=1caae80fb168004a input=64b7020c5ac0b8f4]*/
10853
10854
0
{
10855
0
    int fd;
10856
0
    Py_BEGIN_ALLOW_THREADS
10857
0
    flags |= TFD_CLOEXEC;  // PEP 446: always create non-inheritable FD
10858
0
    fd = timerfd_create(clockid, flags);
10859
0
    Py_END_ALLOW_THREADS
10860
0
    if (fd == -1) {
10861
0
        return PyErr_SetFromErrno(PyExc_OSError);
10862
0
    }
10863
0
    return PyLong_FromLong(fd);
10864
0
}
10865
10866
/*[clinic input]
10867
os.timerfd_settime
10868
10869
    fd: fildes
10870
        A timer file descriptor.
10871
    /
10872
    *
10873
    flags: int = 0
10874
        0 or a bit mask of TFD_TIMER_ABSTIME or TFD_TIMER_CANCEL_ON_SET.
10875
    initial as initial_double: double = 0.0
10876
        The initial expiration time, in seconds.
10877
    interval as interval_double: double = 0.0
10878
        The timer's interval, in seconds.
10879
10880
Alter a timer file descriptor's internal timer in seconds.
10881
[clinic start generated code]*/
10882
10883
static PyObject *
10884
os_timerfd_settime_impl(PyObject *module, int fd, int flags,
10885
                        double initial_double, double interval_double)
10886
/*[clinic end generated code: output=df4c1bce6859224e input=81d2c0d7e936e8a7]*/
10887
0
{
10888
0
    PyTime_t initial, interval;
10889
0
    if (_PyTime_FromSecondsDouble(initial_double, _PyTime_ROUND_FLOOR,
10890
0
                                  &initial) < 0) {
10891
0
        return NULL;
10892
0
    }
10893
0
    if (_PyTime_FromSecondsDouble(interval_double, _PyTime_ROUND_FLOOR,
10894
0
                                  &interval) < 0) {
10895
0
        return NULL;
10896
0
    }
10897
10898
0
    struct itimerspec new_value, old_value;
10899
0
    if (_PyTime_AsTimespec(initial, &new_value.it_value) < 0) {
10900
0
        PyErr_SetString(PyExc_ValueError, "invalid initial value");
10901
0
        return NULL;
10902
0
    }
10903
0
    if (_PyTime_AsTimespec(interval, &new_value.it_interval) < 0) {
10904
0
        PyErr_SetString(PyExc_ValueError, "invalid interval value");
10905
0
        return NULL;
10906
0
    }
10907
10908
0
    int result;
10909
0
    Py_BEGIN_ALLOW_THREADS
10910
0
    result = timerfd_settime(fd, flags, &new_value, &old_value);
10911
0
    Py_END_ALLOW_THREADS
10912
0
    if (result == -1) {
10913
0
        return PyErr_SetFromErrno(PyExc_OSError);
10914
0
    }
10915
0
    return build_itimerspec(&old_value);
10916
0
}
10917
10918
10919
/*[clinic input]
10920
os.timerfd_settime_ns
10921
10922
    fd: fildes
10923
        A timer file descriptor.
10924
    /
10925
    *
10926
    flags: int = 0
10927
        0 or a bit mask of TFD_TIMER_ABSTIME or TFD_TIMER_CANCEL_ON_SET.
10928
    initial: long_long = 0
10929
        initial expiration timing in seconds.
10930
    interval: long_long = 0
10931
        interval for the timer in seconds.
10932
10933
Alter a timer file descriptor's internal timer in nanoseconds.
10934
[clinic start generated code]*/
10935
10936
static PyObject *
10937
os_timerfd_settime_ns_impl(PyObject *module, int fd, int flags,
10938
                           long long initial, long long interval)
10939
/*[clinic end generated code: output=6273ec7d7b4cc0b3 input=261e105d6e42f5bc]*/
10940
0
{
10941
0
    struct itimerspec new_value;
10942
0
    struct itimerspec old_value;
10943
0
    int result;
10944
0
    if (_PyTime_AsTimespec(initial, &new_value.it_value) < 0) {
10945
0
        PyErr_SetString(PyExc_ValueError, "invalid initial value");
10946
0
        return NULL;
10947
0
    }
10948
0
    if (_PyTime_AsTimespec(interval, &new_value.it_interval) < 0) {
10949
0
        PyErr_SetString(PyExc_ValueError, "invalid interval value");
10950
0
        return NULL;
10951
0
    }
10952
0
    Py_BEGIN_ALLOW_THREADS
10953
0
    result = timerfd_settime(fd, flags, &new_value, &old_value);
10954
0
    Py_END_ALLOW_THREADS
10955
0
    if (result == -1) {
10956
0
        return PyErr_SetFromErrno(PyExc_OSError);
10957
0
    }
10958
0
    return build_itimerspec_ns(&old_value);
10959
0
}
10960
10961
/*[clinic input]
10962
@permit_long_summary
10963
os.timerfd_gettime
10964
10965
    fd: fildes
10966
        A timer file descriptor.
10967
    /
10968
10969
Return a tuple of a timer file descriptor's (interval, next expiration) in float seconds.
10970
[clinic start generated code]*/
10971
10972
static PyObject *
10973
os_timerfd_gettime_impl(PyObject *module, int fd)
10974
/*[clinic end generated code: output=ec5a94a66cfe6ab4 input=05f7d568a4820dc6]*/
10975
0
{
10976
0
    struct itimerspec curr_value;
10977
0
    int result;
10978
0
    Py_BEGIN_ALLOW_THREADS
10979
0
    result = timerfd_gettime(fd, &curr_value);
10980
0
    Py_END_ALLOW_THREADS
10981
0
    if (result == -1) {
10982
0
        return PyErr_SetFromErrno(PyExc_OSError);
10983
0
    }
10984
0
    return build_itimerspec(&curr_value);
10985
0
}
10986
10987
10988
/*[clinic input]
10989
@permit_long_summary
10990
os.timerfd_gettime_ns
10991
10992
    fd: fildes
10993
        A timer file descriptor.
10994
    /
10995
10996
Return a tuple of a timer file descriptor's (interval, next expiration) in nanoseconds.
10997
[clinic start generated code]*/
10998
10999
static PyObject *
11000
os_timerfd_gettime_ns_impl(PyObject *module, int fd)
11001
/*[clinic end generated code: output=580633a4465f39fe input=d0de95b9782179c5]*/
11002
0
{
11003
0
    struct itimerspec curr_value;
11004
0
    int result;
11005
0
    Py_BEGIN_ALLOW_THREADS
11006
0
    result = timerfd_gettime(fd, &curr_value);
11007
0
    Py_END_ALLOW_THREADS
11008
0
    if (result == -1) {
11009
0
        return PyErr_SetFromErrno(PyExc_OSError);
11010
0
    }
11011
0
    return build_itimerspec_ns(&curr_value);
11012
0
}
11013
11014
#undef ONE_SECOND_IN_NS
11015
#undef EXTRACT_NSEC
11016
11017
#endif  /* HAVE_TIMERFD_CREATE */
11018
11019
#ifdef HAVE_GETSID
11020
/*[clinic input]
11021
os.getsid
11022
11023
    pid: pid_t
11024
    /
11025
11026
Call the system call getsid(pid) and return the result.
11027
[clinic start generated code]*/
11028
11029
static PyObject *
11030
os_getsid_impl(PyObject *module, pid_t pid)
11031
/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
11032
0
{
11033
0
    int sid;
11034
0
    sid = getsid(pid);
11035
0
    if (sid < 0)
11036
0
        return posix_error();
11037
0
    return PyLong_FromLong((long)sid);
11038
0
}
11039
#endif /* HAVE_GETSID */
11040
11041
11042
#ifdef HAVE_SETSID
11043
/*[clinic input]
11044
os.setsid
11045
11046
Call the system call setsid().
11047
[clinic start generated code]*/
11048
11049
static PyObject *
11050
os_setsid_impl(PyObject *module)
11051
/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
11052
0
{
11053
0
    if (setsid() < 0)
11054
0
        return posix_error();
11055
0
    Py_RETURN_NONE;
11056
0
}
11057
#endif /* HAVE_SETSID */
11058
11059
11060
#ifdef HAVE_SETPGID
11061
/*[clinic input]
11062
os.setpgid
11063
11064
    pid: pid_t
11065
    pgrp: pid_t
11066
    /
11067
11068
Call the system call setpgid(pid, pgrp).
11069
[clinic start generated code]*/
11070
11071
static PyObject *
11072
os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
11073
/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
11074
0
{
11075
0
    if (setpgid(pid, pgrp) < 0)
11076
0
        return posix_error();
11077
0
    Py_RETURN_NONE;
11078
0
}
11079
#endif /* HAVE_SETPGID */
11080
11081
11082
#ifdef HAVE_TCGETPGRP
11083
/*[clinic input]
11084
os.tcgetpgrp
11085
11086
    fd: int
11087
    /
11088
11089
Return the process group associated with the terminal specified by fd.
11090
[clinic start generated code]*/
11091
11092
static PyObject *
11093
os_tcgetpgrp_impl(PyObject *module, int fd)
11094
/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
11095
0
{
11096
0
    pid_t pgid = tcgetpgrp(fd);
11097
0
    if (pgid < 0)
11098
0
        return posix_error();
11099
0
    return PyLong_FromPid(pgid);
11100
0
}
11101
#endif /* HAVE_TCGETPGRP */
11102
11103
11104
#ifdef HAVE_TCSETPGRP
11105
/*[clinic input]
11106
os.tcsetpgrp
11107
11108
    fd: int
11109
    pgid: pid_t
11110
    /
11111
11112
Set the process group associated with the terminal specified by fd.
11113
[clinic start generated code]*/
11114
11115
static PyObject *
11116
os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
11117
/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
11118
0
{
11119
0
    if (tcsetpgrp(fd, pgid) < 0)
11120
0
        return posix_error();
11121
0
    Py_RETURN_NONE;
11122
0
}
11123
#endif /* HAVE_TCSETPGRP */
11124
11125
/* Functions acting on file descriptors */
11126
11127
#ifdef O_CLOEXEC
11128
extern int _Py_open_cloexec_works;
11129
#endif
11130
11131
11132
/*[clinic input]
11133
os.open -> int
11134
    path: path_t
11135
    flags: int
11136
    mode: int = 0o777
11137
    *
11138
    dir_fd: dir_fd(requires='openat') = None
11139
11140
# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
11141
11142
Open a file for low level IO.  Returns a file descriptor (integer).
11143
11144
If dir_fd is not None, it should be a file descriptor open to a directory,
11145
  and path should be relative; path will then be relative to that directory.
11146
dir_fd may not be implemented on your platform.
11147
  If it is unavailable, using it will raise a NotImplementedError.
11148
[clinic start generated code]*/
11149
11150
static int
11151
os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
11152
/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
11153
236
{
11154
236
    int fd;
11155
236
    int async_err = 0;
11156
236
#ifdef HAVE_OPENAT
11157
236
    int openat_unavailable = 0;
11158
236
#endif
11159
11160
236
#ifdef O_CLOEXEC
11161
236
    int *atomic_flag_works = &_Py_open_cloexec_works;
11162
#elif !defined(MS_WINDOWS)
11163
    int *atomic_flag_works = NULL;
11164
#endif
11165
11166
#ifdef MS_WINDOWS
11167
    flags |= O_NOINHERIT;
11168
#elif defined(O_CLOEXEC)
11169
    flags |= O_CLOEXEC;
11170
236
#endif
11171
11172
236
    if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
11173
0
        return -1;
11174
0
    }
11175
11176
236
    _Py_BEGIN_SUPPRESS_IPH
11177
236
    do {
11178
236
        Py_BEGIN_ALLOW_THREADS
11179
#ifdef MS_WINDOWS
11180
        fd = _wopen(path->wide, flags, mode);
11181
#else
11182
236
#ifdef HAVE_OPENAT
11183
236
        if (dir_fd != DEFAULT_DIR_FD) {
11184
0
            if (HAVE_OPENAT_RUNTIME) {
11185
0
                fd = openat(dir_fd, path->narrow, flags, mode);
11186
11187
0
            } else {
11188
0
                openat_unavailable = 1;
11189
0
                fd = -1;
11190
0
            }
11191
0
        } else
11192
236
#endif /* HAVE_OPENAT */
11193
236
            fd = open(path->narrow, flags, mode);
11194
236
#endif /* !MS_WINDOWS */
11195
236
        Py_END_ALLOW_THREADS
11196
236
    } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
11197
236
    _Py_END_SUPPRESS_IPH
11198
11199
236
#ifdef HAVE_OPENAT
11200
236
    if (openat_unavailable) {
11201
0
        argument_unavailable_error(NULL, "dir_fd");
11202
0
        return -1;
11203
0
    }
11204
236
#endif
11205
11206
236
    if (fd < 0) {
11207
0
        if (!async_err)
11208
0
            PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
11209
0
        return -1;
11210
0
    }
11211
11212
236
#ifndef MS_WINDOWS
11213
236
    if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
11214
0
        close(fd);
11215
0
        return -1;
11216
0
    }
11217
236
#endif
11218
11219
236
    return fd;
11220
236
}
11221
11222
11223
/*[clinic input]
11224
os.close
11225
11226
    fd: int
11227
11228
Close a file descriptor.
11229
[clinic start generated code]*/
11230
11231
static PyObject *
11232
os_close_impl(PyObject *module, int fd)
11233
/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
11234
0
{
11235
0
    int res;
11236
    /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
11237
     * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
11238
     * for more details.
11239
     */
11240
0
    Py_BEGIN_ALLOW_THREADS
11241
0
    _Py_BEGIN_SUPPRESS_IPH
11242
0
    res = close(fd);
11243
0
    _Py_END_SUPPRESS_IPH
11244
0
    Py_END_ALLOW_THREADS
11245
0
    if (res < 0)
11246
0
        return posix_error();
11247
0
    Py_RETURN_NONE;
11248
0
}
11249
11250
/*[clinic input]
11251
os.closerange
11252
11253
    fd_low: int
11254
    fd_high: int
11255
    /
11256
11257
Closes all file descriptors in [fd_low, fd_high), ignoring errors.
11258
[clinic start generated code]*/
11259
11260
static PyObject *
11261
os_closerange_impl(PyObject *module, int fd_low, int fd_high)
11262
/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
11263
0
{
11264
0
    Py_BEGIN_ALLOW_THREADS
11265
0
    _Py_closerange(fd_low, fd_high - 1);
11266
0
    Py_END_ALLOW_THREADS
11267
0
    Py_RETURN_NONE;
11268
0
}
11269
11270
11271
/*[clinic input]
11272
os.dup -> int
11273
11274
    fd: int
11275
    /
11276
11277
Return a duplicate of a file descriptor.
11278
[clinic start generated code]*/
11279
11280
static int
11281
os_dup_impl(PyObject *module, int fd)
11282
/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
11283
0
{
11284
0
    return _Py_dup(fd);
11285
0
}
11286
11287
// dup2() is either provided by libc or dup2.c with AC_REPLACE_FUNCS().
11288
// dup2.c provides working dup2() if and only if F_DUPFD is available.
11289
#if (defined(HAVE_DUP3) || defined(F_DUPFD) || defined(MS_WINDOWS))
11290
/*[clinic input]
11291
os.dup2 -> int
11292
    fd: int
11293
    fd2: int
11294
    inheritable: bool=True
11295
11296
Duplicate file descriptor.
11297
[clinic start generated code]*/
11298
11299
static int
11300
os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
11301
/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
11302
0
{
11303
0
    int res = 0;
11304
0
#if defined(HAVE_DUP3) && \
11305
0
    !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
11306
    /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
11307
0
    static int dup3_works = -1;
11308
0
#endif
11309
11310
    /* dup2() can fail with EINTR if the target FD is already open, because it
11311
     * then has to be closed. See os_close_impl() for why we don't handle EINTR
11312
     * upon close(), and therefore below.
11313
     */
11314
#ifdef MS_WINDOWS
11315
    Py_BEGIN_ALLOW_THREADS
11316
    _Py_BEGIN_SUPPRESS_IPH
11317
    res = dup2(fd, fd2);
11318
    _Py_END_SUPPRESS_IPH
11319
    Py_END_ALLOW_THREADS
11320
    if (res < 0) {
11321
        posix_error();
11322
        return -1;
11323
    }
11324
    res = fd2; // msvcrt dup2 returns 0 on success.
11325
11326
    /* Character files like console cannot be make non-inheritable */
11327
    if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
11328
        close(fd2);
11329
        return -1;
11330
    }
11331
11332
#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
11333
    Py_BEGIN_ALLOW_THREADS
11334
    if (!inheritable)
11335
        res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
11336
    else
11337
        res = dup2(fd, fd2);
11338
    Py_END_ALLOW_THREADS
11339
    if (res < 0) {
11340
        posix_error();
11341
        return -1;
11342
    }
11343
11344
#else
11345
11346
0
#ifdef HAVE_DUP3
11347
0
    if (!inheritable && dup3_works != 0) {
11348
0
        Py_BEGIN_ALLOW_THREADS
11349
0
        res = dup3(fd, fd2, O_CLOEXEC);
11350
0
        Py_END_ALLOW_THREADS
11351
0
        if (res < 0) {
11352
0
            if (dup3_works == -1)
11353
0
                dup3_works = (errno != ENOSYS);
11354
0
            if (dup3_works) {
11355
0
                posix_error();
11356
0
                return -1;
11357
0
            }
11358
0
        }
11359
0
    }
11360
11361
0
    if (inheritable || dup3_works == 0)
11362
0
    {
11363
0
#endif
11364
0
        Py_BEGIN_ALLOW_THREADS
11365
0
        res = dup2(fd, fd2);
11366
0
        Py_END_ALLOW_THREADS
11367
0
        if (res < 0) {
11368
0
            posix_error();
11369
0
            return -1;
11370
0
        }
11371
11372
0
        if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
11373
0
            close(fd2);
11374
0
            return -1;
11375
0
        }
11376
0
#ifdef HAVE_DUP3
11377
0
    }
11378
0
#endif
11379
11380
0
#endif
11381
11382
0
    return res;
11383
0
}
11384
#endif
11385
11386
11387
#ifdef HAVE_LOCKF
11388
/*[clinic input]
11389
os.lockf
11390
11391
    fd: int
11392
        An open file descriptor.
11393
    command: int
11394
        One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
11395
    length: Py_off_t
11396
        The number of bytes to lock, starting at the current position.
11397
    /
11398
11399
Apply, test or remove a POSIX lock on an open file descriptor.
11400
11401
[clinic start generated code]*/
11402
11403
static PyObject *
11404
os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
11405
/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
11406
0
{
11407
0
    int res;
11408
11409
0
    if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
11410
0
        return NULL;
11411
0
    }
11412
11413
0
    Py_BEGIN_ALLOW_THREADS
11414
0
    res = lockf(fd, command, length);
11415
0
    Py_END_ALLOW_THREADS
11416
11417
0
    if (res < 0)
11418
0
        return posix_error();
11419
11420
0
    Py_RETURN_NONE;
11421
0
}
11422
#endif /* HAVE_LOCKF */
11423
11424
11425
/*[clinic input]
11426
@permit_long_docstring_body
11427
os.lseek -> Py_off_t
11428
11429
    fd: int
11430
        An open file descriptor, as returned by os.open().
11431
    position: Py_off_t
11432
        Position, interpreted relative to 'whence'.
11433
    whence as how: int
11434
        The relative position to seek from. Valid values are:
11435
        - SEEK_SET: seek from the start of the file.
11436
        - SEEK_CUR: seek from the current file position.
11437
        - SEEK_END: seek from the end of the file.
11438
    /
11439
11440
Set the position of a file descriptor.  Return the new position.
11441
11442
The return value is the number of bytes relative to the beginning of the file.
11443
[clinic start generated code]*/
11444
11445
static Py_off_t
11446
os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
11447
/*[clinic end generated code: output=971e1efb6b30bd2f input=4a3de549f07e1c40]*/
11448
0
{
11449
0
    Py_off_t result;
11450
11451
0
#ifdef SEEK_SET
11452
    /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
11453
0
    switch (how) {
11454
0
        case 0: how = SEEK_SET; break;
11455
0
        case 1: how = SEEK_CUR; break;
11456
0
        case 2: how = SEEK_END; break;
11457
0
    }
11458
0
#endif /* SEEK_END */
11459
11460
0
    Py_BEGIN_ALLOW_THREADS
11461
0
    _Py_BEGIN_SUPPRESS_IPH
11462
#ifdef MS_WINDOWS
11463
    result = _lseeki64(fd, position, how);
11464
#else
11465
0
    result = lseek(fd, position, how);
11466
0
#endif
11467
0
    _Py_END_SUPPRESS_IPH
11468
0
    Py_END_ALLOW_THREADS
11469
0
    if (result < 0)
11470
0
        posix_error();
11471
11472
0
    return result;
11473
0
}
11474
11475
11476
/*[clinic input]
11477
os.read
11478
    fd: int
11479
    length: Py_ssize_t
11480
    /
11481
11482
Read from a file descriptor.  Returns a bytes object.
11483
[clinic start generated code]*/
11484
11485
static PyObject *
11486
os_read_impl(PyObject *module, int fd, Py_ssize_t length)
11487
/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
11488
0
{
11489
0
    Py_ssize_t n;
11490
0
    PyObject *buffer;
11491
11492
0
    if (length < 0) {
11493
0
        errno = EINVAL;
11494
0
        return posix_error();
11495
0
    }
11496
11497
0
    length = Py_MIN(length, _PY_READ_MAX);
11498
11499
0
    buffer = PyBytes_FromStringAndSize((char *)NULL, length);
11500
0
    if (buffer == NULL)
11501
0
        return NULL;
11502
11503
0
    n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
11504
0
    if (n == -1) {
11505
0
        Py_DECREF(buffer);
11506
0
        return NULL;
11507
0
    }
11508
11509
0
    if (n != length)
11510
0
        _PyBytes_Resize(&buffer, n);
11511
11512
0
    return buffer;
11513
0
}
11514
11515
/*[clinic input]
11516
@permit_long_docstring_body
11517
os.readinto -> Py_ssize_t
11518
    fd: int
11519
    buffer: Py_buffer(accept={rwbuffer})
11520
    /
11521
11522
Read into a buffer object from a file descriptor.
11523
11524
The buffer should be mutable and bytes-like. On success, returns the number of
11525
bytes read. Less bytes may be read than the size of the buffer. The underlying
11526
system call will be retried when interrupted by a signal, unless the signal
11527
handler raises an exception. Other errors will not be retried and an error will
11528
be raised.
11529
11530
Returns 0 if *fd* is at end of file or if the provided *buffer* has length 0
11531
(which can be used to check for errors without reading data). Never returns
11532
negative.
11533
[clinic start generated code]*/
11534
11535
static Py_ssize_t
11536
os_readinto_impl(PyObject *module, int fd, Py_buffer *buffer)
11537
/*[clinic end generated code: output=8091a3513c683a80 input=a770382bd3d32f9a]*/
11538
0
{
11539
0
    assert(buffer->len >= 0);
11540
0
    Py_ssize_t result = _Py_read(fd, buffer->buf, buffer->len);
11541
    /* Ensure negative is never returned without an error. Simplifies calling
11542
        code. _Py_read should succeed, possibly reading 0 bytes, _or_ set an
11543
        error. */
11544
0
    assert(result >= 0 || (result == -1 && PyErr_Occurred()));
11545
0
    return result;
11546
0
}
11547
11548
#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
11549
                                || defined(__APPLE__))) \
11550
    || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
11551
    || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
11552
static int
11553
iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
11554
0
{
11555
0
    Py_ssize_t i, j;
11556
11557
0
    *iov = PyMem_New(struct iovec, cnt);
11558
0
    if (*iov == NULL) {
11559
0
        PyErr_NoMemory();
11560
0
        return -1;
11561
0
    }
11562
11563
0
    *buf = PyMem_New(Py_buffer, cnt);
11564
0
    if (*buf == NULL) {
11565
0
        PyMem_Free(*iov);
11566
0
        PyErr_NoMemory();
11567
0
        return -1;
11568
0
    }
11569
11570
0
    for (i = 0; i < cnt; i++) {
11571
0
        PyObject *item = PySequence_GetItem(seq, i);
11572
0
        if (item == NULL)
11573
0
            goto fail;
11574
0
        if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
11575
0
            Py_DECREF(item);
11576
0
            goto fail;
11577
0
        }
11578
0
        Py_DECREF(item);
11579
0
        (*iov)[i].iov_base = (*buf)[i].buf;
11580
0
        (*iov)[i].iov_len = (*buf)[i].len;
11581
0
    }
11582
0
    return 0;
11583
11584
0
fail:
11585
0
    PyMem_Free(*iov);
11586
0
    for (j = 0; j < i; j++) {
11587
0
        PyBuffer_Release(&(*buf)[j]);
11588
0
    }
11589
0
    PyMem_Free(*buf);
11590
0
    return -1;
11591
0
}
11592
11593
static void
11594
iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
11595
0
{
11596
0
    int i;
11597
0
    PyMem_Free(iov);
11598
0
    for (i = 0; i < cnt; i++) {
11599
0
        PyBuffer_Release(&buf[i]);
11600
0
    }
11601
0
    PyMem_Free(buf);
11602
0
}
11603
#endif
11604
11605
11606
#ifdef HAVE_READV
11607
/*[clinic input]
11608
os.readv -> Py_ssize_t
11609
11610
    fd: int
11611
    buffers: object
11612
    /
11613
11614
Read from a file descriptor fd into an iterable of buffers.
11615
11616
The buffers should be mutable buffers accepting bytes.
11617
readv will transfer data into each buffer until it is full
11618
and then move on to the next buffer in the sequence to hold
11619
the rest of the data.
11620
11621
readv returns the total number of bytes read,
11622
which may be less than the total capacity of all the buffers.
11623
[clinic start generated code]*/
11624
11625
static Py_ssize_t
11626
os_readv_impl(PyObject *module, int fd, PyObject *buffers)
11627
/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
11628
0
{
11629
0
    Py_ssize_t cnt, n;
11630
0
    int async_err = 0;
11631
0
    struct iovec *iov;
11632
0
    Py_buffer *buf;
11633
11634
0
    if (!PySequence_Check(buffers)) {
11635
0
        PyErr_SetString(PyExc_TypeError,
11636
0
            "readv() arg 2 must be a sequence");
11637
0
        return -1;
11638
0
    }
11639
11640
0
    cnt = PySequence_Size(buffers);
11641
0
    if (cnt < 0)
11642
0
        return -1;
11643
11644
0
    if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
11645
0
        return -1;
11646
11647
0
    do {
11648
0
        Py_BEGIN_ALLOW_THREADS
11649
0
        n = readv(fd, iov, cnt);
11650
0
        Py_END_ALLOW_THREADS
11651
0
    } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
11652
11653
0
    int saved_errno = errno;
11654
0
    iov_cleanup(iov, buf, cnt);
11655
0
    if (n < 0) {
11656
0
        if (!async_err) {
11657
0
            errno = saved_errno;
11658
0
            posix_error();
11659
0
        }
11660
0
        return -1;
11661
0
    }
11662
11663
0
    return n;
11664
0
}
11665
#endif /* HAVE_READV */
11666
11667
11668
#ifdef HAVE_PREAD
11669
/*[clinic input]
11670
@permit_long_summary
11671
os.pread
11672
11673
    fd: int
11674
    length: Py_ssize_t
11675
    offset: Py_off_t
11676
    /
11677
11678
Read a number of bytes from a file descriptor starting at a particular offset.
11679
11680
Read length bytes from file descriptor fd, starting at offset bytes from
11681
the beginning of the file.  The file offset remains unchanged.
11682
[clinic start generated code]*/
11683
11684
static PyObject *
11685
os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
11686
/*[clinic end generated code: output=3f875c1eef82e32f input=5943beb009d3da04]*/
11687
0
{
11688
0
    Py_ssize_t n;
11689
0
    int async_err = 0;
11690
0
    PyObject *buffer;
11691
11692
0
    if (length < 0) {
11693
0
        errno = EINVAL;
11694
0
        return posix_error();
11695
0
    }
11696
0
    buffer = PyBytes_FromStringAndSize((char *)NULL, length);
11697
0
    if (buffer == NULL)
11698
0
        return NULL;
11699
11700
0
    do {
11701
0
        Py_BEGIN_ALLOW_THREADS
11702
0
        _Py_BEGIN_SUPPRESS_IPH
11703
0
        n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
11704
0
        _Py_END_SUPPRESS_IPH
11705
0
        Py_END_ALLOW_THREADS
11706
0
    } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
11707
11708
0
    if (n < 0) {
11709
0
        if (!async_err) {
11710
0
            posix_error();
11711
0
        }
11712
0
        Py_DECREF(buffer);
11713
0
        return NULL;
11714
0
    }
11715
0
    if (n != length)
11716
0
        _PyBytes_Resize(&buffer, n);
11717
0
    return buffer;
11718
0
}
11719
#endif /* HAVE_PREAD */
11720
11721
#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
11722
/*[clinic input]
11723
@permit_long_docstring_body
11724
os.preadv -> Py_ssize_t
11725
11726
    fd: int
11727
    buffers: object
11728
    offset: Py_off_t
11729
    flags: int = 0
11730
    /
11731
11732
Reads from a file descriptor into a number of mutable bytes-like objects.
11733
11734
Combines the functionality of readv() and pread(). As readv(), it will
11735
transfer data into each buffer until it is full and then move on to the next
11736
buffer in the sequence to hold the rest of the data. Its fourth argument,
11737
specifies the file offset at which the input operation is to be performed. It
11738
will return the total number of bytes read (which can be less than the total
11739
capacity of all the objects).
11740
11741
The flags argument contains a bitwise OR of zero or more of the following flags:
11742
11743
- RWF_HIPRI
11744
- RWF_NOWAIT
11745
11746
Using non-zero flags requires Linux 4.6 or newer.
11747
[clinic start generated code]*/
11748
11749
static Py_ssize_t
11750
os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
11751
               int flags)
11752
/*[clinic end generated code: output=26fc9c6e58e7ada5 input=c1f876866fcd9d41]*/
11753
0
{
11754
0
    Py_ssize_t cnt, n;
11755
0
    int async_err = 0;
11756
0
    struct iovec *iov;
11757
0
    Py_buffer *buf;
11758
11759
0
    if (!PySequence_Check(buffers)) {
11760
0
        PyErr_SetString(PyExc_TypeError,
11761
0
            "preadv2() arg 2 must be a sequence");
11762
0
        return -1;
11763
0
    }
11764
11765
0
    cnt = PySequence_Size(buffers);
11766
0
    if (cnt < 0) {
11767
0
        return -1;
11768
0
    }
11769
11770
#ifndef HAVE_PREADV2
11771
    if(flags != 0) {
11772
        argument_unavailable_error("preadv2", "flags");
11773
        return -1;
11774
    }
11775
#endif
11776
11777
0
    if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
11778
0
        return -1;
11779
0
    }
11780
0
#ifdef HAVE_PREADV2
11781
0
    do {
11782
0
        Py_BEGIN_ALLOW_THREADS
11783
0
        _Py_BEGIN_SUPPRESS_IPH
11784
0
        n = preadv2(fd, iov, cnt, offset, flags);
11785
0
        _Py_END_SUPPRESS_IPH
11786
0
        Py_END_ALLOW_THREADS
11787
0
    } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
11788
#else
11789
    do {
11790
#if defined(__APPLE__) && defined(__clang__)
11791
/* This entire function will be removed from the module dict when the API
11792
 * is not available.
11793
 */
11794
#pragma clang diagnostic push
11795
#pragma clang diagnostic ignored "-Wunguarded-availability"
11796
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
11797
#endif
11798
        Py_BEGIN_ALLOW_THREADS
11799
        _Py_BEGIN_SUPPRESS_IPH
11800
        n = preadv(fd, iov, cnt, offset);
11801
        _Py_END_SUPPRESS_IPH
11802
        Py_END_ALLOW_THREADS
11803
    } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
11804
11805
#if defined(__APPLE__) && defined(__clang__)
11806
#pragma clang diagnostic pop
11807
#endif
11808
11809
#endif
11810
11811
0
    int saved_errno = errno;
11812
0
    iov_cleanup(iov, buf, cnt);
11813
0
    if (n < 0) {
11814
0
        if (!async_err) {
11815
0
            errno = saved_errno;
11816
0
            posix_error();
11817
0
        }
11818
0
        return -1;
11819
0
    }
11820
11821
0
    return n;
11822
0
}
11823
#endif /* HAVE_PREADV */
11824
11825
11826
/*[clinic input]
11827
os.write -> Py_ssize_t
11828
11829
    fd: int
11830
    data: Py_buffer
11831
    /
11832
11833
Write a bytes object to a file descriptor.
11834
[clinic start generated code]*/
11835
11836
static Py_ssize_t
11837
os_write_impl(PyObject *module, int fd, Py_buffer *data)
11838
/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
11839
0
{
11840
0
    return _Py_write(fd, data->buf, data->len);
11841
0
}
11842
11843
#ifdef HAVE_SENDFILE
11844
#ifdef __APPLE__
11845
/*[clinic input]
11846
os.sendfile
11847
11848
    out_fd: int
11849
    in_fd: int
11850
    offset: Py_off_t
11851
    count as sbytes: Py_off_t
11852
    headers: object(c_default="NULL") = ()
11853
    trailers: object(c_default="NULL") = ()
11854
    flags: int = 0
11855
11856
Copy count bytes from file descriptor in_fd to file descriptor out_fd.
11857
[clinic start generated code]*/
11858
11859
static PyObject *
11860
os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
11861
                 Py_off_t sbytes, PyObject *headers, PyObject *trailers,
11862
                 int flags)
11863
/*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/
11864
#elif defined(__FreeBSD__) || defined(__DragonFly__)
11865
/*[clinic input]
11866
os.sendfile
11867
11868
    out_fd: int
11869
    in_fd: int
11870
    offset: Py_off_t
11871
    count: Py_ssize_t
11872
    headers: object(c_default="NULL") = ()
11873
    trailers: object(c_default="NULL") = ()
11874
    flags: int = 0
11875
11876
Copy count bytes from file descriptor in_fd to file descriptor out_fd.
11877
[clinic start generated code]*/
11878
11879
static PyObject *
11880
os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
11881
                 Py_ssize_t count, PyObject *headers, PyObject *trailers,
11882
                 int flags)
11883
/*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/
11884
#else
11885
/*[clinic input]
11886
os.sendfile
11887
11888
    out_fd: int
11889
    in_fd: int
11890
    offset as offobj: object
11891
    count: Py_ssize_t
11892
11893
Copy count bytes from file descriptor in_fd to file descriptor out_fd.
11894
[clinic start generated code]*/
11895
11896
static PyObject *
11897
os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
11898
                 Py_ssize_t count)
11899
/*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/
11900
#endif
11901
0
{
11902
0
    Py_ssize_t ret;
11903
0
    int async_err = 0;
11904
11905
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
11906
#ifndef __APPLE__
11907
    off_t sbytes;
11908
#endif
11909
    Py_buffer *hbuf, *tbuf;
11910
    struct sf_hdtr sf;
11911
11912
    sf.headers = NULL;
11913
    sf.trailers = NULL;
11914
11915
    if (headers != NULL) {
11916
        if (!PySequence_Check(headers)) {
11917
            PyErr_SetString(PyExc_TypeError,
11918
                "sendfile() headers must be a sequence");
11919
            return NULL;
11920
        } else {
11921
            Py_ssize_t i = PySequence_Size(headers);
11922
            if (i < 0)
11923
                return NULL;
11924
            if (i > INT_MAX) {
11925
                PyErr_SetString(PyExc_OverflowError,
11926
                    "sendfile() header is too large");
11927
                return NULL;
11928
            }
11929
            if (i > 0) {
11930
                sf.hdr_cnt = (int)i;
11931
                if (iov_setup(&(sf.headers), &hbuf,
11932
                              headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
11933
                    return NULL;
11934
#ifdef __APPLE__
11935
                for (i = 0; i < sf.hdr_cnt; i++) {
11936
                    Py_ssize_t blen = sf.headers[i].iov_len;
11937
# define OFF_T_MAX 0x7fffffffffffffff
11938
                    if (sbytes >= OFF_T_MAX - blen) {
11939
                        PyErr_SetString(PyExc_OverflowError,
11940
                            "sendfile() header is too large");
11941
                        return NULL;
11942
                    }
11943
                    sbytes += blen;
11944
                }
11945
#endif
11946
            }
11947
        }
11948
    }
11949
    if (trailers != NULL) {
11950
        if (!PySequence_Check(trailers)) {
11951
            PyErr_SetString(PyExc_TypeError,
11952
                "sendfile() trailers must be a sequence");
11953
            return NULL;
11954
        } else {
11955
            Py_ssize_t i = PySequence_Size(trailers);
11956
            if (i < 0)
11957
                return NULL;
11958
            if (i > INT_MAX) {
11959
                PyErr_SetString(PyExc_OverflowError,
11960
                    "sendfile() trailer is too large");
11961
                return NULL;
11962
            }
11963
            if (i > 0) {
11964
                sf.trl_cnt = (int)i;
11965
                if (iov_setup(&(sf.trailers), &tbuf,
11966
                              trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
11967
                    return NULL;
11968
            }
11969
        }
11970
    }
11971
11972
    _Py_BEGIN_SUPPRESS_IPH
11973
    do {
11974
        Py_BEGIN_ALLOW_THREADS
11975
#ifdef __APPLE__
11976
        ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags);
11977
#else
11978
        ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags);
11979
#endif
11980
        Py_END_ALLOW_THREADS
11981
    } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
11982
    _Py_END_SUPPRESS_IPH
11983
11984
    int saved_errno = errno;
11985
    if (sf.headers != NULL)
11986
        iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
11987
    if (sf.trailers != NULL)
11988
        iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
11989
11990
    if (ret < 0) {
11991
        if ((saved_errno == EAGAIN) || (saved_errno == EBUSY)) {
11992
            if (sbytes != 0) {
11993
                // some data has been sent
11994
                goto done;
11995
            }
11996
            // no data has been sent; upper application is supposed
11997
            // to retry on EAGAIN or EBUSY
11998
        }
11999
        if (!async_err) {
12000
            errno = saved_errno;
12001
            posix_error();
12002
        }
12003
        return NULL;
12004
    }
12005
    goto done;
12006
12007
done:
12008
    #if !defined(HAVE_LARGEFILE_SUPPORT)
12009
        return PyLong_FromLong(sbytes);
12010
    #else
12011
        return PyLong_FromLongLong(sbytes);
12012
    #endif
12013
12014
#else
12015
0
#ifdef __linux__
12016
0
    if (offobj == Py_None) {
12017
0
        do {
12018
0
            Py_BEGIN_ALLOW_THREADS
12019
0
            ret = sendfile(out_fd, in_fd, NULL, count);
12020
0
            Py_END_ALLOW_THREADS
12021
0
        } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12022
0
        if (ret < 0)
12023
0
            return (!async_err) ? posix_error() : NULL;
12024
0
        return PyLong_FromSsize_t(ret);
12025
0
    }
12026
0
#endif
12027
0
    off_t offset;
12028
0
    if (!Py_off_t_converter(offobj, &offset))
12029
0
        return NULL;
12030
12031
#if defined(__sun) && defined(__SVR4)
12032
    // On Solaris, sendfile raises EINVAL rather than returning 0
12033
    // when the offset is equal or bigger than the in_fd size.
12034
    struct stat st;
12035
12036
    do {
12037
        Py_BEGIN_ALLOW_THREADS
12038
        ret = fstat(in_fd, &st);
12039
        Py_END_ALLOW_THREADS
12040
    } while (ret != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12041
    if (ret < 0)
12042
        return (!async_err) ? posix_error() : NULL;
12043
12044
    if (offset >= st.st_size) {
12045
        return PyLong_FromLong(0);
12046
    }
12047
12048
    // On illumos specifically sendfile() may perform a partial write but
12049
    // return -1/an error (in one confirmed case the destination socket
12050
    // had a 5 second timeout set and errno was EAGAIN) and it's on the client
12051
    // code to check if the offset parameter was modified by sendfile().
12052
    //
12053
    // We need this variable to track said change.
12054
    off_t original_offset = offset;
12055
#endif
12056
12057
0
    do {
12058
0
        Py_BEGIN_ALLOW_THREADS
12059
0
        ret = sendfile(out_fd, in_fd, &offset, count);
12060
#if defined(__sun) && defined(__SVR4)
12061
        // This handles illumos-specific sendfile() partial write behavior,
12062
        // see a comment above for more details.
12063
        if (ret < 0 && offset != original_offset) {
12064
            ret = offset - original_offset;
12065
        }
12066
#endif
12067
0
        Py_END_ALLOW_THREADS
12068
0
    } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12069
0
    if (ret < 0)
12070
0
        return (!async_err) ? posix_error() : NULL;
12071
0
    return PyLong_FromSsize_t(ret);
12072
0
#endif
12073
0
}
12074
#endif /* HAVE_SENDFILE */
12075
12076
12077
#if defined(__APPLE__)
12078
/*[clinic input]
12079
os._fcopyfile
12080
12081
    in_fd: int
12082
    out_fd: int
12083
    flags: int
12084
    /
12085
12086
Efficiently copy content or metadata of 2 regular file descriptors (macOS).
12087
[clinic start generated code]*/
12088
12089
static PyObject *
12090
os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
12091
/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
12092
{
12093
    int ret;
12094
12095
    Py_BEGIN_ALLOW_THREADS
12096
    ret = fcopyfile(in_fd, out_fd, NULL, flags);
12097
    Py_END_ALLOW_THREADS
12098
    if (ret < 0)
12099
        return posix_error();
12100
    Py_RETURN_NONE;
12101
}
12102
#endif
12103
12104
12105
/*[clinic input]
12106
os.fstat
12107
12108
    fd : int
12109
12110
Perform a stat system call on the given file descriptor.
12111
12112
Like stat(), but for an open file descriptor.
12113
Equivalent to os.stat(fd).
12114
[clinic start generated code]*/
12115
12116
static PyObject *
12117
os_fstat_impl(PyObject *module, int fd)
12118
/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
12119
0
{
12120
0
    STRUCT_STAT st;
12121
0
    int res;
12122
0
    int async_err = 0;
12123
12124
0
    do {
12125
0
        Py_BEGIN_ALLOW_THREADS
12126
0
        res = FSTAT(fd, &st);
12127
0
        Py_END_ALLOW_THREADS
12128
0
    } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12129
0
    if (res != 0) {
12130
#ifdef MS_WINDOWS
12131
        return PyErr_SetFromWindowsErr(0);
12132
#else
12133
0
        return (!async_err) ? posix_error() : NULL;
12134
0
#endif
12135
0
    }
12136
12137
0
    return _pystat_fromstructstat(module, &st);
12138
0
}
12139
12140
12141
/*[clinic input]
12142
os.isatty -> bool
12143
    fd: int
12144
    /
12145
12146
Return True if the fd is connected to a terminal.
12147
12148
Return True if the file descriptor is an open file descriptor
12149
connected to the slave end of a terminal.
12150
[clinic start generated code]*/
12151
12152
static int
12153
os_isatty_impl(PyObject *module, int fd)
12154
/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
12155
0
{
12156
0
    int return_value;
12157
0
    Py_BEGIN_ALLOW_THREADS
12158
0
    _Py_BEGIN_SUPPRESS_IPH
12159
0
    return_value = isatty(fd);
12160
0
    _Py_END_SUPPRESS_IPH
12161
0
    Py_END_ALLOW_THREADS
12162
0
    return return_value;
12163
0
}
12164
12165
12166
#ifdef HAVE_PIPE
12167
/*[clinic input]
12168
os.pipe
12169
12170
Create a pipe.
12171
12172
Returns a tuple of two file descriptors:
12173
  (read_fd, write_fd)
12174
[clinic start generated code]*/
12175
12176
static PyObject *
12177
os_pipe_impl(PyObject *module)
12178
/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
12179
0
{
12180
0
    int fds[2];
12181
#ifdef MS_WINDOWS
12182
    HANDLE read, write;
12183
    SECURITY_ATTRIBUTES attr;
12184
    BOOL ok;
12185
#else
12186
0
    int res;
12187
0
#endif
12188
12189
#ifdef MS_WINDOWS
12190
    attr.nLength = sizeof(attr);
12191
    attr.lpSecurityDescriptor = NULL;
12192
    attr.bInheritHandle = FALSE;
12193
12194
    Py_BEGIN_ALLOW_THREADS
12195
    ok = CreatePipe(&read, &write, &attr, 0);
12196
    if (ok) {
12197
        fds[0] = _Py_open_osfhandle_noraise(read, _O_RDONLY | _O_NOINHERIT);
12198
        fds[1] = _Py_open_osfhandle_noraise(write, _O_WRONLY | _O_NOINHERIT);
12199
        if (fds[0] == -1 || fds[1] == -1) {
12200
            CloseHandle(read);
12201
            CloseHandle(write);
12202
            ok = 0;
12203
        }
12204
    }
12205
    Py_END_ALLOW_THREADS
12206
12207
    if (!ok)
12208
        return PyErr_SetFromWindowsErr(0);
12209
#else
12210
12211
0
#ifdef HAVE_PIPE2
12212
0
    Py_BEGIN_ALLOW_THREADS
12213
0
    res = pipe2(fds, O_CLOEXEC);
12214
0
    Py_END_ALLOW_THREADS
12215
12216
0
    if (res != 0 && errno == ENOSYS)
12217
0
    {
12218
0
#endif
12219
0
        Py_BEGIN_ALLOW_THREADS
12220
0
        res = pipe(fds);
12221
0
        Py_END_ALLOW_THREADS
12222
12223
0
        if (res == 0) {
12224
0
            if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
12225
0
                close(fds[0]);
12226
0
                close(fds[1]);
12227
0
                return NULL;
12228
0
            }
12229
0
            if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
12230
0
                close(fds[0]);
12231
0
                close(fds[1]);
12232
0
                return NULL;
12233
0
            }
12234
0
        }
12235
0
#ifdef HAVE_PIPE2
12236
0
    }
12237
0
#endif
12238
12239
0
    if (res != 0)
12240
0
        return PyErr_SetFromErrno(PyExc_OSError);
12241
0
#endif /* !MS_WINDOWS */
12242
0
    return Py_BuildValue("(ii)", fds[0], fds[1]);
12243
0
}
12244
#endif  /* HAVE_PIPE */
12245
12246
12247
#ifdef HAVE_PIPE2
12248
/*[clinic input]
12249
os.pipe2
12250
12251
    flags: int
12252
    /
12253
12254
Create a pipe with flags set atomically.
12255
12256
Returns a tuple of two file descriptors:
12257
  (read_fd, write_fd)
12258
12259
flags can be constructed by ORing together one or more of these values:
12260
O_NONBLOCK, O_CLOEXEC.
12261
[clinic start generated code]*/
12262
12263
static PyObject *
12264
os_pipe2_impl(PyObject *module, int flags)
12265
/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
12266
0
{
12267
0
    int fds[2];
12268
0
    int res;
12269
12270
0
    res = pipe2(fds, flags);
12271
0
    if (res != 0)
12272
0
        return posix_error();
12273
0
    return Py_BuildValue("(ii)", fds[0], fds[1]);
12274
0
}
12275
#endif /* HAVE_PIPE2 */
12276
12277
12278
#ifdef HAVE_WRITEV
12279
/*[clinic input]
12280
os.writev -> Py_ssize_t
12281
    fd: int
12282
    buffers: object
12283
    /
12284
12285
Iterate over buffers, and write the contents of each to a file descriptor.
12286
12287
Returns the total number of bytes written.
12288
buffers must be a sequence of bytes-like objects.
12289
[clinic start generated code]*/
12290
12291
static Py_ssize_t
12292
os_writev_impl(PyObject *module, int fd, PyObject *buffers)
12293
/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
12294
0
{
12295
0
    Py_ssize_t cnt;
12296
0
    Py_ssize_t result;
12297
0
    int async_err = 0;
12298
0
    struct iovec *iov;
12299
0
    Py_buffer *buf;
12300
12301
0
    if (!PySequence_Check(buffers)) {
12302
0
        PyErr_SetString(PyExc_TypeError,
12303
0
            "writev() arg 2 must be a sequence");
12304
0
        return -1;
12305
0
    }
12306
0
    cnt = PySequence_Size(buffers);
12307
0
    if (cnt < 0)
12308
0
        return -1;
12309
12310
0
    if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
12311
0
        return -1;
12312
0
    }
12313
12314
0
    do {
12315
0
        Py_BEGIN_ALLOW_THREADS
12316
0
        result = writev(fd, iov, cnt);
12317
0
        Py_END_ALLOW_THREADS
12318
0
    } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12319
12320
0
    if (result < 0 && !async_err)
12321
0
        posix_error();
12322
12323
0
    iov_cleanup(iov, buf, cnt);
12324
0
    return result;
12325
0
}
12326
#endif /* HAVE_WRITEV */
12327
12328
12329
#ifdef HAVE_PWRITE
12330
/*[clinic input]
12331
os.pwrite -> Py_ssize_t
12332
12333
    fd: int
12334
    buffer: Py_buffer
12335
    offset: Py_off_t
12336
    /
12337
12338
Write bytes to a file descriptor starting at a particular offset.
12339
12340
Write buffer to fd, starting at offset bytes from the beginning of
12341
the file.  Returns the number of bytes written.  Does not change the
12342
current file offset.
12343
[clinic start generated code]*/
12344
12345
static Py_ssize_t
12346
os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
12347
/*[clinic end generated code: output=c74da630758ee925 input=614acbc7e5a0339a]*/
12348
0
{
12349
0
    Py_ssize_t size;
12350
0
    int async_err = 0;
12351
12352
0
    do {
12353
0
        Py_BEGIN_ALLOW_THREADS
12354
0
        _Py_BEGIN_SUPPRESS_IPH
12355
0
        size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
12356
0
        _Py_END_SUPPRESS_IPH
12357
0
        Py_END_ALLOW_THREADS
12358
0
    } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12359
12360
0
    if (size < 0 && !async_err)
12361
0
        posix_error();
12362
0
    return size;
12363
0
}
12364
#endif /* HAVE_PWRITE */
12365
12366
#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
12367
/*[clinic input]
12368
@permit_long_summary
12369
@permit_long_docstring_body
12370
os.pwritev -> Py_ssize_t
12371
12372
    fd: int
12373
    buffers: object
12374
    offset: Py_off_t
12375
    flags: int = 0
12376
    /
12377
12378
Writes the contents of bytes-like objects to a file descriptor at a given offset.
12379
12380
Combines the functionality of writev() and pwrite(). All buffers must be a sequence
12381
of bytes-like objects. Buffers are processed in array order. Entire contents of first
12382
buffer is written before proceeding to second, and so on. The operating system may
12383
set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
12384
This function writes the contents of each object to the file descriptor and returns
12385
the total number of bytes written.
12386
12387
The flags argument contains a bitwise OR of zero or more of the following flags:
12388
12389
- RWF_DSYNC
12390
- RWF_SYNC
12391
- RWF_APPEND
12392
12393
Using non-zero flags requires Linux 4.7 or newer.
12394
[clinic start generated code]*/
12395
12396
static Py_ssize_t
12397
os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
12398
                int flags)
12399
/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=99d8a21493ff76ca]*/
12400
0
{
12401
0
    Py_ssize_t cnt;
12402
0
    Py_ssize_t result;
12403
0
    int async_err = 0;
12404
0
    struct iovec *iov;
12405
0
    Py_buffer *buf;
12406
12407
0
    if (!PySequence_Check(buffers)) {
12408
0
        PyErr_SetString(PyExc_TypeError,
12409
0
            "pwritev() arg 2 must be a sequence");
12410
0
        return -1;
12411
0
    }
12412
12413
0
    cnt = PySequence_Size(buffers);
12414
0
    if (cnt < 0) {
12415
0
        return -1;
12416
0
    }
12417
12418
#ifndef HAVE_PWRITEV2
12419
    if(flags != 0) {
12420
        argument_unavailable_error("pwritev2", "flags");
12421
        return -1;
12422
    }
12423
#endif
12424
12425
0
    if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
12426
0
        return -1;
12427
0
    }
12428
0
#ifdef HAVE_PWRITEV2
12429
0
    do {
12430
0
        Py_BEGIN_ALLOW_THREADS
12431
0
        _Py_BEGIN_SUPPRESS_IPH
12432
0
        result = pwritev2(fd, iov, cnt, offset, flags);
12433
0
        _Py_END_SUPPRESS_IPH
12434
0
        Py_END_ALLOW_THREADS
12435
0
    } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12436
#else
12437
12438
#if defined(__APPLE__) && defined(__clang__)
12439
/* This entire function will be removed from the module dict when the API
12440
 * is not available.
12441
 */
12442
#pragma clang diagnostic push
12443
#pragma clang diagnostic ignored "-Wunguarded-availability"
12444
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
12445
#endif
12446
    do {
12447
        Py_BEGIN_ALLOW_THREADS
12448
        _Py_BEGIN_SUPPRESS_IPH
12449
        result = pwritev(fd, iov, cnt, offset);
12450
        _Py_END_SUPPRESS_IPH
12451
        Py_END_ALLOW_THREADS
12452
    } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12453
12454
#if defined(__APPLE__) && defined(__clang__)
12455
#pragma clang diagnostic pop
12456
#endif
12457
12458
#endif
12459
12460
0
    if (result < 0) {
12461
0
        if (!async_err) {
12462
0
            posix_error();
12463
0
        }
12464
0
        result = -1;
12465
0
    }
12466
0
    iov_cleanup(iov, buf, cnt);
12467
12468
0
    return result;
12469
0
}
12470
#endif /* HAVE_PWRITEV */
12471
12472
#ifdef HAVE_COPY_FILE_RANGE
12473
/*[clinic input]
12474
12475
os.copy_file_range
12476
    src: int
12477
        Source file descriptor.
12478
    dst: int
12479
        Destination file descriptor.
12480
    count: Py_ssize_t
12481
        Number of bytes to copy.
12482
    offset_src: object = None
12483
        Starting offset in src.
12484
    offset_dst: object = None
12485
        Starting offset in dst.
12486
12487
Copy count bytes from one file descriptor to another.
12488
12489
If offset_src is None, then src is read from the current position;
12490
respectively for offset_dst.
12491
[clinic start generated code]*/
12492
12493
static PyObject *
12494
os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
12495
                        PyObject *offset_src, PyObject *offset_dst)
12496
/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
12497
0
{
12498
0
    off_t offset_src_val, offset_dst_val;
12499
0
    off_t *p_offset_src = NULL;
12500
0
    off_t *p_offset_dst = NULL;
12501
0
    Py_ssize_t ret;
12502
0
    int async_err = 0;
12503
    /* The flags argument is provided to allow
12504
     * for future extensions and currently must be to 0. */
12505
0
    int flags = 0;
12506
12507
12508
0
    if (count < 0) {
12509
0
        PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
12510
0
        return NULL;
12511
0
    }
12512
12513
0
    if (offset_src != Py_None) {
12514
0
        if (!Py_off_t_converter(offset_src, &offset_src_val)) {
12515
0
            return NULL;
12516
0
        }
12517
0
        p_offset_src = &offset_src_val;
12518
0
    }
12519
12520
0
    if (offset_dst != Py_None) {
12521
0
        if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
12522
0
            return NULL;
12523
0
        }
12524
0
        p_offset_dst = &offset_dst_val;
12525
0
    }
12526
12527
0
    do {
12528
0
        Py_BEGIN_ALLOW_THREADS
12529
0
        ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
12530
0
        Py_END_ALLOW_THREADS
12531
0
    } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12532
12533
0
    if (ret < 0) {
12534
0
        return (!async_err) ? posix_error() : NULL;
12535
0
    }
12536
12537
0
    return PyLong_FromSsize_t(ret);
12538
0
}
12539
#endif /* HAVE_COPY_FILE_RANGE*/
12540
12541
#if (defined(HAVE_SPLICE) && !defined(_AIX))
12542
/*[clinic input]
12543
12544
os.splice
12545
    src: int
12546
        Source file descriptor.
12547
    dst: int
12548
        Destination file descriptor.
12549
    count: Py_ssize_t
12550
        Number of bytes to copy.
12551
    offset_src: object = None
12552
        Starting offset in src.
12553
    offset_dst: object = None
12554
        Starting offset in dst.
12555
    flags: unsigned_int = 0
12556
        Flags to modify the semantics of the call.
12557
12558
Transfer count bytes from one pipe to a descriptor or vice versa.
12559
12560
If offset_src is None, then src is read from the current position;
12561
respectively for offset_dst. The offset associated to the file
12562
descriptor that refers to a pipe must be None.
12563
[clinic start generated code]*/
12564
12565
static PyObject *
12566
os_splice_impl(PyObject *module, int src, int dst, Py_ssize_t count,
12567
               PyObject *offset_src, PyObject *offset_dst,
12568
               unsigned int flags)
12569
/*[clinic end generated code: output=d0386f25a8519dc5 input=047527c66c6d2e0a]*/
12570
0
{
12571
0
    off_t offset_src_val, offset_dst_val;
12572
0
    off_t *p_offset_src = NULL;
12573
0
    off_t *p_offset_dst = NULL;
12574
0
    Py_ssize_t ret;
12575
0
    int async_err = 0;
12576
12577
0
    if (count < 0) {
12578
0
        PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
12579
0
        return NULL;
12580
0
    }
12581
12582
0
    if (offset_src != Py_None) {
12583
0
        if (!Py_off_t_converter(offset_src, &offset_src_val)) {
12584
0
            return NULL;
12585
0
        }
12586
0
        p_offset_src = &offset_src_val;
12587
0
    }
12588
12589
0
    if (offset_dst != Py_None) {
12590
0
        if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
12591
0
            return NULL;
12592
0
        }
12593
0
        p_offset_dst = &offset_dst_val;
12594
0
    }
12595
12596
0
    do {
12597
0
        Py_BEGIN_ALLOW_THREADS
12598
0
        ret = splice(src, p_offset_src, dst, p_offset_dst, count, flags);
12599
0
        Py_END_ALLOW_THREADS
12600
0
    } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
12601
12602
0
    if (ret < 0) {
12603
0
        return (!async_err) ? posix_error() : NULL;
12604
0
    }
12605
12606
0
    return PyLong_FromSsize_t(ret);
12607
0
}
12608
#endif /* HAVE_SPLICE*/
12609
12610
#ifdef HAVE_MKFIFO
12611
/*[clinic input]
12612
os.mkfifo
12613
12614
    path: path_t
12615
    mode: int=0o666
12616
    *
12617
    dir_fd: dir_fd(requires='mkfifoat')=None
12618
12619
Create a "fifo" (a POSIX named pipe).
12620
12621
If dir_fd is not None, it should be a file descriptor open to a directory,
12622
  and path should be relative; path will then be relative to that directory.
12623
dir_fd may not be implemented on your platform.
12624
  If it is unavailable, using it will raise a NotImplementedError.
12625
[clinic start generated code]*/
12626
12627
static PyObject *
12628
os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
12629
/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
12630
0
{
12631
0
    int result;
12632
0
    int async_err = 0;
12633
0
#ifdef HAVE_MKFIFOAT
12634
0
    int mkfifoat_unavailable = 0;
12635
0
#endif
12636
12637
0
    do {
12638
0
        Py_BEGIN_ALLOW_THREADS
12639
0
#ifdef HAVE_MKFIFOAT
12640
0
        if (dir_fd != DEFAULT_DIR_FD) {
12641
0
            if (HAVE_MKFIFOAT_RUNTIME) {
12642
0
                result = mkfifoat(dir_fd, path->narrow, mode);
12643
12644
0
            } else {
12645
0
                mkfifoat_unavailable = 1;
12646
0
                result = 0;
12647
0
            }
12648
0
        } else
12649
0
#endif
12650
0
            result = mkfifo(path->narrow, mode);
12651
0
        Py_END_ALLOW_THREADS
12652
0
    } while (result != 0 && errno == EINTR &&
12653
0
             !(async_err = PyErr_CheckSignals()));
12654
12655
0
#ifdef HAVE_MKFIFOAT
12656
0
    if (mkfifoat_unavailable) {
12657
0
        argument_unavailable_error(NULL, "dir_fd");
12658
0
        return NULL;
12659
0
    }
12660
0
#endif
12661
12662
0
    if (result != 0)
12663
0
        return (!async_err) ? posix_error() : NULL;
12664
12665
0
    Py_RETURN_NONE;
12666
0
}
12667
#endif /* HAVE_MKFIFO */
12668
12669
12670
#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
12671
/*[clinic input]
12672
@permit_long_docstring_body
12673
os.mknod
12674
12675
    path: path_t
12676
    mode: int=0o600
12677
    device: dev_t=0
12678
    *
12679
    dir_fd: dir_fd(requires='mknodat')=None
12680
12681
Create a node in the file system.
12682
12683
Create a node in the file system (file, device special file or named pipe)
12684
at path.  mode specifies both the permissions to use and the
12685
type of node to be created, being combined (bitwise OR) with one of
12686
S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO.  If S_IFCHR or S_IFBLK is set on mode,
12687
device defines the newly created device special file (probably using
12688
os.makedev()).  Otherwise device is ignored.
12689
12690
If dir_fd is not None, it should be a file descriptor open to a directory,
12691
  and path should be relative; path will then be relative to that directory.
12692
dir_fd may not be implemented on your platform.
12693
  If it is unavailable, using it will raise a NotImplementedError.
12694
[clinic start generated code]*/
12695
12696
static PyObject *
12697
os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
12698
              int dir_fd)
12699
/*[clinic end generated code: output=92e55d3ca8917461 input=7121c4723d22545b]*/
12700
0
{
12701
0
    int result;
12702
0
    int async_err = 0;
12703
0
#ifdef HAVE_MKNODAT
12704
0
    int mknodat_unavailable = 0;
12705
0
#endif
12706
12707
0
    do {
12708
0
        Py_BEGIN_ALLOW_THREADS
12709
0
#ifdef HAVE_MKNODAT
12710
0
        if (dir_fd != DEFAULT_DIR_FD) {
12711
0
            if (HAVE_MKNODAT_RUNTIME) {
12712
0
                result = mknodat(dir_fd, path->narrow, mode, device);
12713
12714
0
            } else {
12715
0
                mknodat_unavailable = 1;
12716
0
                result = 0;
12717
0
            }
12718
0
        } else
12719
0
#endif
12720
0
            result = mknod(path->narrow, mode, device);
12721
0
        Py_END_ALLOW_THREADS
12722
0
    } while (result != 0 && errno == EINTR &&
12723
0
             !(async_err = PyErr_CheckSignals()));
12724
0
#ifdef HAVE_MKNODAT
12725
0
    if (mknodat_unavailable) {
12726
0
        argument_unavailable_error(NULL, "dir_fd");
12727
0
        return NULL;
12728
0
    }
12729
0
#endif
12730
0
    if (result != 0)
12731
0
        return (!async_err) ? posix_error() : NULL;
12732
12733
0
    Py_RETURN_NONE;
12734
0
}
12735
#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
12736
12737
12738
#ifdef HAVE_DEVICE_MACROS
12739
static PyObject *
12740
major_minor_conv(unsigned int value)
12741
0
{
12742
0
#ifdef NODEV
12743
0
    if (value == (unsigned int)NODEV) {
12744
0
        return PyLong_FromLong((int)NODEV);
12745
0
    }
12746
0
#endif
12747
0
    return PyLong_FromUnsignedLong(value);
12748
0
}
12749
12750
static int
12751
major_minor_check(dev_t value)
12752
0
{
12753
0
#ifdef NODEV
12754
0
    if (value == NODEV) {
12755
0
        return 1;
12756
0
    }
12757
0
#endif
12758
0
    return (dev_t)(unsigned int)value == value;
12759
0
}
12760
12761
/*[clinic input]
12762
os.major
12763
12764
    device: dev_t
12765
    /
12766
12767
Extracts a device major number from a raw device number.
12768
[clinic start generated code]*/
12769
12770
static PyObject *
12771
os_major_impl(PyObject *module, dev_t device)
12772
/*[clinic end generated code: output=4071ffee17647891 input=b1a0a14ec9448229]*/
12773
0
{
12774
0
    return major_minor_conv(major(device));
12775
0
}
12776
12777
12778
/*[clinic input]
12779
os.minor
12780
12781
    device: dev_t
12782
    /
12783
12784
Extracts a device minor number from a raw device number.
12785
[clinic start generated code]*/
12786
12787
static PyObject *
12788
os_minor_impl(PyObject *module, dev_t device)
12789
/*[clinic end generated code: output=306cb78e3bc5004f input=2f686e463682a9da]*/
12790
0
{
12791
0
    return major_minor_conv(minor(device));
12792
0
}
12793
12794
12795
/*[clinic input]
12796
os.makedev -> dev_t
12797
12798
    major: dev_t
12799
    minor: dev_t
12800
    /
12801
12802
Composes a raw device number from the major and minor device numbers.
12803
[clinic start generated code]*/
12804
12805
static dev_t
12806
os_makedev_impl(PyObject *module, dev_t major, dev_t minor)
12807
/*[clinic end generated code: output=cad6125c51f5af80 input=2146126ec02e55c1]*/
12808
0
{
12809
0
    if (!major_minor_check(major) || !major_minor_check(minor)) {
12810
0
        PyErr_SetString(PyExc_OverflowError,
12811
0
                        "Python int too large to convert to C unsigned int");
12812
0
        return (dev_t)-1;
12813
0
    }
12814
0
    return makedev(major, minor);
12815
0
}
12816
#endif /* HAVE_DEVICE_MACROS */
12817
12818
12819
#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
12820
/*[clinic input]
12821
os.ftruncate
12822
12823
    fd: int
12824
    length: Py_off_t
12825
    /
12826
12827
Truncate a file, specified by file descriptor, to a specific length.
12828
[clinic start generated code]*/
12829
12830
static PyObject *
12831
os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
12832
/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
12833
0
{
12834
0
    int result;
12835
0
    int async_err = 0;
12836
12837
0
    if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
12838
0
        return NULL;
12839
0
    }
12840
12841
0
    do {
12842
0
        Py_BEGIN_ALLOW_THREADS
12843
0
        _Py_BEGIN_SUPPRESS_IPH
12844
#ifdef MS_WINDOWS
12845
        result = _chsize_s(fd, length);
12846
#else
12847
0
        result = ftruncate(fd, length);
12848
0
#endif
12849
0
        _Py_END_SUPPRESS_IPH
12850
0
        Py_END_ALLOW_THREADS
12851
0
    } while (result != 0 && errno == EINTR &&
12852
0
             !(async_err = PyErr_CheckSignals()));
12853
0
    if (result != 0)
12854
0
        return (!async_err) ? posix_error() : NULL;
12855
0
    Py_RETURN_NONE;
12856
0
}
12857
#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
12858
12859
12860
#if defined HAVE_TRUNCATE || defined MS_WINDOWS
12861
/*[clinic input]
12862
os.truncate
12863
    path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
12864
    length: Py_off_t
12865
12866
Truncate a file, specified by path, to a specific length.
12867
12868
On some platforms, path may also be specified as an open file descriptor.
12869
  If this functionality is unavailable, using it raises an exception.
12870
[clinic start generated code]*/
12871
12872
static PyObject *
12873
os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
12874
/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
12875
0
{
12876
0
    int result;
12877
#ifdef MS_WINDOWS
12878
    int fd;
12879
#endif
12880
12881
0
    if (path->fd != -1)
12882
0
        return os_ftruncate_impl(module, path->fd, length);
12883
12884
0
    if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
12885
0
        return NULL;
12886
0
    }
12887
12888
0
    Py_BEGIN_ALLOW_THREADS
12889
0
    _Py_BEGIN_SUPPRESS_IPH
12890
#ifdef MS_WINDOWS
12891
    fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
12892
    if (fd < 0)
12893
        result = -1;
12894
    else {
12895
        result = _chsize_s(fd, length);
12896
        close(fd);
12897
        if (result < 0)
12898
            errno = result;
12899
    }
12900
#else
12901
0
    result = truncate(path->narrow, length);
12902
0
#endif
12903
0
    _Py_END_SUPPRESS_IPH
12904
0
    Py_END_ALLOW_THREADS
12905
0
    if (result < 0)
12906
0
        return posix_path_error(path);
12907
12908
0
    Py_RETURN_NONE;
12909
0
}
12910
#endif /* HAVE_TRUNCATE || MS_WINDOWS */
12911
12912
12913
/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
12914
   and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
12915
   defined, which is the case in Python on AIX. AIX bug report:
12916
   http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
12917
#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
12918
#  define POSIX_FADVISE_AIX_BUG
12919
#endif
12920
12921
12922
/* GH-111804: Due to posix_fallocate() not having consistent semantics across
12923
   OSs, support was dropped in WASI preview2. */
12924
#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) && \
12925
    !defined(__wasi__)
12926
/*[clinic input]
12927
os.posix_fallocate
12928
12929
    fd: int
12930
    offset: Py_off_t
12931
    length: Py_off_t
12932
    /
12933
12934
Ensure a file has allocated at least a particular number of bytes on disk.
12935
12936
Ensure that the file specified by fd encompasses a range of bytes
12937
starting at offset bytes from the beginning and continuing for length bytes.
12938
[clinic start generated code]*/
12939
12940
static PyObject *
12941
os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
12942
                        Py_off_t length)
12943
/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
12944
0
{
12945
0
    int result;
12946
0
    int async_err = 0;
12947
12948
0
    do {
12949
0
        Py_BEGIN_ALLOW_THREADS
12950
0
        result = posix_fallocate(fd, offset, length);
12951
0
        Py_END_ALLOW_THREADS
12952
0
    } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
12953
12954
0
    if (result == 0)
12955
0
        Py_RETURN_NONE;
12956
12957
0
    if (async_err)
12958
0
        return NULL;
12959
12960
0
    errno = result;
12961
0
    return posix_error();
12962
0
}
12963
#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG && !defined(__wasi__) */
12964
12965
12966
#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
12967
/*[clinic input]
12968
os.posix_fadvise
12969
12970
    fd: int
12971
    offset: Py_off_t
12972
    length: Py_off_t
12973
    advice: int
12974
    /
12975
12976
Announce an intention to access data in a specific pattern.
12977
12978
Announce an intention to access data in a specific pattern, thus allowing
12979
the kernel to make optimizations.
12980
The advice applies to the region of the file specified by fd starting at
12981
offset and continuing for length bytes.
12982
advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
12983
POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
12984
POSIX_FADV_DONTNEED.
12985
[clinic start generated code]*/
12986
12987
static PyObject *
12988
os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
12989
                      Py_off_t length, int advice)
12990
/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
12991
0
{
12992
0
    int result;
12993
0
    int async_err = 0;
12994
12995
0
    do {
12996
0
        Py_BEGIN_ALLOW_THREADS
12997
0
        result = posix_fadvise(fd, offset, length, advice);
12998
0
        Py_END_ALLOW_THREADS
12999
0
    } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
13000
13001
0
    if (result == 0)
13002
0
        Py_RETURN_NONE;
13003
13004
0
    if (async_err)
13005
0
        return NULL;
13006
13007
0
    errno = result;
13008
0
    return posix_error();
13009
0
}
13010
#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
13011
13012
13013
#ifdef MS_WINDOWS
13014
static PyObject*
13015
win32_putenv(PyObject *name, PyObject *value)
13016
{
13017
    /* Search from index 1 because on Windows starting '=' is allowed for
13018
       defining hidden environment variables. */
13019
    if (PyUnicode_GET_LENGTH(name) == 0 ||
13020
        PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
13021
    {
13022
        PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
13023
        return NULL;
13024
    }
13025
    PyObject *unicode;
13026
    if (value != NULL) {
13027
        unicode = PyUnicode_FromFormat("%U=%U", name, value);
13028
    }
13029
    else {
13030
        unicode = PyUnicode_FromFormat("%U=", name);
13031
    }
13032
    if (unicode == NULL) {
13033
        return NULL;
13034
    }
13035
13036
    Py_ssize_t size;
13037
    wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
13038
    Py_DECREF(unicode);
13039
13040
    if (env == NULL) {
13041
        return NULL;
13042
    }
13043
    if (size > _MAX_ENV) {
13044
        PyErr_Format(PyExc_ValueError,
13045
                     "the environment variable is longer than %u characters",
13046
                     _MAX_ENV);
13047
        PyMem_Free(env);
13048
        return NULL;
13049
    }
13050
    if (wcslen(env) != (size_t)size) {
13051
        PyErr_SetString(PyExc_ValueError,
13052
                        "embedded null character");
13053
        PyMem_Free(env);
13054
        return NULL;
13055
    }
13056
13057
    /* _wputenv() and SetEnvironmentVariableW() update the environment in the
13058
       Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
13059
       and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
13060
13061
       Prefer _wputenv() to be compatible with C libraries using CRT
13062
       variables and CRT functions using these variables (ex: getenv()). */
13063
    int err = _wputenv(env);
13064
13065
    if (err) {
13066
        posix_error();
13067
        PyMem_Free(env);
13068
        return NULL;
13069
    }
13070
    PyMem_Free(env);
13071
13072
    Py_RETURN_NONE;
13073
}
13074
#endif
13075
13076
13077
#ifdef MS_WINDOWS
13078
/*[clinic input]
13079
os.putenv
13080
13081
    name: unicode
13082
    value: unicode
13083
    /
13084
13085
Change or add an environment variable.
13086
[clinic start generated code]*/
13087
13088
static PyObject *
13089
os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
13090
/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
13091
{
13092
    if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
13093
        return NULL;
13094
    }
13095
    return win32_putenv(name, value);
13096
}
13097
#else
13098
/*[clinic input]
13099
os.putenv
13100
13101
    name: FSConverter
13102
    value: FSConverter
13103
    /
13104
13105
Change or add an environment variable.
13106
[clinic start generated code]*/
13107
13108
static PyObject *
13109
os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
13110
/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
13111
0
{
13112
0
    const char *name_string = PyBytes_AS_STRING(name);
13113
0
    const char *value_string = PyBytes_AS_STRING(value);
13114
13115
0
    if (strchr(name_string, '=') != NULL) {
13116
0
        PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
13117
0
        return NULL;
13118
0
    }
13119
13120
0
    if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
13121
0
        return NULL;
13122
0
    }
13123
13124
0
    if (setenv(name_string, value_string, 1)) {
13125
0
        return posix_error();
13126
0
    }
13127
0
    Py_RETURN_NONE;
13128
0
}
13129
#endif  /* !defined(MS_WINDOWS) */
13130
13131
13132
#ifdef MS_WINDOWS
13133
/*[clinic input]
13134
os.unsetenv
13135
    name: unicode
13136
    /
13137
13138
Delete an environment variable.
13139
[clinic start generated code]*/
13140
13141
static PyObject *
13142
os_unsetenv_impl(PyObject *module, PyObject *name)
13143
/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
13144
{
13145
    if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
13146
        return NULL;
13147
    }
13148
    return win32_putenv(name, NULL);
13149
}
13150
#else
13151
/*[clinic input]
13152
os.unsetenv
13153
    name: FSConverter
13154
    /
13155
13156
Delete an environment variable.
13157
[clinic start generated code]*/
13158
13159
static PyObject *
13160
os_unsetenv_impl(PyObject *module, PyObject *name)
13161
/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
13162
0
{
13163
0
    if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
13164
0
        return NULL;
13165
0
    }
13166
#ifdef HAVE_BROKEN_UNSETENV
13167
    unsetenv(PyBytes_AS_STRING(name));
13168
#else
13169
0
    int err = unsetenv(PyBytes_AS_STRING(name));
13170
0
    if (err) {
13171
0
        return posix_error();
13172
0
    }
13173
0
#endif
13174
13175
0
    Py_RETURN_NONE;
13176
0
}
13177
#endif /* !MS_WINDOWS */
13178
13179
13180
/*[clinic input]
13181
os.strerror
13182
13183
    code: int
13184
    /
13185
13186
Translate an error code to a message string.
13187
[clinic start generated code]*/
13188
13189
static PyObject *
13190
os_strerror_impl(PyObject *module, int code)
13191
/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
13192
0
{
13193
0
    char *message = strerror(code);
13194
0
    if (message == NULL) {
13195
0
        PyErr_SetString(PyExc_ValueError,
13196
0
                        "strerror() argument out of range");
13197
0
        return NULL;
13198
0
    }
13199
0
    return PyUnicode_DecodeLocale(message, "surrogateescape");
13200
0
}
13201
13202
13203
#ifdef HAVE_SYS_WAIT_H
13204
#ifdef WCOREDUMP
13205
/*[clinic input]
13206
os.WCOREDUMP -> bool
13207
13208
    status: int
13209
    /
13210
13211
Return True if the process returning status was dumped to a core file.
13212
[clinic start generated code]*/
13213
13214
static int
13215
os_WCOREDUMP_impl(PyObject *module, int status)
13216
/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
13217
0
{
13218
0
    WAIT_TYPE wait_status;
13219
0
    WAIT_STATUS_INT(wait_status) = status;
13220
0
    return WCOREDUMP(wait_status);
13221
0
}
13222
#endif /* WCOREDUMP */
13223
13224
13225
#ifdef WIFCONTINUED
13226
/*[clinic input]
13227
os.WIFCONTINUED -> bool
13228
13229
    status: int
13230
13231
Return True if a particular process was continued from a job control stop.
13232
13233
Return True if the process returning status was continued from a
13234
job control stop.
13235
[clinic start generated code]*/
13236
13237
static int
13238
os_WIFCONTINUED_impl(PyObject *module, int status)
13239
/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
13240
0
{
13241
0
    WAIT_TYPE wait_status;
13242
0
    WAIT_STATUS_INT(wait_status) = status;
13243
0
    return WIFCONTINUED(wait_status);
13244
0
}
13245
#endif /* WIFCONTINUED */
13246
13247
13248
#ifdef WIFSTOPPED
13249
/*[clinic input]
13250
os.WIFSTOPPED -> bool
13251
13252
    status: int
13253
13254
Return True if the process returning status was stopped.
13255
[clinic start generated code]*/
13256
13257
static int
13258
os_WIFSTOPPED_impl(PyObject *module, int status)
13259
/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
13260
0
{
13261
0
    WAIT_TYPE wait_status;
13262
0
    WAIT_STATUS_INT(wait_status) = status;
13263
0
    return WIFSTOPPED(wait_status);
13264
0
}
13265
#endif /* WIFSTOPPED */
13266
13267
13268
#ifdef WIFSIGNALED
13269
/*[clinic input]
13270
os.WIFSIGNALED -> bool
13271
13272
    status: int
13273
13274
Return True if the process returning status was terminated by a signal.
13275
[clinic start generated code]*/
13276
13277
static int
13278
os_WIFSIGNALED_impl(PyObject *module, int status)
13279
/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
13280
0
{
13281
0
    WAIT_TYPE wait_status;
13282
0
    WAIT_STATUS_INT(wait_status) = status;
13283
0
    return WIFSIGNALED(wait_status);
13284
0
}
13285
#endif /* WIFSIGNALED */
13286
13287
13288
#ifdef WIFEXITED
13289
/*[clinic input]
13290
@permit_long_summary
13291
os.WIFEXITED -> bool
13292
13293
    status: int
13294
13295
Return True if the process returning status exited via the exit() system call.
13296
[clinic start generated code]*/
13297
13298
static int
13299
os_WIFEXITED_impl(PyObject *module, int status)
13300
/*[clinic end generated code: output=01c09d6ebfeea397 input=8c24a82148709b30]*/
13301
0
{
13302
0
    WAIT_TYPE wait_status;
13303
0
    WAIT_STATUS_INT(wait_status) = status;
13304
0
    return WIFEXITED(wait_status);
13305
0
}
13306
#endif /* WIFEXITED */
13307
13308
13309
#ifdef WEXITSTATUS
13310
/*[clinic input]
13311
os.WEXITSTATUS -> int
13312
13313
    status: int
13314
13315
Return the process return code from status.
13316
[clinic start generated code]*/
13317
13318
static int
13319
os_WEXITSTATUS_impl(PyObject *module, int status)
13320
/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
13321
0
{
13322
0
    WAIT_TYPE wait_status;
13323
0
    WAIT_STATUS_INT(wait_status) = status;
13324
0
    return WEXITSTATUS(wait_status);
13325
0
}
13326
#endif /* WEXITSTATUS */
13327
13328
13329
#ifdef WTERMSIG
13330
/*[clinic input]
13331
@permit_long_summary
13332
os.WTERMSIG -> int
13333
13334
    status: int
13335
13336
Return the signal that terminated the process that provided the status value.
13337
[clinic start generated code]*/
13338
13339
static int
13340
os_WTERMSIG_impl(PyObject *module, int status)
13341
/*[clinic end generated code: output=172f7dfc8dcfc3ad input=89072f6cbf3f8050]*/
13342
0
{
13343
0
    WAIT_TYPE wait_status;
13344
0
    WAIT_STATUS_INT(wait_status) = status;
13345
0
    return WTERMSIG(wait_status);
13346
0
}
13347
#endif /* WTERMSIG */
13348
13349
13350
#ifdef WSTOPSIG
13351
/*[clinic input]
13352
os.WSTOPSIG -> int
13353
13354
    status: int
13355
13356
Return the signal that stopped the process that provided the status value.
13357
[clinic start generated code]*/
13358
13359
static int
13360
os_WSTOPSIG_impl(PyObject *module, int status)
13361
/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
13362
0
{
13363
0
    WAIT_TYPE wait_status;
13364
0
    WAIT_STATUS_INT(wait_status) = status;
13365
0
    return WSTOPSIG(wait_status);
13366
0
}
13367
#endif /* WSTOPSIG */
13368
#endif /* HAVE_SYS_WAIT_H */
13369
13370
13371
#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
13372
#ifdef _SCO_DS
13373
/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
13374
   needed definitions in sys/statvfs.h */
13375
#define _SVID3
13376
#endif
13377
#include <sys/statvfs.h>
13378
13379
#ifdef __APPLE__
13380
/* On macOS struct statvfs uses 32-bit integers for block counts,
13381
 * resulting in overflow when filesystems are larger than 4TB. Therefore
13382
 * os.statvfs is implemented in terms of statfs(2).
13383
 */
13384
13385
static PyObject*
13386
_pystatvfs_fromstructstatfs(PyObject *module, struct statfs st) {
13387
    PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType;
13388
    PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
13389
    if (v == NULL) {
13390
        return NULL;
13391
    }
13392
13393
    long flags = 0;
13394
    if (st.f_flags & MNT_RDONLY) {
13395
        flags |= ST_RDONLY;
13396
    }
13397
    if (st.f_flags & MNT_NOSUID) {
13398
        flags |= ST_NOSUID;
13399
    }
13400
13401
    _Static_assert(sizeof(st.f_blocks) == sizeof(long long), "assuming large file");
13402
13403
#define SET_ITEM(SEQ, INDEX, EXPR)                       \
13404
    do {                                                 \
13405
        PyObject *obj = (EXPR);                          \
13406
        if (obj == NULL) {                               \
13407
            Py_DECREF((SEQ));                            \
13408
            return NULL;                                 \
13409
        }                                                \
13410
        PyStructSequence_SET_ITEM((SEQ), (INDEX), obj);  \
13411
    } while (0)
13412
13413
    SET_ITEM(v, 0, PyLong_FromLong((long) st.f_iosize));
13414
    SET_ITEM(v, 1, PyLong_FromLong((long) st.f_bsize));
13415
    SET_ITEM(v, 2, PyLong_FromLongLong((long long) st.f_blocks));
13416
    SET_ITEM(v, 3, PyLong_FromLongLong((long long) st.f_bfree));
13417
    SET_ITEM(v, 4, PyLong_FromLongLong((long long) st.f_bavail));
13418
    SET_ITEM(v, 5, PyLong_FromLongLong((long long) st.f_files));
13419
    SET_ITEM(v, 6, PyLong_FromLongLong((long long) st.f_ffree));
13420
    SET_ITEM(v, 7, PyLong_FromLongLong((long long) st.f_ffree));
13421
    SET_ITEM(v, 8, PyLong_FromLong((long) flags));
13422
13423
    SET_ITEM(v, 9, PyLong_FromLong((long) NAME_MAX));
13424
    SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
13425
13426
#undef SET_ITEM
13427
13428
    return v;
13429
}
13430
13431
#else
13432
13433
13434
13435
static PyObject*
13436
0
_pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) {
13437
0
    PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType;
13438
0
    PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
13439
0
    if (v == NULL)
13440
0
        return NULL;
13441
13442
0
    int pos = 0;
13443
13444
0
#define SET_RESULT(CALL)                                     \
13445
0
    do {                                                     \
13446
0
        PyObject *item = (CALL);                             \
13447
0
        if (item == NULL) {                                  \
13448
0
            Py_DECREF(v);                                    \
13449
0
            return NULL;                                     \
13450
0
        }                                                    \
13451
0
        PyStructSequence_SET_ITEM(v, pos++, item);           \
13452
0
    } while(0)
13453
13454
0
#if !defined(HAVE_LARGEFILE_SUPPORT)
13455
0
    SET_RESULT(PyLong_FromLong((long) st.f_bsize));
13456
0
    SET_RESULT(PyLong_FromLong((long) st.f_frsize));
13457
0
    SET_RESULT(PyLong_FromLong((long) st.f_blocks));
13458
0
    SET_RESULT(PyLong_FromLong((long) st.f_bfree));
13459
0
    SET_RESULT(PyLong_FromLong((long) st.f_bavail));
13460
0
    SET_RESULT(PyLong_FromLong((long) st.f_files));
13461
0
    SET_RESULT(PyLong_FromLong((long) st.f_ffree));
13462
0
    SET_RESULT(PyLong_FromLong((long) st.f_favail));
13463
0
    SET_RESULT(PyLong_FromLong((long) st.f_flag));
13464
0
    SET_RESULT(PyLong_FromLong((long) st.f_namemax));
13465
#else
13466
    SET_RESULT(PyLong_FromLong((long) st.f_bsize));
13467
    SET_RESULT(PyLong_FromLong((long) st.f_frsize));
13468
    SET_RESULT(PyLong_FromLongLong((long long) st.f_blocks));
13469
    SET_RESULT(PyLong_FromLongLong((long long) st.f_bfree));
13470
    SET_RESULT(PyLong_FromLongLong((long long) st.f_bavail));
13471
    SET_RESULT(PyLong_FromLongLong((long long) st.f_files));
13472
    SET_RESULT(PyLong_FromLongLong((long long) st.f_ffree));
13473
    SET_RESULT(PyLong_FromLongLong((long long) st.f_favail));
13474
    SET_RESULT(PyLong_FromLong((long) st.f_flag));
13475
    SET_RESULT(PyLong_FromLong((long) st.f_namemax));
13476
#endif
13477
/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
13478
 * (issue #32390). */
13479
#if defined(_AIX) && defined(_ALL_SOURCE)
13480
    SET_RESULT(PyLong_FromUnsignedLong(st.f_fsid.val[0]));
13481
#else
13482
0
    SET_RESULT(PyLong_FromUnsignedLong(st.f_fsid));
13483
0
#endif
13484
13485
0
#undef SET_RESULT
13486
13487
0
    return v;
13488
0
}
13489
13490
#endif
13491
13492
13493
/*[clinic input]
13494
os.fstatvfs
13495
    fd: int
13496
    /
13497
13498
Perform an fstatvfs system call on the given fd.
13499
13500
Equivalent to statvfs(fd).
13501
[clinic start generated code]*/
13502
13503
static PyObject *
13504
os_fstatvfs_impl(PyObject *module, int fd)
13505
/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
13506
0
{
13507
0
    int result;
13508
0
    int async_err = 0;
13509
#ifdef __APPLE__
13510
    struct statfs st;
13511
    /* On macOS os.fstatvfs is implemented using fstatfs(2) because
13512
     * the former uses 32-bit values for block counts.
13513
     */
13514
    do {
13515
        Py_BEGIN_ALLOW_THREADS
13516
        result = fstatfs(fd, &st);
13517
        Py_END_ALLOW_THREADS
13518
    } while (result != 0 && errno == EINTR &&
13519
             !(async_err = PyErr_CheckSignals()));
13520
    if (result != 0)
13521
        return (!async_err) ? posix_error() : NULL;
13522
13523
    return _pystatvfs_fromstructstatfs(module, st);
13524
#else
13525
0
    struct statvfs st;
13526
13527
0
    do {
13528
0
        Py_BEGIN_ALLOW_THREADS
13529
0
        result = fstatvfs(fd, &st);
13530
0
        Py_END_ALLOW_THREADS
13531
0
    } while (result != 0 && errno == EINTR &&
13532
0
             !(async_err = PyErr_CheckSignals()));
13533
0
    if (result != 0)
13534
0
        return (!async_err) ? posix_error() : NULL;
13535
13536
0
    return _pystatvfs_fromstructstatvfs(module, st);
13537
0
#endif
13538
0
}
13539
#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
13540
13541
13542
#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
13543
#include <sys/statvfs.h>
13544
/*[clinic input]
13545
os.statvfs
13546
13547
    path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
13548
13549
Perform a statvfs system call on the given path.
13550
13551
path may always be specified as a string.
13552
On some platforms, path may also be specified as an open file descriptor.
13553
  If this functionality is unavailable, using it raises an exception.
13554
[clinic start generated code]*/
13555
13556
static PyObject *
13557
os_statvfs_impl(PyObject *module, path_t *path)
13558
/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
13559
0
{
13560
0
    int result;
13561
13562
#ifdef __APPLE__
13563
    /* On macOS os.statvfs is implemented using statfs(2)/fstatfs(2) because
13564
     * the former uses 32-bit values for block counts.
13565
     */
13566
    struct statfs st;
13567
13568
    Py_BEGIN_ALLOW_THREADS
13569
    if (path->fd != -1) {
13570
        result = fstatfs(path->fd, &st);
13571
    }
13572
    else
13573
        result = statfs(path->narrow, &st);
13574
    Py_END_ALLOW_THREADS
13575
13576
    if (result) {
13577
        return path_error(path);
13578
    }
13579
13580
    return _pystatvfs_fromstructstatfs(module, st);
13581
13582
#else
13583
0
    struct statvfs st;
13584
13585
0
    Py_BEGIN_ALLOW_THREADS
13586
0
#ifdef HAVE_FSTATVFS
13587
0
    if (path->fd != -1) {
13588
0
        result = fstatvfs(path->fd, &st);
13589
0
    }
13590
0
    else
13591
0
#endif
13592
0
        result = statvfs(path->narrow, &st);
13593
0
    Py_END_ALLOW_THREADS
13594
13595
0
    if (result) {
13596
0
        return path_error(path);
13597
0
    }
13598
13599
0
    return _pystatvfs_fromstructstatvfs(module, st);
13600
0
#endif
13601
0
}
13602
#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
13603
13604
13605
#ifdef MS_WINDOWS
13606
/*[clinic input]
13607
os._getdiskusage
13608
13609
    path: path_t
13610
13611
Return disk usage statistics about the given path as a (total, free) tuple.
13612
[clinic start generated code]*/
13613
13614
static PyObject *
13615
os__getdiskusage_impl(PyObject *module, path_t *path)
13616
/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
13617
{
13618
    BOOL retval;
13619
    ULARGE_INTEGER _, total, free;
13620
    DWORD err = 0;
13621
13622
    Py_BEGIN_ALLOW_THREADS
13623
    retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
13624
    Py_END_ALLOW_THREADS
13625
    if (retval == 0) {
13626
        if (GetLastError() == ERROR_DIRECTORY) {
13627
            wchar_t *dir_path = NULL;
13628
13629
            dir_path = PyMem_New(wchar_t, path->length + 1);
13630
            if (dir_path == NULL) {
13631
                return PyErr_NoMemory();
13632
            }
13633
13634
            wcscpy_s(dir_path, path->length + 1, path->wide);
13635
13636
            if (_dirnameW(dir_path) != -1) {
13637
                Py_BEGIN_ALLOW_THREADS
13638
                retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
13639
                Py_END_ALLOW_THREADS
13640
            }
13641
            /* Record the last error in case it's modified by PyMem_Free. */
13642
            err = GetLastError();
13643
            PyMem_Free(dir_path);
13644
            if (retval) {
13645
                goto success;
13646
            }
13647
        }
13648
        return PyErr_SetFromWindowsErr(err);
13649
    }
13650
13651
success:
13652
    return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
13653
}
13654
#endif /* MS_WINDOWS */
13655
13656
13657
/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
13658
 * It maps strings representing configuration variable names to
13659
 * integer values, allowing those functions to be called with the
13660
 * magic names instead of polluting the module's namespace with tons of
13661
 * rarely-used constants.  There are three separate tables that use
13662
 * these definitions.
13663
 *
13664
 * This code is always included, even if none of the interfaces that
13665
 * need it are included.  The #if hackery needed to avoid it would be
13666
 * sufficiently pervasive that it's not worth the loss of readability.
13667
 */
13668
struct constdef {
13669
    const char *name;
13670
    int value;
13671
};
13672
13673
static int
13674
conv_confname(PyObject *module, PyObject *arg, int *valuep, const char *tablename)
13675
0
{
13676
0
    if (PyUnicode_Check(arg)) {
13677
0
        PyObject *table = PyObject_GetAttrString(module, tablename);
13678
0
        if (table == NULL) {
13679
0
            return 0;
13680
0
        }
13681
13682
0
        arg = PyObject_GetItem(table, arg);
13683
0
        Py_DECREF(table);
13684
0
        if (arg == NULL) {
13685
0
            PyErr_SetString(
13686
0
                PyExc_ValueError, "unrecognized configuration name");
13687
0
            return 0;
13688
0
        }
13689
0
    } else {
13690
0
        Py_INCREF(arg);  // Match the Py_DECREF below.
13691
0
    }
13692
13693
0
    int success = 0;
13694
0
    if (!PyIndex_Check(arg)) {
13695
0
        PyErr_SetString(PyExc_TypeError,
13696
0
            "configuration names must be strings or integers");
13697
0
    } else {
13698
0
        int value = PyLong_AsInt(arg);
13699
0
        if (!(value == -1 && PyErr_Occurred())) {
13700
0
            *valuep = value;
13701
0
            success = 1;
13702
0
        }
13703
0
    }
13704
0
    Py_DECREF(arg);
13705
0
    return success;
13706
0
}
13707
13708
13709
#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
13710
static struct constdef  posix_constants_pathconf[] = {
13711
#ifdef _PC_ABI_AIO_XFER_MAX
13712
    {"PC_ABI_AIO_XFER_MAX",     _PC_ABI_AIO_XFER_MAX},
13713
#endif
13714
#ifdef _PC_ABI_ASYNC_IO
13715
    {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
13716
#endif
13717
#ifdef _PC_ASYNC_IO
13718
    {"PC_ASYNC_IO",     _PC_ASYNC_IO},
13719
#endif
13720
#ifdef _PC_CHOWN_RESTRICTED
13721
    {"PC_CHOWN_RESTRICTED",     _PC_CHOWN_RESTRICTED},
13722
#endif
13723
#ifdef _PC_FILESIZEBITS
13724
    {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
13725
#endif
13726
#ifdef _PC_LAST
13727
    {"PC_LAST", _PC_LAST},
13728
#endif
13729
#ifdef _PC_LINK_MAX
13730
    {"PC_LINK_MAX",     _PC_LINK_MAX},
13731
#endif
13732
#ifdef _PC_MAX_CANON
13733
    {"PC_MAX_CANON",    _PC_MAX_CANON},
13734
#endif
13735
#ifdef _PC_MAX_INPUT
13736
    {"PC_MAX_INPUT",    _PC_MAX_INPUT},
13737
#endif
13738
#ifdef _PC_NAME_MAX
13739
    {"PC_NAME_MAX",     _PC_NAME_MAX},
13740
#endif
13741
#ifdef _PC_NO_TRUNC
13742
    {"PC_NO_TRUNC",     _PC_NO_TRUNC},
13743
#endif
13744
#ifdef _PC_PATH_MAX
13745
    {"PC_PATH_MAX",     _PC_PATH_MAX},
13746
#endif
13747
#ifdef _PC_PIPE_BUF
13748
    {"PC_PIPE_BUF",     _PC_PIPE_BUF},
13749
#endif
13750
#ifdef _PC_PRIO_IO
13751
    {"PC_PRIO_IO",      _PC_PRIO_IO},
13752
#endif
13753
#ifdef _PC_SOCK_MAXBUF
13754
    {"PC_SOCK_MAXBUF",  _PC_SOCK_MAXBUF},
13755
#endif
13756
#ifdef _PC_SYNC_IO
13757
    {"PC_SYNC_IO",      _PC_SYNC_IO},
13758
#endif
13759
#ifdef _PC_VDISABLE
13760
    {"PC_VDISABLE",     _PC_VDISABLE},
13761
#endif
13762
#ifdef _PC_ACL_ENABLED
13763
    {"PC_ACL_ENABLED",  _PC_ACL_ENABLED},
13764
#endif
13765
#ifdef _PC_MIN_HOLE_SIZE
13766
    {"PC_MIN_HOLE_SIZE",    _PC_MIN_HOLE_SIZE},
13767
#endif
13768
#ifdef _PC_ALLOC_SIZE_MIN
13769
    {"PC_ALLOC_SIZE_MIN",   _PC_ALLOC_SIZE_MIN},
13770
#endif
13771
#ifdef _PC_REC_INCR_XFER_SIZE
13772
    {"PC_REC_INCR_XFER_SIZE",   _PC_REC_INCR_XFER_SIZE},
13773
#endif
13774
#ifdef _PC_REC_MAX_XFER_SIZE
13775
    {"PC_REC_MAX_XFER_SIZE",    _PC_REC_MAX_XFER_SIZE},
13776
#endif
13777
#ifdef _PC_REC_MIN_XFER_SIZE
13778
    {"PC_REC_MIN_XFER_SIZE",    _PC_REC_MIN_XFER_SIZE},
13779
#endif
13780
#ifdef _PC_REC_XFER_ALIGN
13781
    {"PC_REC_XFER_ALIGN",   _PC_REC_XFER_ALIGN},
13782
#endif
13783
#ifdef _PC_SYMLINK_MAX
13784
    {"PC_SYMLINK_MAX",  _PC_SYMLINK_MAX},
13785
#endif
13786
#ifdef _PC_XATTR_ENABLED
13787
    {"PC_XATTR_ENABLED",    _PC_XATTR_ENABLED},
13788
#endif
13789
#ifdef _PC_XATTR_EXISTS
13790
    {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
13791
#endif
13792
#ifdef _PC_TIMESTAMP_RESOLUTION
13793
    {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
13794
#endif
13795
};
13796
#endif
13797
13798
13799
#ifdef HAVE_FPATHCONF
13800
/*[clinic input]
13801
os.fpathconf -> long
13802
13803
    fd: fildes
13804
    name: confname(table="pathconf_names")
13805
    /
13806
13807
Return the configuration limit name for the file descriptor fd.
13808
13809
If there is no limit, return -1.
13810
[clinic start generated code]*/
13811
13812
static long
13813
os_fpathconf_impl(PyObject *module, int fd, int name)
13814
/*[clinic end generated code: output=d5b7042425fc3e21 input=023d44589c9ed6aa]*/
13815
0
{
13816
0
    long limit;
13817
13818
0
    errno = 0;
13819
0
    limit = fpathconf(fd, name);
13820
0
    if (limit == -1 && errno != 0)
13821
0
        posix_error();
13822
13823
0
    return limit;
13824
0
}
13825
#endif /* HAVE_FPATHCONF */
13826
13827
13828
#ifdef HAVE_PATHCONF
13829
/*[clinic input]
13830
os.pathconf -> long
13831
    path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
13832
    name: confname(table="pathconf_names")
13833
13834
Return the configuration limit name for the file or directory path.
13835
13836
If there is no limit, return -1.
13837
On some platforms, path may also be specified as an open file descriptor.
13838
  If this functionality is unavailable, using it raises an exception.
13839
[clinic start generated code]*/
13840
13841
static long
13842
os_pathconf_impl(PyObject *module, path_t *path, int name)
13843
/*[clinic end generated code: output=5bedee35b293a089 input=6f6072f57b10c787]*/
13844
0
{
13845
0
    long limit;
13846
13847
0
    errno = 0;
13848
0
#ifdef HAVE_FPATHCONF
13849
0
    if (path->fd != -1)
13850
0
        limit = fpathconf(path->fd, name);
13851
0
    else
13852
0
#endif
13853
0
        limit = pathconf(path->narrow, name);
13854
0
    if (limit == -1 && errno != 0) {
13855
0
        if (errno == EINVAL)
13856
            /* could be a path or name problem */
13857
0
            posix_error();
13858
0
        else
13859
0
            path_error(path);
13860
0
    }
13861
13862
0
    return limit;
13863
0
}
13864
#endif /* HAVE_PATHCONF */
13865
13866
#ifdef HAVE_CONFSTR
13867
static struct constdef posix_constants_confstr[] = {
13868
#ifdef _CS_ARCHITECTURE
13869
    {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
13870
#endif
13871
#ifdef _CS_GNU_LIBC_VERSION
13872
    {"CS_GNU_LIBC_VERSION",     _CS_GNU_LIBC_VERSION},
13873
#endif
13874
#ifdef _CS_GNU_LIBPTHREAD_VERSION
13875
    {"CS_GNU_LIBPTHREAD_VERSION",       _CS_GNU_LIBPTHREAD_VERSION},
13876
#endif
13877
#ifdef _CS_HOSTNAME
13878
    {"CS_HOSTNAME",     _CS_HOSTNAME},
13879
#endif
13880
#ifdef _CS_HW_PROVIDER
13881
    {"CS_HW_PROVIDER",  _CS_HW_PROVIDER},
13882
#endif
13883
#ifdef _CS_HW_SERIAL
13884
    {"CS_HW_SERIAL",    _CS_HW_SERIAL},
13885
#endif
13886
#ifdef _CS_INITTAB_NAME
13887
    {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
13888
#endif
13889
#ifdef _CS_LFS64_CFLAGS
13890
    {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
13891
#endif
13892
#ifdef _CS_LFS64_LDFLAGS
13893
    {"CS_LFS64_LDFLAGS",        _CS_LFS64_LDFLAGS},
13894
#endif
13895
#ifdef _CS_LFS64_LIBS
13896
    {"CS_LFS64_LIBS",   _CS_LFS64_LIBS},
13897
#endif
13898
#ifdef _CS_LFS64_LINTFLAGS
13899
    {"CS_LFS64_LINTFLAGS",      _CS_LFS64_LINTFLAGS},
13900
#endif
13901
#ifdef _CS_LFS_CFLAGS
13902
    {"CS_LFS_CFLAGS",   _CS_LFS_CFLAGS},
13903
#endif
13904
#ifdef _CS_LFS_LDFLAGS
13905
    {"CS_LFS_LDFLAGS",  _CS_LFS_LDFLAGS},
13906
#endif
13907
#ifdef _CS_LFS_LIBS
13908
    {"CS_LFS_LIBS",     _CS_LFS_LIBS},
13909
#endif
13910
#ifdef _CS_LFS_LINTFLAGS
13911
    {"CS_LFS_LINTFLAGS",        _CS_LFS_LINTFLAGS},
13912
#endif
13913
#ifdef _CS_MACHINE
13914
    {"CS_MACHINE",      _CS_MACHINE},
13915
#endif
13916
#ifdef _CS_PATH
13917
    {"CS_PATH", _CS_PATH},
13918
#endif
13919
#ifdef _CS_RELEASE
13920
    {"CS_RELEASE",      _CS_RELEASE},
13921
#endif
13922
#ifdef _CS_SRPC_DOMAIN
13923
    {"CS_SRPC_DOMAIN",  _CS_SRPC_DOMAIN},
13924
#endif
13925
#ifdef _CS_SYSNAME
13926
    {"CS_SYSNAME",      _CS_SYSNAME},
13927
#endif
13928
#ifdef _CS_VERSION
13929
    {"CS_VERSION",      _CS_VERSION},
13930
#endif
13931
#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
13932
    {"CS_XBS5_ILP32_OFF32_CFLAGS",      _CS_XBS5_ILP32_OFF32_CFLAGS},
13933
#endif
13934
#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
13935
    {"CS_XBS5_ILP32_OFF32_LDFLAGS",     _CS_XBS5_ILP32_OFF32_LDFLAGS},
13936
#endif
13937
#ifdef _CS_XBS5_ILP32_OFF32_LIBS
13938
    {"CS_XBS5_ILP32_OFF32_LIBS",        _CS_XBS5_ILP32_OFF32_LIBS},
13939
#endif
13940
#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
13941
    {"CS_XBS5_ILP32_OFF32_LINTFLAGS",   _CS_XBS5_ILP32_OFF32_LINTFLAGS},
13942
#endif
13943
#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
13944
    {"CS_XBS5_ILP32_OFFBIG_CFLAGS",     _CS_XBS5_ILP32_OFFBIG_CFLAGS},
13945
#endif
13946
#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
13947
    {"CS_XBS5_ILP32_OFFBIG_LDFLAGS",    _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
13948
#endif
13949
#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
13950
    {"CS_XBS5_ILP32_OFFBIG_LIBS",       _CS_XBS5_ILP32_OFFBIG_LIBS},
13951
#endif
13952
#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
13953
    {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS",  _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
13954
#endif
13955
#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
13956
    {"CS_XBS5_LP64_OFF64_CFLAGS",       _CS_XBS5_LP64_OFF64_CFLAGS},
13957
#endif
13958
#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
13959
    {"CS_XBS5_LP64_OFF64_LDFLAGS",      _CS_XBS5_LP64_OFF64_LDFLAGS},
13960
#endif
13961
#ifdef _CS_XBS5_LP64_OFF64_LIBS
13962
    {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
13963
#endif
13964
#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
13965
    {"CS_XBS5_LP64_OFF64_LINTFLAGS",    _CS_XBS5_LP64_OFF64_LINTFLAGS},
13966
#endif
13967
#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
13968
    {"CS_XBS5_LPBIG_OFFBIG_CFLAGS",     _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
13969
#endif
13970
#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
13971
    {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS",    _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
13972
#endif
13973
#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
13974
    {"CS_XBS5_LPBIG_OFFBIG_LIBS",       _CS_XBS5_LPBIG_OFFBIG_LIBS},
13975
#endif
13976
#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
13977
    {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS",  _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
13978
#endif
13979
#ifdef _MIPS_CS_AVAIL_PROCESSORS
13980
    {"MIPS_CS_AVAIL_PROCESSORS",        _MIPS_CS_AVAIL_PROCESSORS},
13981
#endif
13982
#ifdef _MIPS_CS_BASE
13983
    {"MIPS_CS_BASE",    _MIPS_CS_BASE},
13984
#endif
13985
#ifdef _MIPS_CS_HOSTID
13986
    {"MIPS_CS_HOSTID",  _MIPS_CS_HOSTID},
13987
#endif
13988
#ifdef _MIPS_CS_HW_NAME
13989
    {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
13990
#endif
13991
#ifdef _MIPS_CS_NUM_PROCESSORS
13992
    {"MIPS_CS_NUM_PROCESSORS",  _MIPS_CS_NUM_PROCESSORS},
13993
#endif
13994
#ifdef _MIPS_CS_OSREL_MAJ
13995
    {"MIPS_CS_OSREL_MAJ",       _MIPS_CS_OSREL_MAJ},
13996
#endif
13997
#ifdef _MIPS_CS_OSREL_MIN
13998
    {"MIPS_CS_OSREL_MIN",       _MIPS_CS_OSREL_MIN},
13999
#endif
14000
#ifdef _MIPS_CS_OSREL_PATCH
14001
    {"MIPS_CS_OSREL_PATCH",     _MIPS_CS_OSREL_PATCH},
14002
#endif
14003
#ifdef _MIPS_CS_OS_NAME
14004
    {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
14005
#endif
14006
#ifdef _MIPS_CS_OS_PROVIDER
14007
    {"MIPS_CS_OS_PROVIDER",     _MIPS_CS_OS_PROVIDER},
14008
#endif
14009
#ifdef _MIPS_CS_PROCESSORS
14010
    {"MIPS_CS_PROCESSORS",      _MIPS_CS_PROCESSORS},
14011
#endif
14012
#ifdef _MIPS_CS_SERIAL
14013
    {"MIPS_CS_SERIAL",  _MIPS_CS_SERIAL},
14014
#endif
14015
#ifdef _MIPS_CS_VENDOR
14016
    {"MIPS_CS_VENDOR",  _MIPS_CS_VENDOR},
14017
#endif
14018
};
14019
14020
14021
/*[clinic input]
14022
os.confstr
14023
14024
    name: confname(table="confstr_names")
14025
    /
14026
14027
Return a string-valued system configuration variable.
14028
[clinic start generated code]*/
14029
14030
static PyObject *
14031
os_confstr_impl(PyObject *module, int name)
14032
/*[clinic end generated code: output=bfb0b1b1e49b9383 input=4c6ffca2837ec959]*/
14033
0
{
14034
0
    PyObject *result = NULL;
14035
0
    char buffer[255];
14036
0
    size_t len;
14037
14038
0
    errno = 0;
14039
0
    len = confstr(name, buffer, sizeof(buffer));
14040
0
    if (len == 0) {
14041
0
        if (errno) {
14042
0
            posix_error();
14043
0
            return NULL;
14044
0
        }
14045
0
        else {
14046
0
            Py_RETURN_NONE;
14047
0
        }
14048
0
    }
14049
14050
0
    if (len >= sizeof(buffer)) {
14051
0
        size_t len2;
14052
0
        char *buf = PyMem_Malloc(len);
14053
0
        if (buf == NULL)
14054
0
            return PyErr_NoMemory();
14055
0
        len2 = confstr(name, buf, len);
14056
0
        assert(len == len2);
14057
0
        result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
14058
0
        PyMem_Free(buf);
14059
0
    }
14060
0
    else
14061
0
        result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
14062
0
    return result;
14063
0
}
14064
#endif /* HAVE_CONFSTR */
14065
14066
14067
#ifdef HAVE_SYSCONF
14068
static struct constdef posix_constants_sysconf[] = {
14069
#ifdef _SC_2_CHAR_TERM
14070
    {"SC_2_CHAR_TERM",  _SC_2_CHAR_TERM},
14071
#endif
14072
#ifdef _SC_2_C_BIND
14073
    {"SC_2_C_BIND",     _SC_2_C_BIND},
14074
#endif
14075
#ifdef _SC_2_C_DEV
14076
    {"SC_2_C_DEV",      _SC_2_C_DEV},
14077
#endif
14078
#ifdef _SC_2_C_VERSION
14079
    {"SC_2_C_VERSION",  _SC_2_C_VERSION},
14080
#endif
14081
#ifdef _SC_2_FORT_DEV
14082
    {"SC_2_FORT_DEV",   _SC_2_FORT_DEV},
14083
#endif
14084
#ifdef _SC_2_FORT_RUN
14085
    {"SC_2_FORT_RUN",   _SC_2_FORT_RUN},
14086
#endif
14087
#ifdef _SC_2_LOCALEDEF
14088
    {"SC_2_LOCALEDEF",  _SC_2_LOCALEDEF},
14089
#endif
14090
#ifdef _SC_2_SW_DEV
14091
    {"SC_2_SW_DEV",     _SC_2_SW_DEV},
14092
#endif
14093
#ifdef _SC_2_UPE
14094
    {"SC_2_UPE",        _SC_2_UPE},
14095
#endif
14096
#ifdef _SC_2_VERSION
14097
    {"SC_2_VERSION",    _SC_2_VERSION},
14098
#endif
14099
#ifdef _SC_ABI_ASYNCHRONOUS_IO
14100
    {"SC_ABI_ASYNCHRONOUS_IO",  _SC_ABI_ASYNCHRONOUS_IO},
14101
#endif
14102
#ifdef _SC_ACL
14103
    {"SC_ACL",  _SC_ACL},
14104
#endif
14105
#ifdef _SC_AIO_LISTIO_MAX
14106
    {"SC_AIO_LISTIO_MAX",       _SC_AIO_LISTIO_MAX},
14107
#endif
14108
#ifdef _SC_AIO_MAX
14109
    {"SC_AIO_MAX",      _SC_AIO_MAX},
14110
#endif
14111
#ifdef _SC_AIO_PRIO_DELTA_MAX
14112
    {"SC_AIO_PRIO_DELTA_MAX",   _SC_AIO_PRIO_DELTA_MAX},
14113
#endif
14114
#ifdef _SC_ARG_MAX
14115
    {"SC_ARG_MAX",      _SC_ARG_MAX},
14116
#endif
14117
#ifdef _SC_ASYNCHRONOUS_IO
14118
    {"SC_ASYNCHRONOUS_IO",      _SC_ASYNCHRONOUS_IO},
14119
#endif
14120
#ifdef _SC_ATEXIT_MAX
14121
    {"SC_ATEXIT_MAX",   _SC_ATEXIT_MAX},
14122
#endif
14123
#ifdef _SC_AUDIT
14124
    {"SC_AUDIT",        _SC_AUDIT},
14125
#endif
14126
#ifdef _SC_AVPHYS_PAGES
14127
    {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
14128
#endif
14129
#ifdef _SC_BC_BASE_MAX
14130
    {"SC_BC_BASE_MAX",  _SC_BC_BASE_MAX},
14131
#endif
14132
#ifdef _SC_BC_DIM_MAX
14133
    {"SC_BC_DIM_MAX",   _SC_BC_DIM_MAX},
14134
#endif
14135
#ifdef _SC_BC_SCALE_MAX
14136
    {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
14137
#endif
14138
#ifdef _SC_BC_STRING_MAX
14139
    {"SC_BC_STRING_MAX",        _SC_BC_STRING_MAX},
14140
#endif
14141
#ifdef _SC_CAP
14142
    {"SC_CAP",  _SC_CAP},
14143
#endif
14144
#ifdef _SC_CHARCLASS_NAME_MAX
14145
    {"SC_CHARCLASS_NAME_MAX",   _SC_CHARCLASS_NAME_MAX},
14146
#endif
14147
#ifdef _SC_CHAR_BIT
14148
    {"SC_CHAR_BIT",     _SC_CHAR_BIT},
14149
#endif
14150
#ifdef _SC_CHAR_MAX
14151
    {"SC_CHAR_MAX",     _SC_CHAR_MAX},
14152
#endif
14153
#ifdef _SC_CHAR_MIN
14154
    {"SC_CHAR_MIN",     _SC_CHAR_MIN},
14155
#endif
14156
#ifdef _SC_CHILD_MAX
14157
    {"SC_CHILD_MAX",    _SC_CHILD_MAX},
14158
#endif
14159
#ifdef _SC_CLK_TCK
14160
    {"SC_CLK_TCK",      _SC_CLK_TCK},
14161
#endif
14162
#ifdef _SC_COHER_BLKSZ
14163
    {"SC_COHER_BLKSZ",  _SC_COHER_BLKSZ},
14164
#endif
14165
#ifdef _SC_COLL_WEIGHTS_MAX
14166
    {"SC_COLL_WEIGHTS_MAX",     _SC_COLL_WEIGHTS_MAX},
14167
#endif
14168
#ifdef _SC_DCACHE_ASSOC
14169
    {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
14170
#endif
14171
#ifdef _SC_DCACHE_BLKSZ
14172
    {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
14173
#endif
14174
#ifdef _SC_DCACHE_LINESZ
14175
    {"SC_DCACHE_LINESZ",        _SC_DCACHE_LINESZ},
14176
#endif
14177
#ifdef _SC_DCACHE_SZ
14178
    {"SC_DCACHE_SZ",    _SC_DCACHE_SZ},
14179
#endif
14180
#ifdef _SC_DCACHE_TBLKSZ
14181
    {"SC_DCACHE_TBLKSZ",        _SC_DCACHE_TBLKSZ},
14182
#endif
14183
#ifdef _SC_DELAYTIMER_MAX
14184
    {"SC_DELAYTIMER_MAX",       _SC_DELAYTIMER_MAX},
14185
#endif
14186
#ifdef _SC_EQUIV_CLASS_MAX
14187
    {"SC_EQUIV_CLASS_MAX",      _SC_EQUIV_CLASS_MAX},
14188
#endif
14189
#ifdef _SC_EXPR_NEST_MAX
14190
    {"SC_EXPR_NEST_MAX",        _SC_EXPR_NEST_MAX},
14191
#endif
14192
#ifdef _SC_FSYNC
14193
    {"SC_FSYNC",        _SC_FSYNC},
14194
#endif
14195
#ifdef _SC_GETGR_R_SIZE_MAX
14196
    {"SC_GETGR_R_SIZE_MAX",     _SC_GETGR_R_SIZE_MAX},
14197
#endif
14198
#ifdef _SC_GETPW_R_SIZE_MAX
14199
    {"SC_GETPW_R_SIZE_MAX",     _SC_GETPW_R_SIZE_MAX},
14200
#endif
14201
#ifdef _SC_ICACHE_ASSOC
14202
    {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
14203
#endif
14204
#ifdef _SC_ICACHE_BLKSZ
14205
    {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
14206
#endif
14207
#ifdef _SC_ICACHE_LINESZ
14208
    {"SC_ICACHE_LINESZ",        _SC_ICACHE_LINESZ},
14209
#endif
14210
#ifdef _SC_ICACHE_SZ
14211
    {"SC_ICACHE_SZ",    _SC_ICACHE_SZ},
14212
#endif
14213
#ifdef _SC_INF
14214
    {"SC_INF",  _SC_INF},
14215
#endif
14216
#ifdef _SC_INT_MAX
14217
    {"SC_INT_MAX",      _SC_INT_MAX},
14218
#endif
14219
#ifdef _SC_INT_MIN
14220
    {"SC_INT_MIN",      _SC_INT_MIN},
14221
#endif
14222
#ifdef _SC_IOV_MAX
14223
    {"SC_IOV_MAX",      _SC_IOV_MAX},
14224
#endif
14225
#ifdef _SC_IP_SECOPTS
14226
    {"SC_IP_SECOPTS",   _SC_IP_SECOPTS},
14227
#endif
14228
#ifdef _SC_JOB_CONTROL
14229
    {"SC_JOB_CONTROL",  _SC_JOB_CONTROL},
14230
#endif
14231
#ifdef _SC_KERN_POINTERS
14232
    {"SC_KERN_POINTERS",        _SC_KERN_POINTERS},
14233
#endif
14234
#ifdef _SC_KERN_SIM
14235
    {"SC_KERN_SIM",     _SC_KERN_SIM},
14236
#endif
14237
#ifdef _SC_LINE_MAX
14238
    {"SC_LINE_MAX",     _SC_LINE_MAX},
14239
#endif
14240
#ifdef _SC_LOGIN_NAME_MAX
14241
    {"SC_LOGIN_NAME_MAX",       _SC_LOGIN_NAME_MAX},
14242
#endif
14243
#ifdef _SC_LOGNAME_MAX
14244
    {"SC_LOGNAME_MAX",  _SC_LOGNAME_MAX},
14245
#endif
14246
#ifdef _SC_LONG_BIT
14247
    {"SC_LONG_BIT",     _SC_LONG_BIT},
14248
#endif
14249
#ifdef _SC_MAC
14250
    {"SC_MAC",  _SC_MAC},
14251
#endif
14252
#ifdef _SC_MAPPED_FILES
14253
    {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
14254
#endif
14255
#ifdef _SC_MAXPID
14256
    {"SC_MAXPID",       _SC_MAXPID},
14257
#endif
14258
#ifdef _SC_MB_LEN_MAX
14259
    {"SC_MB_LEN_MAX",   _SC_MB_LEN_MAX},
14260
#endif
14261
#ifdef _SC_MEMLOCK
14262
    {"SC_MEMLOCK",      _SC_MEMLOCK},
14263
#endif
14264
#ifdef _SC_MEMLOCK_RANGE
14265
    {"SC_MEMLOCK_RANGE",        _SC_MEMLOCK_RANGE},
14266
#endif
14267
#ifdef _SC_MEMORY_PROTECTION
14268
    {"SC_MEMORY_PROTECTION",    _SC_MEMORY_PROTECTION},
14269
#endif
14270
#ifdef _SC_MESSAGE_PASSING
14271
    {"SC_MESSAGE_PASSING",      _SC_MESSAGE_PASSING},
14272
#endif
14273
#ifdef _SC_MMAP_FIXED_ALIGNMENT
14274
    {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
14275
#endif
14276
#ifdef _SC_MQ_OPEN_MAX
14277
    {"SC_MQ_OPEN_MAX",  _SC_MQ_OPEN_MAX},
14278
#endif
14279
#ifdef _SC_MQ_PRIO_MAX
14280
    {"SC_MQ_PRIO_MAX",  _SC_MQ_PRIO_MAX},
14281
#endif
14282
#ifdef _SC_NACLS_MAX
14283
    {"SC_NACLS_MAX",    _SC_NACLS_MAX},
14284
#endif
14285
#ifdef _SC_NGROUPS_MAX
14286
    {"SC_NGROUPS_MAX",  _SC_NGROUPS_MAX},
14287
#endif
14288
#ifdef _SC_NL_ARGMAX
14289
    {"SC_NL_ARGMAX",    _SC_NL_ARGMAX},
14290
#endif
14291
#ifdef _SC_NL_LANGMAX
14292
    {"SC_NL_LANGMAX",   _SC_NL_LANGMAX},
14293
#endif
14294
#ifdef _SC_NL_MSGMAX
14295
    {"SC_NL_MSGMAX",    _SC_NL_MSGMAX},
14296
#endif
14297
#ifdef _SC_NL_NMAX
14298
    {"SC_NL_NMAX",      _SC_NL_NMAX},
14299
#endif
14300
#ifdef _SC_NL_SETMAX
14301
    {"SC_NL_SETMAX",    _SC_NL_SETMAX},
14302
#endif
14303
#ifdef _SC_NL_TEXTMAX
14304
    {"SC_NL_TEXTMAX",   _SC_NL_TEXTMAX},
14305
#endif
14306
#ifdef _SC_NPROCESSORS_CONF
14307
    {"SC_NPROCESSORS_CONF",     _SC_NPROCESSORS_CONF},
14308
#endif
14309
#ifdef _SC_NPROCESSORS_ONLN
14310
    {"SC_NPROCESSORS_ONLN",     _SC_NPROCESSORS_ONLN},
14311
#endif
14312
#ifdef _SC_NPROC_CONF
14313
    {"SC_NPROC_CONF",   _SC_NPROC_CONF},
14314
#endif
14315
#ifdef _SC_NPROC_ONLN
14316
    {"SC_NPROC_ONLN",   _SC_NPROC_ONLN},
14317
#endif
14318
#ifdef _SC_NZERO
14319
    {"SC_NZERO",        _SC_NZERO},
14320
#endif
14321
#ifdef _SC_OPEN_MAX
14322
    {"SC_OPEN_MAX",     _SC_OPEN_MAX},
14323
#endif
14324
#ifdef _SC_PAGESIZE
14325
    {"SC_PAGESIZE",     _SC_PAGESIZE},
14326
#endif
14327
#ifdef _SC_PAGE_SIZE
14328
    {"SC_PAGE_SIZE",    _SC_PAGE_SIZE},
14329
#endif
14330
#ifdef _SC_AIX_REALMEM
14331
    {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
14332
#endif
14333
#ifdef _SC_PASS_MAX
14334
    {"SC_PASS_MAX",     _SC_PASS_MAX},
14335
#endif
14336
#ifdef _SC_PHYS_PAGES
14337
    {"SC_PHYS_PAGES",   _SC_PHYS_PAGES},
14338
#endif
14339
#ifdef _SC_PII
14340
    {"SC_PII",  _SC_PII},
14341
#endif
14342
#ifdef _SC_PII_INTERNET
14343
    {"SC_PII_INTERNET", _SC_PII_INTERNET},
14344
#endif
14345
#ifdef _SC_PII_INTERNET_DGRAM
14346
    {"SC_PII_INTERNET_DGRAM",   _SC_PII_INTERNET_DGRAM},
14347
#endif
14348
#ifdef _SC_PII_INTERNET_STREAM
14349
    {"SC_PII_INTERNET_STREAM",  _SC_PII_INTERNET_STREAM},
14350
#endif
14351
#ifdef _SC_PII_OSI
14352
    {"SC_PII_OSI",      _SC_PII_OSI},
14353
#endif
14354
#ifdef _SC_PII_OSI_CLTS
14355
    {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
14356
#endif
14357
#ifdef _SC_PII_OSI_COTS
14358
    {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
14359
#endif
14360
#ifdef _SC_PII_OSI_M
14361
    {"SC_PII_OSI_M",    _SC_PII_OSI_M},
14362
#endif
14363
#ifdef _SC_PII_SOCKET
14364
    {"SC_PII_SOCKET",   _SC_PII_SOCKET},
14365
#endif
14366
#ifdef _SC_PII_XTI
14367
    {"SC_PII_XTI",      _SC_PII_XTI},
14368
#endif
14369
#ifdef _SC_POLL
14370
    {"SC_POLL", _SC_POLL},
14371
#endif
14372
#ifdef _SC_PRIORITIZED_IO
14373
    {"SC_PRIORITIZED_IO",       _SC_PRIORITIZED_IO},
14374
#endif
14375
#ifdef _SC_PRIORITY_SCHEDULING
14376
    {"SC_PRIORITY_SCHEDULING",  _SC_PRIORITY_SCHEDULING},
14377
#endif
14378
#ifdef _SC_REALTIME_SIGNALS
14379
    {"SC_REALTIME_SIGNALS",     _SC_REALTIME_SIGNALS},
14380
#endif
14381
#ifdef _SC_RE_DUP_MAX
14382
    {"SC_RE_DUP_MAX",   _SC_RE_DUP_MAX},
14383
#endif
14384
#ifdef _SC_RTSIG_MAX
14385
    {"SC_RTSIG_MAX",    _SC_RTSIG_MAX},
14386
#endif
14387
#ifdef _SC_SAVED_IDS
14388
    {"SC_SAVED_IDS",    _SC_SAVED_IDS},
14389
#endif
14390
#ifdef _SC_SCHAR_MAX
14391
    {"SC_SCHAR_MAX",    _SC_SCHAR_MAX},
14392
#endif
14393
#ifdef _SC_SCHAR_MIN
14394
    {"SC_SCHAR_MIN",    _SC_SCHAR_MIN},
14395
#endif
14396
#ifdef _SC_SELECT
14397
    {"SC_SELECT",       _SC_SELECT},
14398
#endif
14399
#ifdef _SC_SEMAPHORES
14400
    {"SC_SEMAPHORES",   _SC_SEMAPHORES},
14401
#endif
14402
#ifdef _SC_SEM_NSEMS_MAX
14403
    {"SC_SEM_NSEMS_MAX",        _SC_SEM_NSEMS_MAX},
14404
#endif
14405
#ifdef _SC_SEM_VALUE_MAX
14406
    {"SC_SEM_VALUE_MAX",        _SC_SEM_VALUE_MAX},
14407
#endif
14408
#ifdef _SC_SHARED_MEMORY_OBJECTS
14409
    {"SC_SHARED_MEMORY_OBJECTS",        _SC_SHARED_MEMORY_OBJECTS},
14410
#endif
14411
#ifdef _SC_SHRT_MAX
14412
    {"SC_SHRT_MAX",     _SC_SHRT_MAX},
14413
#endif
14414
#ifdef _SC_SHRT_MIN
14415
    {"SC_SHRT_MIN",     _SC_SHRT_MIN},
14416
#endif
14417
#ifdef _SC_SIGQUEUE_MAX
14418
    {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
14419
#endif
14420
#ifdef _SC_SIGRT_MAX
14421
    {"SC_SIGRT_MAX",    _SC_SIGRT_MAX},
14422
#endif
14423
#ifdef _SC_SIGRT_MIN
14424
    {"SC_SIGRT_MIN",    _SC_SIGRT_MIN},
14425
#endif
14426
#ifdef _SC_SOFTPOWER
14427
    {"SC_SOFTPOWER",    _SC_SOFTPOWER},
14428
#endif
14429
#ifdef _SC_SPLIT_CACHE
14430
    {"SC_SPLIT_CACHE",  _SC_SPLIT_CACHE},
14431
#endif
14432
#ifdef _SC_SSIZE_MAX
14433
    {"SC_SSIZE_MAX",    _SC_SSIZE_MAX},
14434
#endif
14435
#ifdef _SC_STACK_PROT
14436
    {"SC_STACK_PROT",   _SC_STACK_PROT},
14437
#endif
14438
#ifdef _SC_STREAM_MAX
14439
    {"SC_STREAM_MAX",   _SC_STREAM_MAX},
14440
#endif
14441
#ifdef _SC_SYNCHRONIZED_IO
14442
    {"SC_SYNCHRONIZED_IO",      _SC_SYNCHRONIZED_IO},
14443
#endif
14444
#ifdef _SC_THREADS
14445
    {"SC_THREADS",      _SC_THREADS},
14446
#endif
14447
#ifdef _SC_THREAD_ATTR_STACKADDR
14448
    {"SC_THREAD_ATTR_STACKADDR",        _SC_THREAD_ATTR_STACKADDR},
14449
#endif
14450
#ifdef _SC_THREAD_ATTR_STACKSIZE
14451
    {"SC_THREAD_ATTR_STACKSIZE",        _SC_THREAD_ATTR_STACKSIZE},
14452
#endif
14453
#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
14454
    {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
14455
#endif
14456
#ifdef _SC_THREAD_KEYS_MAX
14457
    {"SC_THREAD_KEYS_MAX",      _SC_THREAD_KEYS_MAX},
14458
#endif
14459
#ifdef _SC_THREAD_PRIORITY_SCHEDULING
14460
    {"SC_THREAD_PRIORITY_SCHEDULING",   _SC_THREAD_PRIORITY_SCHEDULING},
14461
#endif
14462
#ifdef _SC_THREAD_PRIO_INHERIT
14463
    {"SC_THREAD_PRIO_INHERIT",  _SC_THREAD_PRIO_INHERIT},
14464
#endif
14465
#ifdef _SC_THREAD_PRIO_PROTECT
14466
    {"SC_THREAD_PRIO_PROTECT",  _SC_THREAD_PRIO_PROTECT},
14467
#endif
14468
#ifdef _SC_THREAD_PROCESS_SHARED
14469
    {"SC_THREAD_PROCESS_SHARED",        _SC_THREAD_PROCESS_SHARED},
14470
#endif
14471
#ifdef _SC_THREAD_SAFE_FUNCTIONS
14472
    {"SC_THREAD_SAFE_FUNCTIONS",        _SC_THREAD_SAFE_FUNCTIONS},
14473
#endif
14474
#ifdef _SC_THREAD_STACK_MIN
14475
    {"SC_THREAD_STACK_MIN",     _SC_THREAD_STACK_MIN},
14476
#endif
14477
#ifdef _SC_THREAD_THREADS_MAX
14478
    {"SC_THREAD_THREADS_MAX",   _SC_THREAD_THREADS_MAX},
14479
#endif
14480
#ifdef _SC_TIMERS
14481
    {"SC_TIMERS",       _SC_TIMERS},
14482
#endif
14483
#ifdef _SC_TIMER_MAX
14484
    {"SC_TIMER_MAX",    _SC_TIMER_MAX},
14485
#endif
14486
#ifdef _SC_TTY_NAME_MAX
14487
    {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
14488
#endif
14489
#ifdef _SC_TZNAME_MAX
14490
    {"SC_TZNAME_MAX",   _SC_TZNAME_MAX},
14491
#endif
14492
#ifdef _SC_T_IOV_MAX
14493
    {"SC_T_IOV_MAX",    _SC_T_IOV_MAX},
14494
#endif
14495
#ifdef _SC_UCHAR_MAX
14496
    {"SC_UCHAR_MAX",    _SC_UCHAR_MAX},
14497
#endif
14498
#ifdef _SC_UINT_MAX
14499
    {"SC_UINT_MAX",     _SC_UINT_MAX},
14500
#endif
14501
#ifdef _SC_UIO_MAXIOV
14502
    {"SC_UIO_MAXIOV",   _SC_UIO_MAXIOV},
14503
#endif
14504
#ifdef _SC_ULONG_MAX
14505
    {"SC_ULONG_MAX",    _SC_ULONG_MAX},
14506
#endif
14507
#ifdef _SC_USHRT_MAX
14508
    {"SC_USHRT_MAX",    _SC_USHRT_MAX},
14509
#endif
14510
#ifdef _SC_VERSION
14511
    {"SC_VERSION",      _SC_VERSION},
14512
#endif
14513
#ifdef _SC_WORD_BIT
14514
    {"SC_WORD_BIT",     _SC_WORD_BIT},
14515
#endif
14516
#ifdef _SC_XBS5_ILP32_OFF32
14517
    {"SC_XBS5_ILP32_OFF32",     _SC_XBS5_ILP32_OFF32},
14518
#endif
14519
#ifdef _SC_XBS5_ILP32_OFFBIG
14520
    {"SC_XBS5_ILP32_OFFBIG",    _SC_XBS5_ILP32_OFFBIG},
14521
#endif
14522
#ifdef _SC_XBS5_LP64_OFF64
14523
    {"SC_XBS5_LP64_OFF64",      _SC_XBS5_LP64_OFF64},
14524
#endif
14525
#ifdef _SC_XBS5_LPBIG_OFFBIG
14526
    {"SC_XBS5_LPBIG_OFFBIG",    _SC_XBS5_LPBIG_OFFBIG},
14527
#endif
14528
#ifdef _SC_XOPEN_CRYPT
14529
    {"SC_XOPEN_CRYPT",  _SC_XOPEN_CRYPT},
14530
#endif
14531
#ifdef _SC_XOPEN_ENH_I18N
14532
    {"SC_XOPEN_ENH_I18N",       _SC_XOPEN_ENH_I18N},
14533
#endif
14534
#ifdef _SC_XOPEN_LEGACY
14535
    {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
14536
#endif
14537
#ifdef _SC_XOPEN_REALTIME
14538
    {"SC_XOPEN_REALTIME",       _SC_XOPEN_REALTIME},
14539
#endif
14540
#ifdef _SC_XOPEN_REALTIME_THREADS
14541
    {"SC_XOPEN_REALTIME_THREADS",       _SC_XOPEN_REALTIME_THREADS},
14542
#endif
14543
#ifdef _SC_XOPEN_SHM
14544
    {"SC_XOPEN_SHM",    _SC_XOPEN_SHM},
14545
#endif
14546
#ifdef _SC_XOPEN_UNIX
14547
    {"SC_XOPEN_UNIX",   _SC_XOPEN_UNIX},
14548
#endif
14549
#ifdef _SC_XOPEN_VERSION
14550
    {"SC_XOPEN_VERSION",        _SC_XOPEN_VERSION},
14551
#endif
14552
#ifdef _SC_XOPEN_XCU_VERSION
14553
    {"SC_XOPEN_XCU_VERSION",    _SC_XOPEN_XCU_VERSION},
14554
#endif
14555
#ifdef _SC_XOPEN_XPG2
14556
    {"SC_XOPEN_XPG2",   _SC_XOPEN_XPG2},
14557
#endif
14558
#ifdef _SC_XOPEN_XPG3
14559
    {"SC_XOPEN_XPG3",   _SC_XOPEN_XPG3},
14560
#endif
14561
#ifdef _SC_XOPEN_XPG4
14562
    {"SC_XOPEN_XPG4",   _SC_XOPEN_XPG4},
14563
#endif
14564
#ifdef _SC_MINSIGSTKSZ
14565
    {"SC_MINSIGSTKSZ",   _SC_MINSIGSTKSZ},
14566
#endif
14567
};
14568
14569
14570
/*[clinic input]
14571
os.sysconf -> long
14572
    name: confname(table="sysconf_names")
14573
    /
14574
14575
Return an integer-valued system configuration variable.
14576
[clinic start generated code]*/
14577
14578
static long
14579
os_sysconf_impl(PyObject *module, int name)
14580
/*[clinic end generated code: output=3662f945fc0cc756 input=930b8f23b5d15086]*/
14581
0
{
14582
0
    long value;
14583
14584
0
    errno = 0;
14585
0
    value = sysconf(name);
14586
0
    if (value == -1 && errno != 0)
14587
0
        posix_error();
14588
0
    return value;
14589
0
}
14590
#endif /* HAVE_SYSCONF */
14591
14592
14593
static int
14594
setup_confname_table(struct constdef *table, size_t tablesize,
14595
                     const char *tablename, PyObject *module)
14596
48
{
14597
48
    PyObject *d = PyDict_New();
14598
48
    if (d == NULL)
14599
0
        return -1;
14600
14601
2.94k
    for (size_t i=0; i < tablesize; ++i) {
14602
2.89k
        PyObject *o = PyLong_FromLong(table[i].value);
14603
2.89k
        if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
14604
0
            Py_XDECREF(o);
14605
0
            Py_DECREF(d);
14606
0
            return -1;
14607
0
        }
14608
2.89k
        Py_DECREF(o);
14609
2.89k
    }
14610
48
    return PyModule_Add(module, tablename, d);
14611
48
}
14612
14613
/* Return -1 on failure, 0 on success. */
14614
static int
14615
setup_confname_tables(PyObject *module)
14616
16
{
14617
16
#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
14618
16
    if (setup_confname_table(posix_constants_pathconf,
14619
16
                             sizeof(posix_constants_pathconf)
14620
16
                               / sizeof(struct constdef),
14621
16
                             "pathconf_names", module))
14622
0
        return -1;
14623
16
#endif
14624
16
#ifdef HAVE_CONFSTR
14625
16
    if (setup_confname_table(posix_constants_confstr,
14626
16
                             sizeof(posix_constants_confstr)
14627
16
                               / sizeof(struct constdef),
14628
16
                             "confstr_names", module))
14629
0
        return -1;
14630
16
#endif
14631
16
#ifdef HAVE_SYSCONF
14632
16
    if (setup_confname_table(posix_constants_sysconf,
14633
16
                             sizeof(posix_constants_sysconf)
14634
16
                               / sizeof(struct constdef),
14635
16
                             "sysconf_names", module))
14636
0
        return -1;
14637
16
#endif
14638
16
    return 0;
14639
16
}
14640
14641
14642
/*[clinic input]
14643
os.abort
14644
14645
Abort the interpreter immediately.
14646
14647
This function 'dumps core' or otherwise fails in the hardest way possible
14648
on the hosting operating system.  This function never returns.
14649
[clinic start generated code]*/
14650
14651
static PyObject *
14652
os_abort_impl(PyObject *module)
14653
/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
14654
0
{
14655
0
    abort();
14656
    /*NOTREACHED*/
14657
#ifndef __clang__
14658
    /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
14659
       GCC emits a warning without "return NULL;" (compiler bug?), but Clang
14660
       is smarter and emits a warning on the return. */
14661
    Py_FatalError("abort() called from Python code didn't abort!");
14662
    return NULL;
14663
#endif
14664
0
}
14665
14666
#ifdef MS_WINDOWS
14667
/* Grab ShellExecute dynamically from shell32 */
14668
static int has_ShellExecute = -1;
14669
static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
14670
                                              LPCWSTR, INT);
14671
static int
14672
check_ShellExecute(void)
14673
{
14674
    HINSTANCE hShell32;
14675
14676
    /* only recheck */
14677
    if (-1 == has_ShellExecute) {
14678
        Py_BEGIN_ALLOW_THREADS
14679
        /* Security note: this call is not vulnerable to "DLL hijacking".
14680
           SHELL32 is part of "KnownDLLs" and so Windows always load
14681
           the system SHELL32.DLL, even if there is another SHELL32.DLL
14682
           in the DLL search path. */
14683
        hShell32 = LoadLibraryW(L"SHELL32");
14684
        if (hShell32) {
14685
            *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
14686
                                            "ShellExecuteW");
14687
            has_ShellExecute = Py_ShellExecuteW != NULL;
14688
        } else {
14689
            has_ShellExecute = 0;
14690
        }
14691
        Py_END_ALLOW_THREADS
14692
    }
14693
    return has_ShellExecute;
14694
}
14695
14696
14697
/*[clinic input]
14698
os.startfile
14699
    filepath: path_t
14700
    operation: Py_UNICODE = NULL
14701
    arguments: Py_UNICODE = NULL
14702
    cwd: path_t(nullable=True) = None
14703
    show_cmd: int = 1
14704
14705
Start a file with its associated application.
14706
14707
When "operation" is not specified or "open", this acts like
14708
double-clicking the file in Explorer, or giving the file name as an
14709
argument to the DOS "start" command: the file is opened with whatever
14710
application (if any) its extension is associated.
14711
When another "operation" is given, it specifies what should be done with
14712
the file.  A typical operation is "print".
14713
14714
"arguments" is passed to the application, but should be omitted if the
14715
file is a document.
14716
14717
"cwd" is the working directory for the operation. If "filepath" is
14718
relative, it will be resolved against this directory. This argument
14719
should usually be an absolute path.
14720
14721
"show_cmd" can be used to override the recommended visibility option.
14722
See the Windows ShellExecute documentation for values.
14723
14724
startfile returns as soon as the associated application is launched.
14725
There is no option to wait for the application to close, and no way
14726
to retrieve the application's exit status.
14727
14728
The filepath is relative to the current directory.  If you want to use
14729
an absolute path, make sure the first character is not a slash ("/");
14730
the underlying Win32 ShellExecute function doesn't work if it is.
14731
[clinic start generated code]*/
14732
14733
static PyObject *
14734
os_startfile_impl(PyObject *module, path_t *filepath,
14735
                  const wchar_t *operation, const wchar_t *arguments,
14736
                  path_t *cwd, int show_cmd)
14737
/*[clinic end generated code: output=1c6f2f3340e31ffa input=8248997b80669622]*/
14738
{
14739
    HINSTANCE rc;
14740
14741
    if(!check_ShellExecute()) {
14742
        /* If the OS doesn't have ShellExecute, return a
14743
           NotImplementedError. */
14744
        return PyErr_Format(PyExc_NotImplementedError,
14745
            "startfile not available on this platform");
14746
    }
14747
14748
    if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
14749
        return NULL;
14750
    }
14751
    if (PySys_Audit("os.startfile/2", "OuuOi", filepath->object, operation,
14752
                    arguments, cwd->object ? cwd->object : Py_None,
14753
                    show_cmd) < 0) {
14754
        return NULL;
14755
    }
14756
14757
    Py_BEGIN_ALLOW_THREADS
14758
    rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
14759
                          arguments, cwd->wide, show_cmd);
14760
    Py_END_ALLOW_THREADS
14761
14762
    if (rc <= (HINSTANCE)32) {
14763
        win32_error_object("startfile", filepath->object);
14764
        return NULL;
14765
    }
14766
    Py_RETURN_NONE;
14767
}
14768
#endif /* MS_WINDOWS */
14769
14770
14771
#ifdef HAVE_GETLOADAVG
14772
/*[clinic input]
14773
os.getloadavg
14774
14775
Return average recent system load information.
14776
14777
Return the number of processes in the system run queue averaged over
14778
the last 1, 5, and 15 minutes as a tuple of three floats.
14779
Raises OSError if the load average was unobtainable.
14780
[clinic start generated code]*/
14781
14782
static PyObject *
14783
os_getloadavg_impl(PyObject *module)
14784
/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
14785
0
{
14786
0
    double loadavg[3];
14787
0
    if (getloadavg(loadavg, 3)!=3) {
14788
0
        PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
14789
0
        return NULL;
14790
0
    } else
14791
0
        return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
14792
0
}
14793
#endif /* HAVE_GETLOADAVG */
14794
14795
14796
/*[clinic input]
14797
os.device_encoding
14798
    fd: int
14799
14800
Return a string describing the encoding of a terminal's file descriptor.
14801
14802
The file descriptor must be attached to a terminal.
14803
If the device is not a terminal, return None.
14804
[clinic start generated code]*/
14805
14806
static PyObject *
14807
os_device_encoding_impl(PyObject *module, int fd)
14808
/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
14809
0
{
14810
0
    return _Py_device_encoding(fd);
14811
0
}
14812
14813
14814
#ifdef HAVE_SETRESUID
14815
/*[clinic input]
14816
os.setresuid
14817
14818
    ruid: uid_t
14819
    euid: uid_t
14820
    suid: uid_t
14821
    /
14822
14823
Set the current process's real, effective, and saved user ids.
14824
[clinic start generated code]*/
14825
14826
static PyObject *
14827
os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
14828
/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
14829
0
{
14830
0
    if (setresuid(ruid, euid, suid) < 0)
14831
0
        return posix_error();
14832
0
    Py_RETURN_NONE;
14833
0
}
14834
#endif /* HAVE_SETRESUID */
14835
14836
14837
#ifdef HAVE_SETRESGID
14838
/*[clinic input]
14839
os.setresgid
14840
14841
    rgid: gid_t
14842
    egid: gid_t
14843
    sgid: gid_t
14844
    /
14845
14846
Set the current process's real, effective, and saved group ids.
14847
[clinic start generated code]*/
14848
14849
static PyObject *
14850
os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
14851
/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
14852
0
{
14853
0
    if (setresgid(rgid, egid, sgid) < 0)
14854
0
        return posix_error();
14855
0
    Py_RETURN_NONE;
14856
0
}
14857
#endif /* HAVE_SETRESGID */
14858
14859
14860
#ifdef HAVE_GETRESUID
14861
/*[clinic input]
14862
os.getresuid
14863
14864
Return a tuple of the current process's real, effective, and saved user ids.
14865
[clinic start generated code]*/
14866
14867
static PyObject *
14868
os_getresuid_impl(PyObject *module)
14869
/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
14870
0
{
14871
0
    uid_t ruid, euid, suid;
14872
0
    if (getresuid(&ruid, &euid, &suid) < 0)
14873
0
        return posix_error();
14874
0
    return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
14875
0
                                  _PyLong_FromUid(euid),
14876
0
                                  _PyLong_FromUid(suid));
14877
0
}
14878
#endif /* HAVE_GETRESUID */
14879
14880
14881
#ifdef HAVE_GETRESGID
14882
/*[clinic input]
14883
@permit_long_summary
14884
os.getresgid
14885
14886
Return a tuple of the current process's real, effective, and saved group ids.
14887
[clinic start generated code]*/
14888
14889
static PyObject *
14890
os_getresgid_impl(PyObject *module)
14891
/*[clinic end generated code: output=2719c4bfcf27fb9f input=ad9adadc86fbdb17]*/
14892
0
{
14893
0
    gid_t rgid, egid, sgid;
14894
0
    if (getresgid(&rgid, &egid, &sgid) < 0)
14895
0
        return posix_error();
14896
0
    return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
14897
0
                                  _PyLong_FromGid(egid),
14898
0
                                  _PyLong_FromGid(sgid));
14899
0
}
14900
#endif /* HAVE_GETRESGID */
14901
14902
14903
#ifdef USE_XATTRS
14904
/*[clinic input]
14905
os.getxattr
14906
14907
    path: path_t(allow_fd=True)
14908
    attribute: path_t
14909
    *
14910
    follow_symlinks: bool = True
14911
14912
Return the value of extended attribute attribute on path.
14913
14914
path may be either a string, a path-like object, or an open file descriptor.
14915
If follow_symlinks is False, and the last element of the path is a symbolic
14916
  link, getxattr will examine the symbolic link itself instead of the file
14917
  the link points to.
14918
14919
[clinic start generated code]*/
14920
14921
static PyObject *
14922
os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
14923
                 int follow_symlinks)
14924
/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
14925
0
{
14926
0
    Py_ssize_t i;
14927
0
    PyObject *buffer = NULL;
14928
14929
0
    if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
14930
0
        return NULL;
14931
14932
0
    if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
14933
0
        return NULL;
14934
0
    }
14935
14936
0
    for (i = 0; ; i++) {
14937
0
        void *ptr;
14938
0
        ssize_t result;
14939
0
        static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
14940
0
        Py_ssize_t buffer_size = buffer_sizes[i];
14941
0
        if (!buffer_size) {
14942
0
            path_error(path);
14943
0
            return NULL;
14944
0
        }
14945
0
        buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
14946
0
        if (!buffer)
14947
0
            return NULL;
14948
0
        ptr = PyBytes_AS_STRING(buffer);
14949
14950
0
        Py_BEGIN_ALLOW_THREADS;
14951
0
        if (path->fd >= 0)
14952
0
            result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
14953
0
        else if (follow_symlinks)
14954
0
            result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
14955
0
        else
14956
0
            result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
14957
0
        Py_END_ALLOW_THREADS;
14958
14959
0
        if (result < 0) {
14960
0
            if (errno == ERANGE) {
14961
0
                Py_DECREF(buffer);
14962
0
                continue;
14963
0
            }
14964
0
            path_error(path);
14965
0
            Py_DECREF(buffer);
14966
0
            return NULL;
14967
0
        }
14968
14969
0
        if (result != buffer_size) {
14970
            /* Can only shrink. */
14971
0
            _PyBytes_Resize(&buffer, result);
14972
0
        }
14973
0
        break;
14974
0
    }
14975
14976
0
    return buffer;
14977
0
}
14978
14979
14980
/*[clinic input]
14981
@permit_long_docstring_body
14982
os.setxattr
14983
14984
    path: path_t(allow_fd=True)
14985
    attribute: path_t
14986
    value: Py_buffer
14987
    flags: int = 0
14988
    *
14989
    follow_symlinks: bool = True
14990
14991
Set extended attribute attribute on path to value.
14992
14993
path may be either a string, a path-like object,  or an open file descriptor.
14994
If follow_symlinks is False, and the last element of the path is a symbolic
14995
  link, setxattr will modify the symbolic link itself instead of the file
14996
  the link points to.
14997
14998
[clinic start generated code]*/
14999
15000
static PyObject *
15001
os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
15002
                 Py_buffer *value, int flags, int follow_symlinks)
15003
/*[clinic end generated code: output=98b83f63fdde26bb input=4098e6f68699f3d7]*/
15004
0
{
15005
0
    ssize_t result;
15006
15007
0
    if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
15008
0
        return NULL;
15009
15010
0
    if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
15011
0
                    value->buf, value->len, flags) < 0) {
15012
0
        return NULL;
15013
0
    }
15014
15015
0
    Py_BEGIN_ALLOW_THREADS;
15016
0
    if (path->fd > -1)
15017
0
        result = fsetxattr(path->fd, attribute->narrow,
15018
0
                           value->buf, value->len, flags);
15019
0
    else if (follow_symlinks)
15020
0
        result = setxattr(path->narrow, attribute->narrow,
15021
0
                           value->buf, value->len, flags);
15022
0
    else
15023
0
        result = lsetxattr(path->narrow, attribute->narrow,
15024
0
                           value->buf, value->len, flags);
15025
0
    Py_END_ALLOW_THREADS;
15026
15027
0
    if (result) {
15028
0
        path_error(path);
15029
0
        return NULL;
15030
0
    }
15031
15032
0
    Py_RETURN_NONE;
15033
0
}
15034
15035
15036
/*[clinic input]
15037
os.removexattr
15038
15039
    path: path_t(allow_fd=True)
15040
    attribute: path_t
15041
    *
15042
    follow_symlinks: bool = True
15043
15044
Remove extended attribute attribute on path.
15045
15046
path may be either a string, a path-like object, or an open file descriptor.
15047
If follow_symlinks is False, and the last element of the path is a symbolic
15048
  link, removexattr will modify the symbolic link itself instead of the file
15049
  the link points to.
15050
15051
[clinic start generated code]*/
15052
15053
static PyObject *
15054
os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
15055
                    int follow_symlinks)
15056
/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
15057
0
{
15058
0
    ssize_t result;
15059
15060
0
    if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
15061
0
        return NULL;
15062
15063
0
    if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
15064
0
        return NULL;
15065
0
    }
15066
15067
0
    Py_BEGIN_ALLOW_THREADS;
15068
0
    if (path->fd > -1)
15069
0
        result = fremovexattr(path->fd, attribute->narrow);
15070
0
    else if (follow_symlinks)
15071
0
        result = removexattr(path->narrow, attribute->narrow);
15072
0
    else
15073
0
        result = lremovexattr(path->narrow, attribute->narrow);
15074
0
    Py_END_ALLOW_THREADS;
15075
15076
0
    if (result) {
15077
0
        return path_error(path);
15078
0
    }
15079
15080
0
    Py_RETURN_NONE;
15081
0
}
15082
15083
15084
/*[clinic input]
15085
@permit_long_docstring_body
15086
os.listxattr
15087
15088
    path: path_t(allow_fd=True, nullable=True) = None
15089
    *
15090
    follow_symlinks: bool = True
15091
15092
Return a list of extended attributes on path.
15093
15094
path may be either None, a string, a path-like object, or an open file descriptor.
15095
if path is None, listxattr will examine the current directory.
15096
If follow_symlinks is False, and the last element of the path is a symbolic
15097
  link, listxattr will examine the symbolic link itself instead of the file
15098
  the link points to.
15099
[clinic start generated code]*/
15100
15101
static PyObject *
15102
os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
15103
/*[clinic end generated code: output=bebdb4e2ad0ce435 input=48aa9ac8be47dea1]*/
15104
0
{
15105
0
    Py_ssize_t i;
15106
0
    PyObject *result = NULL;
15107
0
    const char *name;
15108
0
    char *buffer = NULL;
15109
15110
0
    if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
15111
0
        goto exit;
15112
15113
0
    if (PySys_Audit("os.listxattr", "(O)",
15114
0
                    path->object ? path->object : Py_None) < 0) {
15115
0
        return NULL;
15116
0
    }
15117
15118
0
    name = path->narrow ? path->narrow : ".";
15119
15120
0
    for (i = 0; ; i++) {
15121
0
        const char *start, *trace, *end;
15122
0
        ssize_t length;
15123
0
        static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
15124
0
        Py_ssize_t buffer_size = buffer_sizes[i];
15125
0
        if (!buffer_size) {
15126
            /* ERANGE */
15127
0
            path_error(path);
15128
0
            break;
15129
0
        }
15130
0
        buffer = PyMem_Malloc(buffer_size);
15131
0
        if (!buffer) {
15132
0
            PyErr_NoMemory();
15133
0
            break;
15134
0
        }
15135
15136
0
        Py_BEGIN_ALLOW_THREADS;
15137
0
        if (path->fd > -1)
15138
0
            length = flistxattr(path->fd, buffer, buffer_size);
15139
0
        else if (follow_symlinks)
15140
0
            length = listxattr(name, buffer, buffer_size);
15141
0
        else
15142
0
            length = llistxattr(name, buffer, buffer_size);
15143
0
        Py_END_ALLOW_THREADS;
15144
15145
0
        if (length < 0) {
15146
0
            if (errno == ERANGE) {
15147
0
                PyMem_Free(buffer);
15148
0
                buffer = NULL;
15149
0
                continue;
15150
0
            }
15151
0
            path_error(path);
15152
0
            break;
15153
0
        }
15154
15155
0
        result = PyList_New(0);
15156
0
        if (!result) {
15157
0
            goto exit;
15158
0
        }
15159
15160
0
        end = buffer + length;
15161
0
        for (trace = start = buffer; trace != end; trace++) {
15162
0
            if (!*trace) {
15163
0
                int error;
15164
0
                PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
15165
0
                                                                 trace - start);
15166
0
                if (!attribute) {
15167
0
                    Py_SETREF(result, NULL);
15168
0
                    goto exit;
15169
0
                }
15170
0
                error = PyList_Append(result, attribute);
15171
0
                Py_DECREF(attribute);
15172
0
                if (error) {
15173
0
                    Py_SETREF(result, NULL);
15174
0
                    goto exit;
15175
0
                }
15176
0
                start = trace + 1;
15177
0
            }
15178
0
        }
15179
0
    break;
15180
0
    }
15181
0
exit:
15182
0
    if (buffer)
15183
0
        PyMem_Free(buffer);
15184
0
    return result;
15185
0
}
15186
#endif /* USE_XATTRS */
15187
15188
15189
/*[clinic input]
15190
@permit_long_summary
15191
os.urandom
15192
15193
    size: Py_ssize_t
15194
    /
15195
15196
Return a bytes object containing random bytes suitable for cryptographic use.
15197
[clinic start generated code]*/
15198
15199
static PyObject *
15200
os_urandom_impl(PyObject *module, Py_ssize_t size)
15201
/*[clinic end generated code: output=42c5cca9d18068e9 input=ade19e6b362e7388]*/
15202
0
{
15203
0
    PyObject *bytes;
15204
0
    int result;
15205
15206
0
    if (size < 0)
15207
0
        return PyErr_Format(PyExc_ValueError,
15208
0
                            "negative argument not allowed");
15209
0
    bytes = PyBytes_FromStringAndSize(NULL, size);
15210
0
    if (bytes == NULL)
15211
0
        return NULL;
15212
15213
0
    result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
15214
0
    if (result == -1) {
15215
0
        Py_DECREF(bytes);
15216
0
        return NULL;
15217
0
    }
15218
0
    return bytes;
15219
0
}
15220
15221
#ifdef HAVE_MEMFD_CREATE
15222
/*[clinic input]
15223
os.memfd_create
15224
15225
    name: FSConverter
15226
    flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
15227
15228
[clinic start generated code]*/
15229
15230
static PyObject *
15231
os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
15232
/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
15233
0
{
15234
0
    int fd;
15235
0
    const char *bytes = PyBytes_AS_STRING(name);
15236
0
    Py_BEGIN_ALLOW_THREADS
15237
0
    fd = memfd_create(bytes, flags);
15238
0
    Py_END_ALLOW_THREADS
15239
0
    if (fd == -1) {
15240
0
        return PyErr_SetFromErrno(PyExc_OSError);
15241
0
    }
15242
0
    return PyLong_FromLong(fd);
15243
0
}
15244
#endif
15245
15246
#if defined(HAVE_EVENTFD) && defined(EFD_CLOEXEC)
15247
/*[clinic input]
15248
os.eventfd
15249
15250
    initval: unsigned_int
15251
    flags: int(c_default="EFD_CLOEXEC") = EFD_CLOEXEC
15252
15253
Creates and returns an event notification file descriptor.
15254
[clinic start generated code]*/
15255
15256
static PyObject *
15257
os_eventfd_impl(PyObject *module, unsigned int initval, int flags)
15258
/*[clinic end generated code: output=ce9c9bbd1446f2de input=66203e3c50c4028b]*/
15259
15260
0
{
15261
    /* initval is limited to uint32_t, internal counter is uint64_t */
15262
0
    int fd;
15263
0
    Py_BEGIN_ALLOW_THREADS
15264
0
    fd = eventfd(initval, flags);
15265
0
    Py_END_ALLOW_THREADS
15266
0
    if (fd == -1) {
15267
0
        return PyErr_SetFromErrno(PyExc_OSError);
15268
0
    }
15269
0
    return PyLong_FromLong(fd);
15270
0
}
15271
15272
/*[clinic input]
15273
os.eventfd_read
15274
15275
    fd: fildes
15276
15277
Read eventfd value
15278
[clinic start generated code]*/
15279
15280
static PyObject *
15281
os_eventfd_read_impl(PyObject *module, int fd)
15282
/*[clinic end generated code: output=8f2c7b59a3521fd1 input=110f8b57fa596afe]*/
15283
0
{
15284
0
    eventfd_t value;
15285
0
    int result;
15286
0
    Py_BEGIN_ALLOW_THREADS
15287
0
    result = eventfd_read(fd, &value);
15288
0
    Py_END_ALLOW_THREADS
15289
0
    if (result == -1) {
15290
0
        return PyErr_SetFromErrno(PyExc_OSError);
15291
0
    }
15292
0
    return PyLong_FromUnsignedLongLong(value);
15293
0
}
15294
15295
/*[clinic input]
15296
os.eventfd_write
15297
15298
    fd: fildes
15299
    value: unsigned_long_long
15300
15301
Write eventfd value.
15302
[clinic start generated code]*/
15303
15304
static PyObject *
15305
os_eventfd_write_impl(PyObject *module, int fd, unsigned long long value)
15306
/*[clinic end generated code: output=bebd9040bbf987f5 input=156de8555be5a949]*/
15307
0
{
15308
0
    int result;
15309
0
    Py_BEGIN_ALLOW_THREADS
15310
0
    result = eventfd_write(fd, value);
15311
0
    Py_END_ALLOW_THREADS
15312
0
    if (result == -1) {
15313
0
        return PyErr_SetFromErrno(PyExc_OSError);
15314
0
    }
15315
0
    Py_RETURN_NONE;
15316
0
}
15317
#endif  /* HAVE_EVENTFD && EFD_CLOEXEC */
15318
15319
/* Terminal size querying */
15320
15321
PyDoc_STRVAR(TerminalSize_docstring,
15322
    "A tuple of (columns, lines) for holding terminal window size");
15323
15324
static PyStructSequence_Field TerminalSize_fields[] = {
15325
    {"columns", "width of the terminal window in characters"},
15326
    {"lines", "height of the terminal window in characters"},
15327
    {NULL, NULL}
15328
};
15329
15330
static PyStructSequence_Desc TerminalSize_desc = {
15331
    "os.terminal_size",
15332
    TerminalSize_docstring,
15333
    TerminalSize_fields,
15334
    2,
15335
};
15336
15337
#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
15338
/*[clinic input]
15339
os.get_terminal_size
15340
15341
    fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1
15342
    /
15343
15344
Return the size of the terminal window as (columns, lines).
15345
15346
The optional argument fd (default standard output) specifies
15347
which file descriptor should be queried.
15348
15349
If the file descriptor is not connected to a terminal, an OSError
15350
is thrown.
15351
15352
This function will only be defined if an implementation is
15353
available for this system.
15354
15355
shutil.get_terminal_size is the high-level function which should
15356
normally be used, os.get_terminal_size is the low-level implementation.
15357
[clinic start generated code]*/
15358
15359
static PyObject *
15360
os_get_terminal_size_impl(PyObject *module, int fd)
15361
/*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/
15362
0
{
15363
0
    int columns, lines;
15364
0
    PyObject *termsize;
15365
15366
    /* Under some conditions stdout may not be connected and
15367
     * fileno(stdout) may point to an invalid file descriptor. For example
15368
     * GUI apps don't have valid standard streams by default.
15369
     *
15370
     * If this happens, and the optional fd argument is not present,
15371
     * the ioctl below will fail returning EBADF. This is what we want.
15372
     */
15373
15374
0
#ifdef TERMSIZE_USE_IOCTL
15375
0
    {
15376
0
        struct winsize w;
15377
0
        if (ioctl(fd, TIOCGWINSZ, &w))
15378
0
            return PyErr_SetFromErrno(PyExc_OSError);
15379
0
        columns = w.ws_col;
15380
0
        lines = w.ws_row;
15381
0
    }
15382
0
#endif /* TERMSIZE_USE_IOCTL */
15383
15384
#ifdef TERMSIZE_USE_CONIO
15385
    {
15386
        HANDLE handle;
15387
        CONSOLE_SCREEN_BUFFER_INFO csbi;
15388
        handle = _Py_get_osfhandle(fd);
15389
        if (handle == INVALID_HANDLE_VALUE)
15390
            return NULL;
15391
15392
        if (!GetConsoleScreenBufferInfo(handle, &csbi))
15393
            return PyErr_SetFromWindowsErr(0);
15394
15395
        columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
15396
        lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
15397
    }
15398
#endif /* TERMSIZE_USE_CONIO */
15399
15400
0
    PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType;
15401
0
    termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
15402
0
    if (termsize == NULL)
15403
0
        return NULL;
15404
15405
0
    int pos = 0;
15406
15407
0
#define SET_TERMSIZE(CALL)                                   \
15408
0
    do {                                                     \
15409
0
        PyObject *item = (CALL);                             \
15410
0
        if (item == NULL) {                                  \
15411
0
            Py_DECREF(termsize);                             \
15412
0
            return NULL;                                     \
15413
0
        }                                                    \
15414
0
        PyStructSequence_SET_ITEM(termsize, pos++, item);    \
15415
0
    } while(0)
15416
15417
0
    SET_TERMSIZE(PyLong_FromLong(columns));
15418
0
    SET_TERMSIZE(PyLong_FromLong(lines));
15419
0
#undef SET_TERMSIZE
15420
15421
0
    return termsize;
15422
0
}
15423
#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
15424
15425
/*[clinic input]
15426
os.cpu_count
15427
15428
Return the number of logical CPUs in the system.
15429
15430
Return None if indeterminable.
15431
[clinic start generated code]*/
15432
15433
static PyObject *
15434
os_cpu_count_impl(PyObject *module)
15435
/*[clinic end generated code: output=5fc29463c3936a9c input=ba2f6f8980a0e2eb]*/
15436
0
{
15437
0
    const PyConfig *config = _Py_GetConfig();
15438
0
    if (config->cpu_count > 0) {
15439
0
        return PyLong_FromLong(config->cpu_count);
15440
0
    }
15441
15442
0
    int ncpu = 0;
15443
#ifdef MS_WINDOWS
15444
# ifdef MS_WINDOWS_DESKTOP
15445
    ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
15446
# else
15447
    ncpu = 0;
15448
# endif
15449
15450
#elif defined(__hpux)
15451
    ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
15452
15453
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
15454
    ncpu = sysconf(_SC_NPROCESSORS_ONLN);
15455
15456
#elif defined(__VXWORKS__)
15457
    ncpu = _Py_popcount32(vxCpuEnabledGet());
15458
15459
#elif defined(__DragonFly__) || \
15460
      defined(__OpenBSD__)   || \
15461
      defined(__FreeBSD__)   || \
15462
      defined(__NetBSD__)    || \
15463
      defined(__APPLE__)
15464
    ncpu = 0;
15465
    size_t len = sizeof(ncpu);
15466
    int mib[2] = {CTL_HW, HW_NCPU};
15467
    if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) {
15468
        ncpu = 0;
15469
    }
15470
#endif
15471
15472
0
    if (ncpu < 1) {
15473
0
        Py_RETURN_NONE;
15474
0
    }
15475
0
    return PyLong_FromLong(ncpu);
15476
0
}
15477
15478
15479
/*[clinic input]
15480
os.get_inheritable -> bool
15481
15482
    fd: int
15483
    /
15484
15485
Get the close-on-exe flag of the specified file descriptor.
15486
[clinic start generated code]*/
15487
15488
static int
15489
os_get_inheritable_impl(PyObject *module, int fd)
15490
/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
15491
0
{
15492
0
    int return_value;
15493
0
    _Py_BEGIN_SUPPRESS_IPH
15494
0
    return_value = _Py_get_inheritable(fd);
15495
0
    _Py_END_SUPPRESS_IPH
15496
0
    return return_value;
15497
0
}
15498
15499
15500
/*[clinic input]
15501
os.set_inheritable
15502
    fd: int
15503
    inheritable: int
15504
    /
15505
15506
Set the inheritable flag of the specified file descriptor.
15507
[clinic start generated code]*/
15508
15509
static PyObject *
15510
os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
15511
/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
15512
0
{
15513
0
    int result;
15514
15515
0
    _Py_BEGIN_SUPPRESS_IPH
15516
0
    result = _Py_set_inheritable(fd, inheritable, NULL);
15517
0
    _Py_END_SUPPRESS_IPH
15518
0
    if (result < 0)
15519
0
        return NULL;
15520
0
    Py_RETURN_NONE;
15521
0
}
15522
15523
15524
#ifdef MS_WINDOWS
15525
#ifndef HANDLE_FLAG_INHERIT
15526
#define HANDLE_FLAG_INHERIT 0x00000001
15527
#endif
15528
15529
/*[clinic input]
15530
os.get_handle_inheritable -> bool
15531
    handle: intptr_t
15532
    /
15533
15534
Get the close-on-exe flag of the specified file descriptor.
15535
[clinic start generated code]*/
15536
15537
static int
15538
os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
15539
/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
15540
{
15541
    DWORD flags;
15542
15543
    if (!GetHandleInformation((HANDLE)handle, &flags)) {
15544
        PyErr_SetFromWindowsErr(0);
15545
        return -1;
15546
    }
15547
15548
    return flags & HANDLE_FLAG_INHERIT;
15549
}
15550
15551
15552
/*[clinic input]
15553
os.set_handle_inheritable
15554
    handle: intptr_t
15555
    inheritable: bool
15556
    /
15557
15558
Set the inheritable flag of the specified handle.
15559
[clinic start generated code]*/
15560
15561
static PyObject *
15562
os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
15563
                               int inheritable)
15564
/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
15565
{
15566
    DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
15567
    if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
15568
        PyErr_SetFromWindowsErr(0);
15569
        return NULL;
15570
    }
15571
    Py_RETURN_NONE;
15572
}
15573
#endif /* MS_WINDOWS */
15574
15575
/*[clinic input]
15576
os.get_blocking -> bool
15577
    fd: int
15578
    /
15579
15580
Get the blocking mode of the file descriptor.
15581
15582
Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
15583
[clinic start generated code]*/
15584
15585
static int
15586
os_get_blocking_impl(PyObject *module, int fd)
15587
/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
15588
0
{
15589
0
    int blocking;
15590
15591
0
    _Py_BEGIN_SUPPRESS_IPH
15592
0
    blocking = _Py_get_blocking(fd);
15593
0
    _Py_END_SUPPRESS_IPH
15594
0
    return blocking;
15595
0
}
15596
15597
/*[clinic input]
15598
os.set_blocking
15599
    fd: int
15600
    blocking: bool
15601
    /
15602
15603
Set the blocking mode of the specified file descriptor.
15604
15605
Set the O_NONBLOCK flag if blocking is False,
15606
clear the O_NONBLOCK flag otherwise.
15607
[clinic start generated code]*/
15608
15609
static PyObject *
15610
os_set_blocking_impl(PyObject *module, int fd, int blocking)
15611
/*[clinic end generated code: output=384eb43aa0762a9d input=7e9dfc9b14804dd4]*/
15612
0
{
15613
0
    int result;
15614
15615
0
    _Py_BEGIN_SUPPRESS_IPH
15616
0
    result = _Py_set_blocking(fd, blocking);
15617
0
    _Py_END_SUPPRESS_IPH
15618
0
    if (result < 0)
15619
0
        return NULL;
15620
0
    Py_RETURN_NONE;
15621
0
}
15622
15623
15624
/*[clinic input]
15625
class os.DirEntry "DirEntry *" "DirEntryType"
15626
[clinic start generated code]*/
15627
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
15628
15629
typedef struct {
15630
    PyObject_HEAD
15631
    PyObject *name;
15632
    PyObject *path;
15633
    PyObject *stat;
15634
    PyObject *lstat;
15635
#ifdef MS_WINDOWS
15636
    struct _Py_stat_struct win32_lstat;
15637
    uint64_t win32_file_index;
15638
    uint64_t win32_file_index_high;
15639
    int got_file_index;
15640
#else /* POSIX */
15641
#ifdef HAVE_DIRENT_D_TYPE
15642
    unsigned char d_type;
15643
#endif
15644
    ino_t d_ino;
15645
    int dir_fd;
15646
#endif
15647
} DirEntry;
15648
15649
0
#define DirEntry_CAST(op)   ((DirEntry *)(op))
15650
15651
static void
15652
DirEntry_dealloc(PyObject *op)
15653
0
{
15654
0
    DirEntry *entry = DirEntry_CAST(op);
15655
0
    PyTypeObject *tp = Py_TYPE(entry);
15656
0
    Py_XDECREF(entry->name);
15657
0
    Py_XDECREF(entry->path);
15658
0
    Py_XDECREF(entry->stat);
15659
0
    Py_XDECREF(entry->lstat);
15660
0
    freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
15661
0
    free_func(entry);
15662
0
    Py_DECREF(tp);
15663
0
}
15664
15665
/* Forward reference */
15666
static int
15667
DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
15668
                   int follow_symlinks, unsigned short mode_bits);
15669
15670
/*[clinic input]
15671
os.DirEntry.is_symlink -> bool
15672
    defining_class: defining_class
15673
    /
15674
15675
Return True if the entry is a symbolic link; cached per entry.
15676
[clinic start generated code]*/
15677
15678
static int
15679
os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class)
15680
/*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/
15681
0
{
15682
#ifdef MS_WINDOWS
15683
    return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
15684
#elif defined(HAVE_DIRENT_D_TYPE)
15685
    /* POSIX */
15686
0
    if (self->d_type != DT_UNKNOWN)
15687
0
        return self->d_type == DT_LNK;
15688
0
    else
15689
0
        return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
15690
#else
15691
    /* POSIX without d_type */
15692
    return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
15693
#endif
15694
0
}
15695
15696
/*[clinic input]
15697
os.DirEntry.is_junction -> bool
15698
15699
Return True if the entry is a junction; cached per entry.
15700
[clinic start generated code]*/
15701
15702
static int
15703
os_DirEntry_is_junction_impl(DirEntry *self)
15704
/*[clinic end generated code: output=97f64d5d99eeccb5 input=4fc8e701eea118a1]*/
15705
0
{
15706
#ifdef MS_WINDOWS
15707
    return self->win32_lstat.st_reparse_tag == IO_REPARSE_TAG_MOUNT_POINT;
15708
#else
15709
0
    return 0;
15710
0
#endif
15711
0
}
15712
15713
static PyObject *
15714
DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks)
15715
0
{
15716
0
    int result;
15717
0
    STRUCT_STAT st;
15718
0
    PyObject *ub;
15719
15720
#ifdef MS_WINDOWS
15721
    if (!PyUnicode_FSDecoder(self->path, &ub))
15722
        return NULL;
15723
    wchar_t *path = PyUnicode_AsWideCharString(ub, NULL);
15724
    Py_DECREF(ub);
15725
#else /* POSIX */
15726
0
    if (!PyUnicode_FSConverter(self->path, &ub))
15727
0
        return NULL;
15728
0
    const char *path = PyBytes_AS_STRING(ub);
15729
0
    if (self->dir_fd != DEFAULT_DIR_FD) {
15730
0
#ifdef HAVE_FSTATAT
15731
0
      if (HAVE_FSTATAT_RUNTIME) {
15732
0
        Py_BEGIN_ALLOW_THREADS
15733
0
        result = fstatat(self->dir_fd, path, &st,
15734
0
                         follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
15735
0
        Py_END_ALLOW_THREADS
15736
0
      } else
15737
15738
0
#endif /* HAVE_FSTATAT */
15739
0
      {
15740
0
        Py_DECREF(ub);
15741
0
        PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
15742
0
        return NULL;
15743
0
      }
15744
0
    }
15745
0
    else
15746
0
#endif
15747
0
    {
15748
0
        Py_BEGIN_ALLOW_THREADS
15749
0
        if (follow_symlinks) {
15750
0
            result = STAT(path, &st);
15751
0
        }
15752
0
        else {
15753
0
            result = LSTAT(path, &st);
15754
0
        }
15755
0
        Py_END_ALLOW_THREADS
15756
0
    }
15757
15758
0
    int saved_errno = errno;
15759
#if defined(MS_WINDOWS)
15760
    PyMem_Free(path);
15761
#else
15762
0
    Py_DECREF(ub);
15763
0
#endif
15764
15765
0
    if (result != 0) {
15766
0
        errno = saved_errno;
15767
0
        path_object_error(self->path);
15768
0
        return NULL;
15769
0
    }
15770
15771
0
    return _pystat_fromstructstat(module, &st);
15772
0
}
15773
15774
static PyObject *
15775
DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self)
15776
0
{
15777
0
    if (!self->lstat) {
15778
0
        PyObject *module = PyType_GetModule(defining_class);
15779
#ifdef MS_WINDOWS
15780
        self->lstat = _pystat_fromstructstat(module, &self->win32_lstat);
15781
#else /* POSIX */
15782
0
        self->lstat = DirEntry_fetch_stat(module, self, 0);
15783
0
#endif
15784
0
    }
15785
0
    return Py_XNewRef(self->lstat);
15786
0
}
15787
15788
/*[clinic input]
15789
os.DirEntry.stat
15790
    defining_class: defining_class
15791
    /
15792
    *
15793
    follow_symlinks: bool = True
15794
15795
Return stat_result object for the entry; cached per entry.
15796
[clinic start generated code]*/
15797
15798
static PyObject *
15799
os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
15800
                      int follow_symlinks)
15801
/*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/
15802
0
{
15803
0
    if (!follow_symlinks) {
15804
0
        return DirEntry_get_lstat(defining_class, self);
15805
0
    }
15806
15807
0
    if (!self->stat) {
15808
0
        int result = os_DirEntry_is_symlink_impl(self, defining_class);
15809
0
        if (result == -1) {
15810
0
            return NULL;
15811
0
        }
15812
0
        if (result) {
15813
0
            PyObject *module = PyType_GetModule(defining_class);
15814
0
            self->stat = DirEntry_fetch_stat(module, self, 1);
15815
0
        }
15816
0
        else {
15817
0
            self->stat = DirEntry_get_lstat(defining_class, self);
15818
0
        }
15819
0
    }
15820
15821
0
    return Py_XNewRef(self->stat);
15822
0
}
15823
15824
/* Set exception and return -1 on error, 0 for False, 1 for True */
15825
static int
15826
DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
15827
                   int follow_symlinks, unsigned short mode_bits)
15828
0
{
15829
0
    PyObject *stat = NULL;
15830
0
    PyObject *st_mode = NULL;
15831
0
    long mode;
15832
0
    int result;
15833
0
#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
15834
0
    int is_symlink;
15835
0
    int need_stat;
15836
0
#endif
15837
#ifdef MS_WINDOWS
15838
    unsigned long dir_bits;
15839
#endif
15840
15841
#ifdef MS_WINDOWS
15842
    is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
15843
    need_stat = follow_symlinks && is_symlink;
15844
#elif defined(HAVE_DIRENT_D_TYPE)
15845
    is_symlink = self->d_type == DT_LNK;
15846
0
    need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
15847
0
#endif
15848
15849
0
#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
15850
0
    if (need_stat) {
15851
0
#endif
15852
0
        stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks);
15853
0
        if (!stat) {
15854
0
            if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
15855
                /* If file doesn't exist (anymore), then return False
15856
                   (i.e., say it's not a file/directory) */
15857
0
                PyErr_Clear();
15858
0
                return 0;
15859
0
            }
15860
0
            goto error;
15861
0
        }
15862
0
        _posixstate* state = get_posix_state(PyType_GetModule(defining_class));
15863
0
        st_mode = PyObject_GetAttr(stat, state->st_mode);
15864
0
        if (!st_mode)
15865
0
            goto error;
15866
15867
0
        mode = PyLong_AsLong(st_mode);
15868
0
        if (mode == -1 && PyErr_Occurred())
15869
0
            goto error;
15870
0
        Py_CLEAR(st_mode);
15871
0
        Py_CLEAR(stat);
15872
0
        result = (mode & S_IFMT) == mode_bits;
15873
0
#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
15874
0
    }
15875
0
    else if (is_symlink) {
15876
0
        assert(mode_bits != S_IFLNK);
15877
0
        result = 0;
15878
0
    }
15879
0
    else {
15880
0
        assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
15881
#ifdef MS_WINDOWS
15882
        dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
15883
        if (mode_bits == S_IFDIR)
15884
            result = dir_bits != 0;
15885
        else
15886
            result = dir_bits == 0;
15887
#else /* POSIX */
15888
0
        if (mode_bits == S_IFDIR)
15889
0
            result = self->d_type == DT_DIR;
15890
0
        else
15891
0
            result = self->d_type == DT_REG;
15892
0
#endif
15893
0
    }
15894
0
#endif
15895
15896
0
    return result;
15897
15898
0
error:
15899
0
    Py_XDECREF(st_mode);
15900
0
    Py_XDECREF(stat);
15901
0
    return -1;
15902
0
}
15903
15904
/*[clinic input]
15905
os.DirEntry.is_dir -> bool
15906
    defining_class: defining_class
15907
    /
15908
    *
15909
    follow_symlinks: bool = True
15910
15911
Return True if the entry is a directory; cached per entry.
15912
[clinic start generated code]*/
15913
15914
static int
15915
os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class,
15916
                        int follow_symlinks)
15917
/*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/
15918
0
{
15919
0
    return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR);
15920
0
}
15921
15922
/*[clinic input]
15923
os.DirEntry.is_file -> bool
15924
    defining_class: defining_class
15925
    /
15926
    *
15927
    follow_symlinks: bool = True
15928
15929
Return True if the entry is a file; cached per entry.
15930
[clinic start generated code]*/
15931
15932
static int
15933
os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class,
15934
                         int follow_symlinks)
15935
/*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/
15936
0
{
15937
0
    return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG);
15938
0
}
15939
15940
/*[clinic input]
15941
os.DirEntry.inode
15942
15943
Return inode of the entry; cached per entry.
15944
[clinic start generated code]*/
15945
15946
static PyObject *
15947
os_DirEntry_inode_impl(DirEntry *self)
15948
/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
15949
0
{
15950
#ifdef MS_WINDOWS
15951
    if (!self->got_file_index) {
15952
        PyObject *unicode;
15953
        STRUCT_STAT stat;
15954
        int result;
15955
15956
        if (!PyUnicode_FSDecoder(self->path, &unicode))
15957
            return NULL;
15958
        wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL);
15959
        Py_DECREF(unicode);
15960
        result = LSTAT(path, &stat);
15961
15962
        int saved_errno = errno;
15963
        PyMem_Free(path);
15964
15965
        if (result != 0) {
15966
            errno = saved_errno;
15967
            return path_object_error(self->path);
15968
        }
15969
15970
        self->win32_file_index = stat.st_ino;
15971
        self->win32_file_index_high = stat.st_ino_high;
15972
        self->got_file_index = 1;
15973
    }
15974
    return _pystat_l128_from_l64_l64(self->win32_file_index, self->win32_file_index_high);
15975
#else /* POSIX */
15976
0
    static_assert(sizeof(unsigned long long) >= sizeof(self->d_ino),
15977
0
                  "DirEntry.d_ino is larger than unsigned long long");
15978
0
    return PyLong_FromUnsignedLongLong(self->d_ino);
15979
0
#endif
15980
0
}
15981
15982
static PyObject *
15983
DirEntry_repr(PyObject *op)
15984
0
{
15985
0
    DirEntry *self = DirEntry_CAST(op);
15986
0
    return PyUnicode_FromFormat("<DirEntry %R>", self->name);
15987
0
}
15988
15989
/*[clinic input]
15990
os.DirEntry.__fspath__
15991
15992
Returns the path for the entry.
15993
[clinic start generated code]*/
15994
15995
static PyObject *
15996
os_DirEntry___fspath___impl(DirEntry *self)
15997
/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
15998
0
{
15999
0
    return Py_NewRef(self->path);
16000
0
}
16001
16002
static PyMemberDef DirEntry_members[] = {
16003
    {"name", Py_T_OBJECT_EX, offsetof(DirEntry, name), Py_READONLY,
16004
     "the entry's base filename, relative to scandir() \"path\" argument"},
16005
    {"path", Py_T_OBJECT_EX, offsetof(DirEntry, path), Py_READONLY,
16006
     "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
16007
    {NULL}
16008
};
16009
16010
#include "clinic/posixmodule.c.h"
16011
16012
static PyMethodDef DirEntry_methods[] = {
16013
    OS_DIRENTRY_IS_DIR_METHODDEF
16014
    OS_DIRENTRY_IS_FILE_METHODDEF
16015
    OS_DIRENTRY_IS_SYMLINK_METHODDEF
16016
    OS_DIRENTRY_IS_JUNCTION_METHODDEF
16017
    OS_DIRENTRY_STAT_METHODDEF
16018
    OS_DIRENTRY_INODE_METHODDEF
16019
    OS_DIRENTRY___FSPATH___METHODDEF
16020
    {"__class_getitem__",       Py_GenericAlias,
16021
    METH_O|METH_CLASS,          PyDoc_STR("See PEP 585")},
16022
    {NULL}
16023
};
16024
16025
static PyType_Slot DirEntryType_slots[] = {
16026
    {Py_tp_dealloc, DirEntry_dealloc},
16027
    {Py_tp_repr, DirEntry_repr},
16028
    {Py_tp_methods, DirEntry_methods},
16029
    {Py_tp_members, DirEntry_members},
16030
    {0, 0},
16031
};
16032
16033
static PyType_Spec DirEntryType_spec = {
16034
    MODNAME ".DirEntry",
16035
    sizeof(DirEntry),
16036
    0,
16037
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
16038
    DirEntryType_slots
16039
};
16040
16041
16042
#ifdef MS_WINDOWS
16043
16044
static wchar_t *
16045
join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
16046
{
16047
    Py_ssize_t path_len;
16048
    Py_ssize_t size;
16049
    wchar_t *result;
16050
    wchar_t ch;
16051
16052
    if (!path_wide) { /* Default arg: "." */
16053
        path_wide = L".";
16054
        path_len = 1;
16055
    }
16056
    else {
16057
        path_len = wcslen(path_wide);
16058
    }
16059
16060
    /* The +1's are for the path separator and the NUL */
16061
    size = path_len + 1 + wcslen(filename) + 1;
16062
    result = PyMem_New(wchar_t, size);
16063
    if (!result) {
16064
        PyErr_NoMemory();
16065
        return NULL;
16066
    }
16067
    wcscpy(result, path_wide);
16068
    if (path_len > 0) {
16069
        ch = result[path_len - 1];
16070
        if (ch != SEP && ch != ALTSEP && ch != L':')
16071
            result[path_len++] = SEP;
16072
        wcscpy(result + path_len, filename);
16073
    }
16074
    return result;
16075
}
16076
16077
static PyObject *
16078
DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
16079
{
16080
    DirEntry *entry;
16081
    BY_HANDLE_FILE_INFORMATION file_info;
16082
    ULONG reparse_tag;
16083
    wchar_t *joined_path;
16084
16085
    PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
16086
    entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
16087
    if (!entry)
16088
        return NULL;
16089
    entry->name = NULL;
16090
    entry->path = NULL;
16091
    entry->stat = NULL;
16092
    entry->lstat = NULL;
16093
    entry->got_file_index = 0;
16094
16095
    entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
16096
    if (!entry->name)
16097
        goto error;
16098
    int return_bytes = path->wide && PyBytes_Check(path->object);
16099
    if (return_bytes) {
16100
        Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
16101
        if (!entry->name)
16102
            goto error;
16103
    }
16104
16105
    joined_path = join_path_filenameW(path->wide, dataW->cFileName);
16106
    if (!joined_path)
16107
        goto error;
16108
16109
    entry->path = PyUnicode_FromWideChar(joined_path, -1);
16110
    PyMem_Free(joined_path);
16111
    if (!entry->path)
16112
        goto error;
16113
    if (return_bytes) {
16114
        Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
16115
        if (!entry->path)
16116
            goto error;
16117
    }
16118
16119
    find_data_to_file_info(dataW, &file_info, &reparse_tag);
16120
    _Py_attribute_data_to_stat(&file_info, reparse_tag, NULL, NULL, &entry->win32_lstat);
16121
16122
    /* ctime is only deprecated from 3.12, so we copy birthtime across */
16123
    entry->win32_lstat.st_ctime = entry->win32_lstat.st_birthtime;
16124
    entry->win32_lstat.st_ctime_nsec = entry->win32_lstat.st_birthtime_nsec;
16125
16126
    return (PyObject *)entry;
16127
16128
error:
16129
    Py_DECREF(entry);
16130
    return NULL;
16131
}
16132
16133
#else /* POSIX */
16134
16135
static char *
16136
join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
16137
0
{
16138
0
    Py_ssize_t path_len;
16139
0
    Py_ssize_t size;
16140
0
    char *result;
16141
16142
0
    if (!path_narrow) { /* Default arg: "." */
16143
0
        path_narrow = ".";
16144
0
        path_len = 1;
16145
0
    }
16146
0
    else {
16147
0
        path_len = strlen(path_narrow);
16148
0
    }
16149
16150
0
    if (filename_len == -1)
16151
0
        filename_len = strlen(filename);
16152
16153
    /* The +1's are for the path separator and the NUL */
16154
0
    size = path_len + 1 + filename_len + 1;
16155
0
    result = PyMem_New(char, size);
16156
0
    if (!result) {
16157
0
        PyErr_NoMemory();
16158
0
        return NULL;
16159
0
    }
16160
0
    strcpy(result, path_narrow);
16161
0
    if (path_len > 0 && result[path_len - 1] != '/')
16162
0
        result[path_len++] = '/';
16163
0
    strcpy(result + path_len, filename);
16164
0
    return result;
16165
0
}
16166
16167
static PyObject *
16168
DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
16169
                         Py_ssize_t name_len, ino_t d_ino
16170
#ifdef HAVE_DIRENT_D_TYPE
16171
                         , unsigned char d_type
16172
#endif
16173
                         )
16174
0
{
16175
0
    DirEntry *entry;
16176
0
    char *joined_path;
16177
16178
0
    PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
16179
0
    entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
16180
0
    if (!entry)
16181
0
        return NULL;
16182
0
    entry->name = NULL;
16183
0
    entry->path = NULL;
16184
0
    entry->stat = NULL;
16185
0
    entry->lstat = NULL;
16186
16187
0
    if (path->fd != -1) {
16188
0
        entry->dir_fd = path->fd;
16189
0
        joined_path = NULL;
16190
0
    }
16191
0
    else {
16192
0
        entry->dir_fd = DEFAULT_DIR_FD;
16193
0
        joined_path = join_path_filename(path->narrow, name, name_len);
16194
0
        if (!joined_path)
16195
0
            goto error;
16196
0
    }
16197
16198
0
    if (!path->narrow || !PyBytes_Check(path->object)) {
16199
0
        entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
16200
0
        if (joined_path)
16201
0
            entry->path = PyUnicode_DecodeFSDefault(joined_path);
16202
0
    }
16203
0
    else {
16204
0
        entry->name = PyBytes_FromStringAndSize(name, name_len);
16205
0
        if (joined_path)
16206
0
            entry->path = PyBytes_FromString(joined_path);
16207
0
    }
16208
0
    PyMem_Free(joined_path);
16209
0
    if (!entry->name)
16210
0
        goto error;
16211
16212
0
    if (path->fd != -1) {
16213
0
        entry->path = Py_NewRef(entry->name);
16214
0
    }
16215
0
    else if (!entry->path)
16216
0
        goto error;
16217
16218
0
#ifdef HAVE_DIRENT_D_TYPE
16219
0
    entry->d_type = d_type;
16220
0
#endif
16221
0
    entry->d_ino = d_ino;
16222
16223
0
    return (PyObject *)entry;
16224
16225
0
error:
16226
0
    Py_XDECREF(entry);
16227
0
    return NULL;
16228
0
}
16229
16230
#endif
16231
16232
16233
typedef struct {
16234
    PyObject_HEAD
16235
    path_t path;
16236
#ifdef MS_WINDOWS
16237
    HANDLE handle;
16238
    WIN32_FIND_DATAW file_data;
16239
    int first_time;
16240
#else /* POSIX */
16241
    DIR *dirp;
16242
#endif
16243
#ifdef HAVE_FDOPENDIR
16244
    int fd;
16245
#endif
16246
} ScandirIterator;
16247
16248
0
#define ScandirIterator_CAST(op)    ((ScandirIterator *)(op))
16249
16250
#ifdef MS_WINDOWS
16251
16252
static int
16253
ScandirIterator_is_closed(ScandirIterator *iterator)
16254
{
16255
    return iterator->handle == INVALID_HANDLE_VALUE;
16256
}
16257
16258
static void
16259
ScandirIterator_closedir(ScandirIterator *iterator)
16260
{
16261
    HANDLE handle = iterator->handle;
16262
16263
    if (handle == INVALID_HANDLE_VALUE)
16264
        return;
16265
16266
    iterator->handle = INVALID_HANDLE_VALUE;
16267
    Py_BEGIN_ALLOW_THREADS
16268
    FindClose(handle);
16269
    Py_END_ALLOW_THREADS
16270
}
16271
16272
static PyObject *
16273
ScandirIterator_iternext(PyObject *op)
16274
{
16275
    ScandirIterator *iterator = ScandirIterator_CAST(op);
16276
    WIN32_FIND_DATAW *file_data = &iterator->file_data;
16277
    BOOL success;
16278
    PyObject *entry;
16279
16280
    /* Happens if the iterator is iterated twice, or closed explicitly */
16281
    if (iterator->handle == INVALID_HANDLE_VALUE)
16282
        return NULL;
16283
16284
    while (1) {
16285
        if (!iterator->first_time) {
16286
            Py_BEGIN_ALLOW_THREADS
16287
            success = FindNextFileW(iterator->handle, file_data);
16288
            Py_END_ALLOW_THREADS
16289
            if (!success) {
16290
                /* Error or no more files */
16291
                if (GetLastError() != ERROR_NO_MORE_FILES)
16292
                    path_error(&iterator->path);
16293
                break;
16294
            }
16295
        }
16296
        iterator->first_time = 0;
16297
16298
        /* Skip over . and .. */
16299
        if (wcscmp(file_data->cFileName, L".") != 0 &&
16300
            wcscmp(file_data->cFileName, L"..") != 0)
16301
        {
16302
            PyObject *module = PyType_GetModule(Py_TYPE(iterator));
16303
            entry = DirEntry_from_find_data(module, &iterator->path, file_data);
16304
            if (!entry)
16305
                break;
16306
            return entry;
16307
        }
16308
16309
        /* Loop till we get a non-dot directory or finish iterating */
16310
    }
16311
16312
    /* Error or no more files */
16313
    ScandirIterator_closedir(iterator);
16314
    return NULL;
16315
}
16316
16317
#else /* POSIX */
16318
16319
static int
16320
ScandirIterator_is_closed(ScandirIterator *iterator)
16321
0
{
16322
0
    return !iterator->dirp;
16323
0
}
16324
16325
static void
16326
ScandirIterator_closedir(ScandirIterator *iterator)
16327
0
{
16328
0
    DIR *dirp = iterator->dirp;
16329
16330
0
    if (!dirp)
16331
0
        return;
16332
16333
0
    iterator->dirp = NULL;
16334
0
    Py_BEGIN_ALLOW_THREADS
16335
0
#ifdef HAVE_FDOPENDIR
16336
0
    if (iterator->path.fd != -1)
16337
0
        rewinddir(dirp);
16338
0
#endif
16339
0
    closedir(dirp);
16340
0
    Py_END_ALLOW_THREADS
16341
0
    return;
16342
0
}
16343
16344
static PyObject *
16345
ScandirIterator_iternext(PyObject *op)
16346
0
{
16347
0
    ScandirIterator *iterator = ScandirIterator_CAST(op);
16348
0
    struct dirent *direntp;
16349
0
    Py_ssize_t name_len;
16350
0
    int is_dot;
16351
0
    PyObject *entry;
16352
16353
    /* Happens if the iterator is iterated twice, or closed explicitly */
16354
0
    if (!iterator->dirp)
16355
0
        return NULL;
16356
16357
0
    while (1) {
16358
0
        errno = 0;
16359
0
        Py_BEGIN_ALLOW_THREADS
16360
0
        direntp = readdir(iterator->dirp);
16361
0
        Py_END_ALLOW_THREADS
16362
16363
0
        if (!direntp) {
16364
            /* Error or no more files */
16365
0
            if (errno != 0)
16366
0
                path_error(&iterator->path);
16367
0
            break;
16368
0
        }
16369
16370
        /* Skip over . and .. */
16371
0
        name_len = NAMLEN(direntp);
16372
0
        is_dot = direntp->d_name[0] == '.' &&
16373
0
                 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
16374
0
        if (!is_dot) {
16375
0
            PyObject *module = PyType_GetModule(Py_TYPE(iterator));
16376
0
            entry = DirEntry_from_posix_info(module,
16377
0
                                             &iterator->path, direntp->d_name,
16378
0
                                             name_len, direntp->d_ino
16379
0
#ifdef HAVE_DIRENT_D_TYPE
16380
0
                                             , direntp->d_type
16381
0
#endif
16382
0
                                            );
16383
0
            if (!entry)
16384
0
                break;
16385
0
            return entry;
16386
0
        }
16387
16388
        /* Loop till we get a non-dot directory or finish iterating */
16389
0
    }
16390
16391
    /* Error or no more files */
16392
0
    ScandirIterator_closedir(iterator);
16393
0
    return NULL;
16394
0
}
16395
16396
#endif
16397
16398
static PyObject *
16399
ScandirIterator_close(PyObject *op, PyObject *Py_UNUSED(dummy))
16400
0
{
16401
0
    ScandirIterator *self = ScandirIterator_CAST(op);
16402
0
    ScandirIterator_closedir(self);
16403
0
    Py_RETURN_NONE;
16404
0
}
16405
16406
static PyObject *
16407
ScandirIterator_enter(PyObject *self, PyObject *Py_UNUSED(dummy))
16408
0
{
16409
0
    return Py_NewRef(self);
16410
0
}
16411
16412
static PyObject *
16413
ScandirIterator_exit(PyObject *op, PyObject *Py_UNUSED(args))
16414
0
{
16415
0
    ScandirIterator *self = ScandirIterator_CAST(op);
16416
0
    ScandirIterator_closedir(self);
16417
0
    Py_RETURN_NONE;
16418
0
}
16419
16420
static void
16421
ScandirIterator_finalize(PyObject *op)
16422
0
{
16423
0
    ScandirIterator *iterator = ScandirIterator_CAST(op);
16424
    /* Save the current exception, if any. */
16425
0
    PyObject *exc = PyErr_GetRaisedException();
16426
16427
0
    if (!ScandirIterator_is_closed(iterator)) {
16428
0
        ScandirIterator_closedir(iterator);
16429
16430
0
        if (PyErr_ResourceWarning(op, 1,
16431
0
                                  "unclosed scandir iterator %R", iterator))
16432
0
        {
16433
            /* Spurious errors can appear at shutdown */
16434
0
            if (PyErr_ExceptionMatches(PyExc_Warning)) {
16435
0
                PyErr_FormatUnraisable("Exception ignored while finalizing "
16436
0
                                       "scandir iterator %R", iterator);
16437
0
            }
16438
0
        }
16439
0
    }
16440
16441
0
    path_cleanup(&iterator->path);
16442
16443
    /* Restore the saved exception. */
16444
0
    PyErr_SetRaisedException(exc);
16445
0
}
16446
16447
static void
16448
ScandirIterator_dealloc(PyObject *op)
16449
0
{
16450
0
    PyTypeObject *tp = Py_TYPE(op);
16451
0
    if (PyObject_CallFinalizerFromDealloc(op) < 0)
16452
0
        return;
16453
16454
0
    freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
16455
0
    free_func(op);
16456
0
    Py_DECREF(tp);
16457
0
}
16458
16459
static PyMethodDef ScandirIterator_methods[] = {
16460
    {"__enter__", ScandirIterator_enter, METH_NOARGS},
16461
    {"__exit__", ScandirIterator_exit, METH_VARARGS},
16462
    {"close", ScandirIterator_close, METH_NOARGS},
16463
    {NULL}
16464
};
16465
16466
static PyType_Slot ScandirIteratorType_slots[] = {
16467
    {Py_tp_dealloc, ScandirIterator_dealloc},
16468
    {Py_tp_finalize, ScandirIterator_finalize},
16469
    {Py_tp_iter, PyObject_SelfIter},
16470
    {Py_tp_iternext, ScandirIterator_iternext},
16471
    {Py_tp_methods, ScandirIterator_methods},
16472
    {0, 0},
16473
};
16474
16475
static PyType_Spec ScandirIteratorType_spec = {
16476
    MODNAME ".ScandirIterator",
16477
    sizeof(ScandirIterator),
16478
    0,
16479
    // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
16480
    // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
16481
    (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE
16482
        | Py_TPFLAGS_DISALLOW_INSTANTIATION),
16483
    ScandirIteratorType_slots
16484
};
16485
16486
/*[clinic input]
16487
os.scandir
16488
16489
    path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
16490
16491
Return an iterator of DirEntry objects for given path.
16492
16493
path can be specified as either str, bytes, or a path-like object.  If path
16494
is bytes, the names of yielded DirEntry objects will also be bytes; in
16495
all other circumstances they will be str.
16496
16497
If path is None, uses the path='.'.
16498
[clinic start generated code]*/
16499
16500
static PyObject *
16501
os_scandir_impl(PyObject *module, path_t *path)
16502
/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
16503
0
{
16504
0
    ScandirIterator *iterator;
16505
#ifdef MS_WINDOWS
16506
    wchar_t *path_strW;
16507
#else
16508
0
    const char *path_str;
16509
0
#ifdef HAVE_FDOPENDIR
16510
0
    int fd = -1;
16511
0
#endif
16512
0
#endif
16513
16514
0
    if (PySys_Audit("os.scandir", "O",
16515
0
                    path->object ? path->object : Py_None) < 0) {
16516
0
        return NULL;
16517
0
    }
16518
16519
0
    PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
16520
0
    iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
16521
0
    if (!iterator)
16522
0
        return NULL;
16523
16524
#ifdef MS_WINDOWS
16525
    iterator->handle = INVALID_HANDLE_VALUE;
16526
#else
16527
0
    iterator->dirp = NULL;
16528
0
#endif
16529
16530
    /* Move the ownership to iterator->path */
16531
0
    memcpy(&iterator->path, path, sizeof(path_t));
16532
0
    memset(path, 0, sizeof(path_t));
16533
16534
#ifdef MS_WINDOWS
16535
    iterator->first_time = 1;
16536
16537
    path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
16538
    if (!path_strW)
16539
        goto error;
16540
16541
    Py_BEGIN_ALLOW_THREADS
16542
    iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
16543
    Py_END_ALLOW_THREADS
16544
16545
    if (iterator->handle == INVALID_HANDLE_VALUE) {
16546
        path_error(&iterator->path);
16547
        PyMem_Free(path_strW);
16548
        goto error;
16549
    }
16550
    PyMem_Free(path_strW);
16551
#else /* POSIX */
16552
0
    errno = 0;
16553
0
#ifdef HAVE_FDOPENDIR
16554
0
    if (iterator->path.fd != -1) {
16555
0
      if (HAVE_FDOPENDIR_RUNTIME) {
16556
        /* closedir() closes the FD, so we duplicate it */
16557
0
        fd = _Py_dup(iterator->path.fd);
16558
0
        if (fd == -1)
16559
0
            goto error;
16560
16561
0
        Py_BEGIN_ALLOW_THREADS
16562
0
        iterator->dirp = fdopendir(fd);
16563
0
        Py_END_ALLOW_THREADS
16564
0
      } else {
16565
0
        PyErr_SetString(PyExc_TypeError,
16566
0
            "scandir: path should be string, bytes, os.PathLike or None, not int");
16567
0
        return NULL;
16568
0
      }
16569
0
    }
16570
0
    else
16571
0
#endif
16572
0
    {
16573
0
        if (iterator->path.narrow)
16574
0
            path_str = iterator->path.narrow;
16575
0
        else
16576
0
            path_str = ".";
16577
16578
0
        Py_BEGIN_ALLOW_THREADS
16579
0
        iterator->dirp = opendir(path_str);
16580
0
        Py_END_ALLOW_THREADS
16581
0
    }
16582
16583
0
    if (!iterator->dirp) {
16584
0
        path_error(&iterator->path);
16585
0
#ifdef HAVE_FDOPENDIR
16586
0
        if (fd != -1) {
16587
0
            Py_BEGIN_ALLOW_THREADS
16588
0
            close(fd);
16589
0
            Py_END_ALLOW_THREADS
16590
0
        }
16591
0
#endif
16592
0
        goto error;
16593
0
    }
16594
0
#endif
16595
16596
0
    return (PyObject *)iterator;
16597
16598
0
error:
16599
0
    Py_DECREF(iterator);
16600
0
    return NULL;
16601
0
}
16602
16603
/*
16604
    Return the file system path representation of the object.
16605
16606
    If the object is str or bytes, then allow it to pass through with
16607
    an incremented refcount. If the object defines __fspath__(), then
16608
    return the result of that method. All other types raise a TypeError.
16609
*/
16610
PyObject *
16611
PyOS_FSPath(PyObject *path)
16612
39.8k
{
16613
    /* For error message reasons, this function is manually inlined in
16614
       path_converter(). */
16615
39.8k
    PyObject *func = NULL;
16616
39.8k
    PyObject *path_repr = NULL;
16617
16618
39.8k
    if (PyUnicode_Check(path) || PyBytes_Check(path)) {
16619
39.8k
        return Py_NewRef(path);
16620
39.8k
    }
16621
16622
0
    func = _PyObject_LookupSpecial(path, &_Py_ID(__fspath__));
16623
0
    if ((NULL == func) || (func == Py_None)) {
16624
0
        return PyErr_Format(PyExc_TypeError,
16625
0
                            "expected str, bytes or os.PathLike object, "
16626
0
                            "not %.200s",
16627
0
                            _PyType_Name(Py_TYPE(path)));
16628
0
    }
16629
16630
0
    path_repr = _PyObject_CallNoArgs(func);
16631
0
    Py_DECREF(func);
16632
0
    if (NULL == path_repr) {
16633
0
        return NULL;
16634
0
    }
16635
16636
0
    if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
16637
0
        PyErr_Format(PyExc_TypeError,
16638
0
                     "expected %.200s.__fspath__() to return str or bytes, "
16639
0
                     "not %.200s", _PyType_Name(Py_TYPE(path)),
16640
0
                     _PyType_Name(Py_TYPE(path_repr)));
16641
0
        Py_DECREF(path_repr);
16642
0
        return NULL;
16643
0
    }
16644
16645
0
    return path_repr;
16646
0
}
16647
16648
/*[clinic input]
16649
@permit_long_docstring_body
16650
os.fspath
16651
16652
    path: object
16653
16654
Return the file system path representation of the object.
16655
16656
If the object is str or bytes, then allow it to pass through as-is. If the
16657
object defines __fspath__(), then return the result of that method. All other
16658
types raise a TypeError.
16659
[clinic start generated code]*/
16660
16661
static PyObject *
16662
os_fspath_impl(PyObject *module, PyObject *path)
16663
/*[clinic end generated code: output=c3c3b78ecff2914f input=f608743e60a3211e]*/
16664
3.01k
{
16665
3.01k
    return PyOS_FSPath(path);
16666
3.01k
}
16667
16668
#ifdef HAVE_GETRANDOM_SYSCALL
16669
/*[clinic input]
16670
os.getrandom
16671
16672
    size: Py_ssize_t
16673
    flags: int=0
16674
16675
Obtain a series of random bytes.
16676
[clinic start generated code]*/
16677
16678
static PyObject *
16679
os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
16680
/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
16681
0
{
16682
0
    PyObject *bytes;
16683
0
    Py_ssize_t n;
16684
16685
0
    if (size < 0) {
16686
0
        errno = EINVAL;
16687
0
        return posix_error();
16688
0
    }
16689
16690
0
    bytes = PyBytes_FromStringAndSize(NULL, size);
16691
0
    if (bytes == NULL) {
16692
0
        PyErr_NoMemory();
16693
0
        return NULL;
16694
0
    }
16695
16696
0
    while (1) {
16697
0
        n = syscall(SYS_getrandom,
16698
0
                    PyBytes_AS_STRING(bytes),
16699
0
                    PyBytes_GET_SIZE(bytes),
16700
0
                    flags);
16701
0
        if (n < 0 && errno == EINTR) {
16702
0
            if (PyErr_CheckSignals() < 0) {
16703
0
                goto error;
16704
0
            }
16705
16706
            /* getrandom() was interrupted by a signal: retry */
16707
0
            continue;
16708
0
        }
16709
0
        break;
16710
0
    }
16711
16712
0
    if (n < 0) {
16713
0
        PyErr_SetFromErrno(PyExc_OSError);
16714
0
        goto error;
16715
0
    }
16716
16717
0
    if (n != size) {
16718
0
        _PyBytes_Resize(&bytes, n);
16719
0
    }
16720
16721
0
    return bytes;
16722
16723
0
error:
16724
0
    Py_DECREF(bytes);
16725
0
    return NULL;
16726
0
}
16727
#endif   /* HAVE_GETRANDOM_SYSCALL */
16728
16729
#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
16730
16731
/* bpo-36085: Helper functions for managing DLL search directories
16732
 * on win32
16733
 */
16734
16735
/*[clinic input]
16736
os._add_dll_directory
16737
16738
    path: path_t
16739
16740
Add a path to the DLL search path.
16741
16742
This search path is used when resolving dependencies for imported
16743
extension modules (the module itself is resolved through sys.path),
16744
and also by ctypes.
16745
16746
Returns an opaque value that may be passed to os.remove_dll_directory
16747
to remove this directory from the search path.
16748
[clinic start generated code]*/
16749
16750
static PyObject *
16751
os__add_dll_directory_impl(PyObject *module, path_t *path)
16752
/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
16753
{
16754
    DLL_DIRECTORY_COOKIE cookie = 0;
16755
    DWORD err = 0;
16756
16757
    if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
16758
        return NULL;
16759
    }
16760
16761
    Py_BEGIN_ALLOW_THREADS
16762
    if (!(cookie = AddDllDirectory(path->wide))) {
16763
        err = GetLastError();
16764
    }
16765
    Py_END_ALLOW_THREADS
16766
16767
    if (err) {
16768
        return win32_error_object_err("add_dll_directory",
16769
                                      path->object, err);
16770
    }
16771
16772
    return PyCapsule_New(cookie, "DLL directory cookie", NULL);
16773
}
16774
16775
/*[clinic input]
16776
os._remove_dll_directory
16777
16778
    cookie: object
16779
16780
Removes a path from the DLL search path.
16781
16782
The parameter is an opaque value that was returned from
16783
os.add_dll_directory. You can only remove directories that you added
16784
yourself.
16785
[clinic start generated code]*/
16786
16787
static PyObject *
16788
os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
16789
/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
16790
{
16791
    DLL_DIRECTORY_COOKIE cookieValue;
16792
    DWORD err = 0;
16793
16794
    if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
16795
        PyErr_SetString(PyExc_TypeError,
16796
            "Provided cookie was not returned from os.add_dll_directory");
16797
        return NULL;
16798
    }
16799
16800
    cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
16801
        cookie, "DLL directory cookie");
16802
16803
    Py_BEGIN_ALLOW_THREADS
16804
    if (!RemoveDllDirectory(cookieValue)) {
16805
        err = GetLastError();
16806
    }
16807
    Py_END_ALLOW_THREADS
16808
16809
    if (err) {
16810
        return win32_error_object_err("remove_dll_directory",
16811
                                      NULL, err);
16812
    }
16813
16814
    if (PyCapsule_SetName(cookie, NULL)) {
16815
        return NULL;
16816
    }
16817
16818
    Py_RETURN_NONE;
16819
}
16820
16821
#endif /* MS_WINDOWS_APP || MS_WINDOWS_SYSTEM */
16822
16823
16824
/* Only check if WIFEXITED is available: expect that it comes
16825
   with WEXITSTATUS, WIFSIGNALED, etc.
16826
16827
   os.waitstatus_to_exitcode() is implemented in C and not in Python, so
16828
   subprocess can safely call it during late Python finalization without
16829
   risking that used os attributes were set to None by finalize_modules(). */
16830
#if defined(WIFEXITED) || defined(MS_WINDOWS)
16831
/*[clinic input]
16832
os.waitstatus_to_exitcode
16833
16834
    status as status_obj: object
16835
16836
Convert a wait status to an exit code.
16837
16838
On Unix:
16839
16840
* If WIFEXITED(status) is true, return WEXITSTATUS(status).
16841
* If WIFSIGNALED(status) is true, return -WTERMSIG(status).
16842
* Otherwise, raise a ValueError.
16843
16844
On Windows, return status shifted right by 8 bits.
16845
16846
On Unix, if the process is being traced or if waitpid() was called with
16847
WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
16848
This function must not be called if WIFSTOPPED(status) is true.
16849
[clinic start generated code]*/
16850
16851
static PyObject *
16852
os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
16853
/*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/
16854
0
{
16855
0
#ifndef MS_WINDOWS
16856
0
    int status = PyLong_AsInt(status_obj);
16857
0
    if (status == -1 && PyErr_Occurred()) {
16858
0
        return NULL;
16859
0
    }
16860
16861
0
    WAIT_TYPE wait_status;
16862
0
    WAIT_STATUS_INT(wait_status) = status;
16863
0
    int exitcode;
16864
0
    if (WIFEXITED(wait_status)) {
16865
0
        exitcode = WEXITSTATUS(wait_status);
16866
        /* Sanity check to provide warranty on the function behavior.
16867
           It should not occur in practice */
16868
0
        if (exitcode < 0) {
16869
0
            PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
16870
0
            return NULL;
16871
0
        }
16872
0
    }
16873
0
    else if (WIFSIGNALED(wait_status)) {
16874
0
        int signum = WTERMSIG(wait_status);
16875
        /* Sanity check to provide warranty on the function behavior.
16876
           It should not occurs in practice */
16877
0
        if (signum <= 0) {
16878
0
            PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
16879
0
            return NULL;
16880
0
        }
16881
0
        exitcode = -signum;
16882
0
    } else if (WIFSTOPPED(wait_status)) {
16883
        /* Status only received if the process is being traced
16884
           or if waitpid() was called with WUNTRACED option. */
16885
0
        int signum = WSTOPSIG(wait_status);
16886
0
        PyErr_Format(PyExc_ValueError,
16887
0
                     "process stopped by delivery of signal %i",
16888
0
                     signum);
16889
0
        return NULL;
16890
0
    }
16891
0
    else {
16892
0
        PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
16893
0
        return NULL;
16894
0
    }
16895
0
    return PyLong_FromLong(exitcode);
16896
#else
16897
    /* Windows implementation: see os.waitpid() implementation
16898
       which uses _cwait(). */
16899
    unsigned long long status = PyLong_AsUnsignedLongLong(status_obj);
16900
    if (status == (unsigned long long)-1 && PyErr_Occurred()) {
16901
        return NULL;
16902
    }
16903
16904
    unsigned long long exitcode = (status >> 8);
16905
    /* ExitProcess() accepts an UINT type:
16906
       reject exit code which doesn't fit in an UINT */
16907
    if (exitcode > UINT_MAX) {
16908
        PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode);
16909
        return NULL;
16910
    }
16911
    return PyLong_FromUnsignedLong((unsigned long)exitcode);
16912
#endif
16913
0
}
16914
#endif
16915
16916
#if defined(MS_WINDOWS)
16917
/*[clinic input]
16918
os._supports_virtual_terminal
16919
16920
Checks if virtual terminal is supported in windows
16921
[clinic start generated code]*/
16922
16923
static PyObject *
16924
os__supports_virtual_terminal_impl(PyObject *module)
16925
/*[clinic end generated code: output=bd0556a6d9d99fe6 input=0752c98e5d321542]*/
16926
{
16927
#ifdef HAVE_WINDOWS_CONSOLE_IO
16928
    DWORD mode = 0;
16929
    HANDLE handle = GetStdHandle(STD_ERROR_HANDLE);
16930
    if (!GetConsoleMode(handle, &mode)) {
16931
        Py_RETURN_FALSE;
16932
    }
16933
    return PyBool_FromLong(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING);
16934
#else
16935
    Py_RETURN_FALSE;
16936
#endif /* HAVE_WINDOWS_CONSOLE_IO */
16937
}
16938
#endif
16939
16940
/*[clinic input]
16941
os._inputhook
16942
16943
Calls PyOS_CallInputHook droppong the GIL first
16944
[clinic start generated code]*/
16945
16946
static PyObject *
16947
os__inputhook_impl(PyObject *module)
16948
/*[clinic end generated code: output=525aca4ef3c6149f input=fc531701930d064f]*/
16949
0
{
16950
0
     int result = 0;
16951
0
     if (PyOS_InputHook) {
16952
0
         Py_BEGIN_ALLOW_THREADS;
16953
0
         result = PyOS_InputHook();
16954
0
         Py_END_ALLOW_THREADS;
16955
0
     }
16956
0
     return PyLong_FromLong(result);
16957
0
}
16958
16959
/*[clinic input]
16960
os._is_inputhook_installed
16961
16962
Checks if PyOS_CallInputHook is set
16963
[clinic start generated code]*/
16964
16965
static PyObject *
16966
os__is_inputhook_installed_impl(PyObject *module)
16967
/*[clinic end generated code: output=3b3eab4f672c689a input=ff177c9938dd76d8]*/
16968
0
{
16969
0
    return PyBool_FromLong(PyOS_InputHook != NULL);
16970
0
}
16971
16972
/*[clinic input]
16973
os._create_environ
16974
16975
Create the environment dictionary.
16976
[clinic start generated code]*/
16977
16978
static PyObject *
16979
os__create_environ_impl(PyObject *module)
16980
/*[clinic end generated code: output=19d9039ab14f8ad4 input=a4c05686b34635e8]*/
16981
0
{
16982
0
    return convertenviron();
16983
0
}
16984
16985
16986
#ifdef __EMSCRIPTEN__
16987
/*[clinic input]
16988
os._emscripten_debugger
16989
16990
Create a breakpoint for the JavaScript debugger. Emscripten only.
16991
[clinic start generated code]*/
16992
16993
static PyObject *
16994
os__emscripten_debugger_impl(PyObject *module)
16995
/*[clinic end generated code: output=ad47dc3bf0661343 input=d814b1877fb6083a]*/
16996
{
16997
    emscripten_debugger();
16998
    Py_RETURN_NONE;
16999
}
17000
17001
EM_JS(void, emscripten_log_impl_js, (const char* arg), {
17002
    console.warn(UTF8ToString(arg));
17003
});
17004
17005
/*[clinic input]
17006
os._emscripten_log
17007
   arg: str
17008
17009
Log something to the JS console. Emscripten only.
17010
[clinic start generated code]*/
17011
17012
static PyObject *
17013
os__emscripten_log_impl(PyObject *module, const char *arg)
17014
/*[clinic end generated code: output=9749e5e293c42784 input=350aa1f70bc1e905]*/
17015
{
17016
    emscripten_log_impl_js(arg);
17017
    Py_RETURN_NONE;
17018
}
17019
#endif /* __EMSCRIPTEN__ */
17020
17021
17022
static PyMethodDef posix_methods[] = {
17023
    OS_STAT_METHODDEF
17024
    OS_ACCESS_METHODDEF
17025
    OS_TTYNAME_METHODDEF
17026
    OS_CHDIR_METHODDEF
17027
    OS_CHFLAGS_METHODDEF
17028
    OS_CHMOD_METHODDEF
17029
    OS_FCHMOD_METHODDEF
17030
    OS_LCHMOD_METHODDEF
17031
    OS_CHOWN_METHODDEF
17032
    OS_FCHOWN_METHODDEF
17033
    OS_LCHOWN_METHODDEF
17034
    OS_LCHFLAGS_METHODDEF
17035
    OS_CHROOT_METHODDEF
17036
    OS_CTERMID_METHODDEF
17037
    OS_GETCWD_METHODDEF
17038
    OS_GETCWDB_METHODDEF
17039
    OS_LINK_METHODDEF
17040
    OS_LISTDIR_METHODDEF
17041
    OS_LISTDRIVES_METHODDEF
17042
    OS_LISTMOUNTS_METHODDEF
17043
    OS_LISTVOLUMES_METHODDEF
17044
    OS_LSTAT_METHODDEF
17045
    OS_MKDIR_METHODDEF
17046
    OS_NICE_METHODDEF
17047
    OS_GETPRIORITY_METHODDEF
17048
    OS_SETPRIORITY_METHODDEF
17049
    OS_POSIX_SPAWN_METHODDEF
17050
    OS_POSIX_SPAWNP_METHODDEF
17051
    OS_READLINK_METHODDEF
17052
    OS_COPY_FILE_RANGE_METHODDEF
17053
    OS_SPLICE_METHODDEF
17054
    OS_RENAME_METHODDEF
17055
    OS_REPLACE_METHODDEF
17056
    OS_RMDIR_METHODDEF
17057
    OS_SYMLINK_METHODDEF
17058
    OS_SYSTEM_METHODDEF
17059
    OS_UMASK_METHODDEF
17060
    OS_UNAME_METHODDEF
17061
    OS_UNLINK_METHODDEF
17062
    OS_REMOVE_METHODDEF
17063
    OS_UTIME_METHODDEF
17064
    OS_TIMES_METHODDEF
17065
    OS__EXIT_METHODDEF
17066
    OS__FCOPYFILE_METHODDEF
17067
    OS_EXECV_METHODDEF
17068
    OS_EXECVE_METHODDEF
17069
    OS_SPAWNV_METHODDEF
17070
    OS_SPAWNVE_METHODDEF
17071
    OS_FORK1_METHODDEF
17072
    OS_FORK_METHODDEF
17073
    OS_REGISTER_AT_FORK_METHODDEF
17074
    OS_SCHED_GET_PRIORITY_MAX_METHODDEF
17075
    OS_SCHED_GET_PRIORITY_MIN_METHODDEF
17076
    OS_SCHED_GETPARAM_METHODDEF
17077
    OS_SCHED_GETSCHEDULER_METHODDEF
17078
    OS_SCHED_RR_GET_INTERVAL_METHODDEF
17079
    OS_SCHED_SETPARAM_METHODDEF
17080
    OS_SCHED_SETSCHEDULER_METHODDEF
17081
    OS_SCHED_YIELD_METHODDEF
17082
    OS_SCHED_SETAFFINITY_METHODDEF
17083
    OS_SCHED_GETAFFINITY_METHODDEF
17084
    OS_POSIX_OPENPT_METHODDEF
17085
    OS_GRANTPT_METHODDEF
17086
    OS_UNLOCKPT_METHODDEF
17087
    OS_PTSNAME_METHODDEF
17088
    OS_OPENPTY_METHODDEF
17089
    OS_LOGIN_TTY_METHODDEF
17090
    OS_FORKPTY_METHODDEF
17091
    OS_GETEGID_METHODDEF
17092
    OS_GETEUID_METHODDEF
17093
    OS_GETGID_METHODDEF
17094
    OS_GETGROUPLIST_METHODDEF
17095
    OS_GETGROUPS_METHODDEF
17096
    OS_GETPID_METHODDEF
17097
    OS_GETPGRP_METHODDEF
17098
    OS_GETPPID_METHODDEF
17099
    OS_GETUID_METHODDEF
17100
    OS_GETLOGIN_METHODDEF
17101
    OS_KILL_METHODDEF
17102
    OS_KILLPG_METHODDEF
17103
    OS_PLOCK_METHODDEF
17104
    OS_STARTFILE_METHODDEF
17105
    OS_SETUID_METHODDEF
17106
    OS_SETEUID_METHODDEF
17107
    OS_SETREUID_METHODDEF
17108
    OS_SETGID_METHODDEF
17109
    OS_SETEGID_METHODDEF
17110
    OS_SETREGID_METHODDEF
17111
    OS_SETGROUPS_METHODDEF
17112
    OS_INITGROUPS_METHODDEF
17113
    OS_GETPGID_METHODDEF
17114
    OS_SETPGRP_METHODDEF
17115
    OS_WAIT_METHODDEF
17116
    OS_WAIT3_METHODDEF
17117
    OS_WAIT4_METHODDEF
17118
    OS_WAITID_METHODDEF
17119
    OS_WAITPID_METHODDEF
17120
    OS_PIDFD_OPEN_METHODDEF
17121
    OS_GETSID_METHODDEF
17122
    OS_SETSID_METHODDEF
17123
    OS_SETPGID_METHODDEF
17124
    OS_TCGETPGRP_METHODDEF
17125
    OS_TCSETPGRP_METHODDEF
17126
    OS_OPEN_METHODDEF
17127
    OS_CLOSE_METHODDEF
17128
    OS_CLOSERANGE_METHODDEF
17129
    OS_DEVICE_ENCODING_METHODDEF
17130
    OS_DUP_METHODDEF
17131
    OS_DUP2_METHODDEF
17132
    OS_LOCKF_METHODDEF
17133
    OS_LSEEK_METHODDEF
17134
    OS_READ_METHODDEF
17135
    OS_READINTO_METHODDEF
17136
    OS_READV_METHODDEF
17137
    OS_PREAD_METHODDEF
17138
    OS_PREADV_METHODDEF
17139
    OS_WRITE_METHODDEF
17140
    OS_WRITEV_METHODDEF
17141
    OS_PWRITE_METHODDEF
17142
    OS_PWRITEV_METHODDEF
17143
    OS_SENDFILE_METHODDEF
17144
    OS_FSTAT_METHODDEF
17145
    OS_ISATTY_METHODDEF
17146
    OS_PIPE_METHODDEF
17147
    OS_PIPE2_METHODDEF
17148
    OS_MKFIFO_METHODDEF
17149
    OS_MKNOD_METHODDEF
17150
    OS_MAJOR_METHODDEF
17151
    OS_MINOR_METHODDEF
17152
    OS_MAKEDEV_METHODDEF
17153
    OS_FTRUNCATE_METHODDEF
17154
    OS_TRUNCATE_METHODDEF
17155
    OS_POSIX_FALLOCATE_METHODDEF
17156
    OS_POSIX_FADVISE_METHODDEF
17157
    OS_PUTENV_METHODDEF
17158
    OS_UNSETENV_METHODDEF
17159
    OS_STRERROR_METHODDEF
17160
    OS_FCHDIR_METHODDEF
17161
    OS_FSYNC_METHODDEF
17162
    OS_SYNC_METHODDEF
17163
    OS_FDATASYNC_METHODDEF
17164
    OS_WCOREDUMP_METHODDEF
17165
    OS_WIFCONTINUED_METHODDEF
17166
    OS_WIFSTOPPED_METHODDEF
17167
    OS_WIFSIGNALED_METHODDEF
17168
    OS_WIFEXITED_METHODDEF
17169
    OS_WEXITSTATUS_METHODDEF
17170
    OS_WTERMSIG_METHODDEF
17171
    OS_WSTOPSIG_METHODDEF
17172
    OS_FSTATVFS_METHODDEF
17173
    OS_STATVFS_METHODDEF
17174
    OS_CONFSTR_METHODDEF
17175
    OS_SYSCONF_METHODDEF
17176
    OS_FPATHCONF_METHODDEF
17177
    OS_PATHCONF_METHODDEF
17178
    OS_ABORT_METHODDEF
17179
    OS__GETFULLPATHNAME_METHODDEF
17180
    OS__GETDISKUSAGE_METHODDEF
17181
    OS__GETFINALPATHNAME_METHODDEF
17182
    OS__FINDFIRSTFILE_METHODDEF
17183
    OS__GETVOLUMEPATHNAME_METHODDEF
17184
    OS__PATH_SPLITROOT_METHODDEF
17185
    OS__PATH_SPLITROOT_EX_METHODDEF
17186
    OS__PATH_NORMPATH_METHODDEF
17187
    OS_GETLOADAVG_METHODDEF
17188
    OS_URANDOM_METHODDEF
17189
    OS_SETRESUID_METHODDEF
17190
    OS_SETRESGID_METHODDEF
17191
    OS_GETRESUID_METHODDEF
17192
    OS_GETRESGID_METHODDEF
17193
17194
    OS_GETXATTR_METHODDEF
17195
    OS_SETXATTR_METHODDEF
17196
    OS_REMOVEXATTR_METHODDEF
17197
    OS_LISTXATTR_METHODDEF
17198
17199
    OS_GET_TERMINAL_SIZE_METHODDEF
17200
    OS_CPU_COUNT_METHODDEF
17201
    OS_GET_INHERITABLE_METHODDEF
17202
    OS_SET_INHERITABLE_METHODDEF
17203
    OS_GET_HANDLE_INHERITABLE_METHODDEF
17204
    OS_SET_HANDLE_INHERITABLE_METHODDEF
17205
    OS_GET_BLOCKING_METHODDEF
17206
    OS_SET_BLOCKING_METHODDEF
17207
    OS_SCANDIR_METHODDEF
17208
    OS_FSPATH_METHODDEF
17209
    OS_GETRANDOM_METHODDEF
17210
    OS_MEMFD_CREATE_METHODDEF
17211
    OS_EVENTFD_METHODDEF
17212
    OS_EVENTFD_READ_METHODDEF
17213
    OS_EVENTFD_WRITE_METHODDEF
17214
    OS__ADD_DLL_DIRECTORY_METHODDEF
17215
    OS__REMOVE_DLL_DIRECTORY_METHODDEF
17216
    OS_WAITSTATUS_TO_EXITCODE_METHODDEF
17217
    OS_SETNS_METHODDEF
17218
    OS_UNSHARE_METHODDEF
17219
    OS_TIMERFD_CREATE_METHODDEF
17220
    OS_TIMERFD_SETTIME_METHODDEF
17221
    OS_TIMERFD_SETTIME_NS_METHODDEF
17222
    OS_TIMERFD_GETTIME_METHODDEF
17223
    OS_TIMERFD_GETTIME_NS_METHODDEF
17224
17225
    OS__PATH_ISDEVDRIVE_METHODDEF
17226
    OS__PATH_ISDIR_METHODDEF
17227
    OS__PATH_ISFILE_METHODDEF
17228
    OS__PATH_ISLINK_METHODDEF
17229
    OS__PATH_ISJUNCTION_METHODDEF
17230
    OS__PATH_EXISTS_METHODDEF
17231
    OS__PATH_LEXISTS_METHODDEF
17232
17233
    OS__SUPPORTS_VIRTUAL_TERMINAL_METHODDEF
17234
    OS__INPUTHOOK_METHODDEF
17235
    OS__IS_INPUTHOOK_INSTALLED_METHODDEF
17236
    OS__CREATE_ENVIRON_METHODDEF
17237
    OS__EMSCRIPTEN_DEBUGGER_METHODDEF
17238
    OS__EMSCRIPTEN_LOG_METHODDEF
17239
    {NULL,              NULL}            /* Sentinel */
17240
};
17241
17242
static int
17243
all_ins(PyObject *m)
17244
16
{
17245
16
#ifdef F_OK
17246
16
    if (PyModule_AddIntMacro(m, F_OK)) return -1;
17247
16
#endif
17248
16
#ifdef R_OK
17249
16
    if (PyModule_AddIntMacro(m, R_OK)) return -1;
17250
16
#endif
17251
16
#ifdef W_OK
17252
16
    if (PyModule_AddIntMacro(m, W_OK)) return -1;
17253
16
#endif
17254
16
#ifdef X_OK
17255
16
    if (PyModule_AddIntMacro(m, X_OK)) return -1;
17256
16
#endif
17257
16
#ifdef NGROUPS_MAX
17258
16
    if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
17259
16
#endif
17260
16
#ifdef TMP_MAX
17261
16
    if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
17262
16
#endif
17263
16
#ifdef WCONTINUED
17264
16
    if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
17265
16
#endif
17266
16
#ifdef WNOHANG
17267
16
    if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
17268
16
#endif
17269
16
#ifdef WUNTRACED
17270
16
    if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
17271
16
#endif
17272
16
#ifdef O_RDONLY
17273
16
    if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
17274
16
#endif
17275
16
#ifdef O_WRONLY
17276
16
    if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
17277
16
#endif
17278
16
#ifdef O_RDWR
17279
16
    if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
17280
16
#endif
17281
16
#ifdef O_NDELAY
17282
16
    if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
17283
16
#endif
17284
16
#ifdef O_NONBLOCK
17285
16
    if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
17286
16
#endif
17287
16
#ifdef O_APPEND
17288
16
    if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
17289
16
#endif
17290
16
#ifdef O_DSYNC
17291
16
    if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
17292
16
#endif
17293
16
#ifdef O_RSYNC
17294
16
    if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
17295
16
#endif
17296
16
#ifdef O_SYNC
17297
16
    if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
17298
16
#endif
17299
16
#ifdef O_NOCTTY
17300
16
    if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
17301
16
#endif
17302
16
#ifdef O_CREAT
17303
16
    if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
17304
16
#endif
17305
16
#ifdef O_EXCL
17306
16
    if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
17307
16
#endif
17308
16
#ifdef O_TRUNC
17309
16
    if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
17310
16
#endif
17311
#ifdef O_BINARY
17312
    if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
17313
#endif
17314
#ifdef O_TEXT
17315
    if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
17316
#endif
17317
#ifdef O_XATTR
17318
    if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
17319
#endif
17320
16
#ifdef O_LARGEFILE
17321
16
    if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
17322
16
#endif
17323
16
#ifndef __GNU__
17324
#ifdef O_SHLOCK
17325
    if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
17326
#endif
17327
#ifdef O_EXLOCK
17328
    if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
17329
#endif
17330
16
#endif
17331
#ifdef O_EXEC
17332
    if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
17333
#endif
17334
#ifdef O_SEARCH
17335
    if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
17336
#endif
17337
16
#ifdef O_PATH
17338
16
    if (PyModule_AddIntMacro(m, O_PATH)) return -1;
17339
16
#endif
17340
#ifdef O_TTY_INIT
17341
    if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
17342
#endif
17343
16
#ifdef O_TMPFILE
17344
16
    if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
17345
16
#endif
17346
16
#ifdef PRIO_PROCESS
17347
16
    if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
17348
16
#endif
17349
16
#ifdef PRIO_PGRP
17350
16
    if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
17351
16
#endif
17352
16
#ifdef PRIO_USER
17353
16
    if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
17354
16
#endif
17355
#ifdef PRIO_DARWIN_THREAD
17356
    if (PyModule_AddIntMacro(m, PRIO_DARWIN_THREAD)) return -1;
17357
#endif
17358
#ifdef PRIO_DARWIN_PROCESS
17359
    if (PyModule_AddIntMacro(m, PRIO_DARWIN_PROCESS)) return -1;
17360
#endif
17361
#ifdef PRIO_DARWIN_BG
17362
    if (PyModule_AddIntMacro(m, PRIO_DARWIN_BG)) return -1;
17363
#endif
17364
#ifdef PRIO_DARWIN_NONUI
17365
    if (PyModule_AddIntMacro(m, PRIO_DARWIN_NONUI)) return -1;
17366
#endif
17367
16
#ifdef O_CLOEXEC
17368
16
    if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
17369
16
#endif
17370
16
#ifdef O_ACCMODE
17371
16
    if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
17372
16
#endif
17373
#ifdef O_EVTONLY
17374
    if (PyModule_AddIntMacro(m, O_EVTONLY)) return -1;
17375
#endif
17376
16
#ifdef O_FSYNC
17377
16
    if (PyModule_AddIntMacro(m, O_FSYNC)) return -1;
17378
16
#endif
17379
#ifdef O_SYMLINK
17380
    if (PyModule_AddIntMacro(m, O_SYMLINK)) return -1;
17381
#endif
17382
17383
16
#ifdef SEEK_HOLE
17384
16
    if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
17385
16
#endif
17386
16
#ifdef SEEK_DATA
17387
16
    if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
17388
16
#endif
17389
17390
/* MS Windows */
17391
#ifdef O_NOINHERIT
17392
    /* Don't inherit in child processes. */
17393
    if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
17394
#endif
17395
#ifdef _O_SHORT_LIVED
17396
    /* Optimize for short life (keep in memory). */
17397
    /* MS forgot to define this one with a non-underscore form too. */
17398
    if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
17399
#endif
17400
#ifdef O_TEMPORARY
17401
    /* Automatically delete when last handle is closed. */
17402
    if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
17403
#endif
17404
#ifdef O_RANDOM
17405
    /* Optimize for random access. */
17406
    if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
17407
#endif
17408
#ifdef O_SEQUENTIAL
17409
    /* Optimize for sequential access. */
17410
    if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
17411
#endif
17412
17413
/* GNU extensions. */
17414
16
#ifdef O_ASYNC
17415
    /* Send a SIGIO signal whenever input or output
17416
       becomes available on file descriptor */
17417
16
    if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
17418
16
#endif
17419
16
#ifdef O_DIRECT
17420
    /* Direct disk access. */
17421
16
    if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
17422
16
#endif
17423
16
#ifdef O_DIRECTORY
17424
    /* Must be a directory.      */
17425
16
    if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
17426
16
#endif
17427
16
#ifdef O_NOFOLLOW
17428
    /* Do not follow links.      */
17429
16
    if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
17430
16
#endif
17431
#ifdef O_NOFOLLOW_ANY
17432
    if (PyModule_AddIntMacro(m, O_NOFOLLOW_ANY)) return -1;
17433
#endif
17434
#ifdef O_NOLINKS
17435
    /* Fails if link count of the named file is greater than 1 */
17436
    if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
17437
#endif
17438
16
#ifdef O_NOATIME
17439
    /* Do not update the access time. */
17440
16
    if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
17441
16
#endif
17442
17443
    /* These come from sysexits.h */
17444
16
#ifdef EX_OK
17445
16
    if (PyModule_AddIntMacro(m, EX_OK)) return -1;
17446
16
#endif /* EX_OK */
17447
16
#ifdef EX_USAGE
17448
16
    if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
17449
16
#endif /* EX_USAGE */
17450
16
#ifdef EX_DATAERR
17451
16
    if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
17452
16
#endif /* EX_DATAERR */
17453
16
#ifdef EX_NOINPUT
17454
16
    if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
17455
16
#endif /* EX_NOINPUT */
17456
16
#ifdef EX_NOUSER
17457
16
    if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
17458
16
#endif /* EX_NOUSER */
17459
16
#ifdef EX_NOHOST
17460
16
    if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
17461
16
#endif /* EX_NOHOST */
17462
16
#ifdef EX_UNAVAILABLE
17463
16
    if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
17464
16
#endif /* EX_UNAVAILABLE */
17465
16
#ifdef EX_SOFTWARE
17466
16
    if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
17467
16
#endif /* EX_SOFTWARE */
17468
16
#ifdef EX_OSERR
17469
16
    if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
17470
16
#endif /* EX_OSERR */
17471
16
#ifdef EX_OSFILE
17472
16
    if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
17473
16
#endif /* EX_OSFILE */
17474
16
#ifdef EX_CANTCREAT
17475
16
    if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
17476
16
#endif /* EX_CANTCREAT */
17477
16
#ifdef EX_IOERR
17478
16
    if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
17479
16
#endif /* EX_IOERR */
17480
16
#ifdef EX_TEMPFAIL
17481
16
    if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
17482
16
#endif /* EX_TEMPFAIL */
17483
16
#ifdef EX_PROTOCOL
17484
16
    if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
17485
16
#endif /* EX_PROTOCOL */
17486
16
#ifdef EX_NOPERM
17487
16
    if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
17488
16
#endif /* EX_NOPERM */
17489
16
#ifdef EX_CONFIG
17490
16
    if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
17491
16
#endif /* EX_CONFIG */
17492
#ifdef EX_NOTFOUND
17493
    if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
17494
#endif /* EX_NOTFOUND */
17495
17496
    /* statvfs */
17497
16
#ifdef ST_RDONLY
17498
16
    if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
17499
16
#endif /* ST_RDONLY */
17500
16
#ifdef ST_NOSUID
17501
16
    if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
17502
16
#endif /* ST_NOSUID */
17503
17504
       /* GNU extensions */
17505
16
#ifdef ST_NODEV
17506
16
    if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
17507
16
#endif /* ST_NODEV */
17508
16
#ifdef ST_NOEXEC
17509
16
    if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
17510
16
#endif /* ST_NOEXEC */
17511
16
#ifdef ST_SYNCHRONOUS
17512
16
    if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
17513
16
#endif /* ST_SYNCHRONOUS */
17514
16
#ifdef ST_MANDLOCK
17515
16
    if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
17516
16
#endif /* ST_MANDLOCK */
17517
16
#ifdef ST_WRITE
17518
16
    if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
17519
16
#endif /* ST_WRITE */
17520
16
#ifdef ST_APPEND
17521
16
    if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
17522
16
#endif /* ST_APPEND */
17523
16
#ifdef ST_NOATIME
17524
16
    if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
17525
16
#endif /* ST_NOATIME */
17526
16
#ifdef ST_NODIRATIME
17527
16
    if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
17528
16
#endif /* ST_NODIRATIME */
17529
16
#ifdef ST_RELATIME
17530
16
    if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
17531
16
#endif /* ST_RELATIME */
17532
17533
    /* FreeBSD sendfile() constants */
17534
#ifdef SF_NODISKIO
17535
    if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
17536
#endif
17537
    /* is obsolete since the 11.x release */
17538
#ifdef SF_MNOWAIT
17539
    if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
17540
#endif
17541
#ifdef SF_SYNC
17542
    if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
17543
#endif
17544
#ifdef SF_NOCACHE
17545
    if (PyModule_AddIntMacro(m, SF_NOCACHE)) return -1;
17546
#endif
17547
17548
16
#ifdef TFD_NONBLOCK
17549
16
    if (PyModule_AddIntMacro(m, TFD_NONBLOCK)) return -1;
17550
16
#endif
17551
16
#ifdef TFD_CLOEXEC
17552
16
    if (PyModule_AddIntMacro(m, TFD_CLOEXEC)) return -1;
17553
16
#endif
17554
16
#ifdef TFD_TIMER_ABSTIME
17555
16
    if (PyModule_AddIntMacro(m, TFD_TIMER_ABSTIME)) return -1;
17556
16
#endif
17557
16
#ifdef TFD_TIMER_CANCEL_ON_SET
17558
16
    if (PyModule_AddIntMacro(m, TFD_TIMER_CANCEL_ON_SET)) return -1;
17559
16
#endif
17560
17561
    /* constants for posix_fadvise */
17562
16
#ifdef POSIX_FADV_NORMAL
17563
16
    if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
17564
16
#endif
17565
16
#ifdef POSIX_FADV_SEQUENTIAL
17566
16
    if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
17567
16
#endif
17568
16
#ifdef POSIX_FADV_RANDOM
17569
16
    if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
17570
16
#endif
17571
16
#ifdef POSIX_FADV_NOREUSE
17572
16
    if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
17573
16
#endif
17574
16
#ifdef POSIX_FADV_WILLNEED
17575
16
    if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
17576
16
#endif
17577
16
#ifdef POSIX_FADV_DONTNEED
17578
16
    if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
17579
16
#endif
17580
17581
    /* constants for waitid */
17582
16
#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
17583
16
    if (PyModule_AddIntMacro(m, P_PID)) return -1;
17584
16
    if (PyModule_AddIntMacro(m, P_PGID)) return -1;
17585
16
    if (PyModule_AddIntMacro(m, P_ALL)) return -1;
17586
16
#ifdef P_PIDFD
17587
16
    if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
17588
16
#endif
17589
#ifdef PIDFD_NONBLOCK
17590
    if (PyModule_AddIntMacro(m, PIDFD_NONBLOCK)) return -1;
17591
#endif
17592
16
#endif
17593
16
#ifdef WEXITED
17594
16
    if (PyModule_AddIntMacro(m, WEXITED)) return -1;
17595
16
#endif
17596
16
#ifdef WNOWAIT
17597
16
    if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
17598
16
#endif
17599
16
#ifdef WSTOPPED
17600
16
    if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
17601
16
#endif
17602
16
#ifdef CLD_EXITED
17603
16
    if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
17604
16
#endif
17605
16
#ifdef CLD_KILLED
17606
16
    if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
17607
16
#endif
17608
16
#ifdef CLD_DUMPED
17609
16
    if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
17610
16
#endif
17611
16
#ifdef CLD_TRAPPED
17612
16
    if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
17613
16
#endif
17614
16
#ifdef CLD_STOPPED
17615
16
    if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
17616
16
#endif
17617
16
#ifdef CLD_CONTINUED
17618
16
    if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
17619
16
#endif
17620
17621
    /* constants for lockf */
17622
16
#ifdef F_LOCK
17623
16
    if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
17624
16
#endif
17625
16
#ifdef F_TLOCK
17626
16
    if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
17627
16
#endif
17628
16
#ifdef F_ULOCK
17629
16
    if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
17630
16
#endif
17631
16
#ifdef F_TEST
17632
16
    if (PyModule_AddIntMacro(m, F_TEST)) return -1;
17633
16
#endif
17634
17635
16
#ifdef RWF_DSYNC
17636
16
    if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
17637
16
#endif
17638
16
#ifdef RWF_HIPRI
17639
16
    if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
17640
16
#endif
17641
16
#ifdef RWF_SYNC
17642
16
    if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
17643
16
#endif
17644
16
#ifdef RWF_NOWAIT
17645
16
    if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
17646
16
#endif
17647
16
#ifdef RWF_APPEND
17648
16
    if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1;
17649
16
#endif
17650
17651
/* constants for splice */
17652
16
#if defined(HAVE_SPLICE) && defined(__linux__)
17653
16
    if (PyModule_AddIntConstant(m, "SPLICE_F_MOVE", SPLICE_F_MOVE)) return -1;
17654
16
    if (PyModule_AddIntConstant(m, "SPLICE_F_NONBLOCK", SPLICE_F_NONBLOCK)) return -1;
17655
16
    if (PyModule_AddIntConstant(m, "SPLICE_F_MORE", SPLICE_F_MORE)) return -1;
17656
16
#endif
17657
17658
/* constants for posix_spawn */
17659
16
#ifdef HAVE_POSIX_SPAWN
17660
16
    if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
17661
16
    if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
17662
16
    if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
17663
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
17664
    if (PyModule_AddIntMacro(m, POSIX_SPAWN_CLOSEFROM)) return -1;
17665
#endif
17666
16
#endif
17667
17668
#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
17669
    if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
17670
    if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
17671
    if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
17672
#endif
17673
#ifdef HAVE_SPAWNV
17674
    if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
17675
    if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
17676
#endif
17677
17678
16
#ifdef HAVE_SCHED_H
17679
16
#ifdef SCHED_OTHER
17680
16
    if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
17681
16
#endif
17682
16
#ifdef SCHED_DEADLINE
17683
16
    if (PyModule_AddIntMacro(m, SCHED_DEADLINE)) return -1;
17684
16
#endif
17685
16
#ifdef SCHED_FIFO
17686
16
    if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
17687
16
#endif
17688
16
#ifdef SCHED_NORMAL
17689
16
    if (PyModule_AddIntMacro(m, SCHED_NORMAL)) return -1;
17690
16
#endif
17691
16
#ifdef SCHED_RR
17692
16
    if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
17693
16
#endif
17694
#ifdef SCHED_SPORADIC
17695
    if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
17696
#endif
17697
16
#ifdef SCHED_BATCH
17698
16
    if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
17699
16
#endif
17700
16
#ifdef SCHED_IDLE
17701
16
    if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
17702
16
#endif
17703
16
#ifdef SCHED_RESET_ON_FORK
17704
16
    if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
17705
16
#endif
17706
#ifdef SCHED_SYS
17707
    if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
17708
#endif
17709
#ifdef SCHED_IA
17710
    if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
17711
#endif
17712
#ifdef SCHED_FSS
17713
    if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
17714
#endif
17715
#ifdef SCHED_FX
17716
    if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
17717
#endif
17718
17719
/* constants for namespaces */
17720
16
#if defined(HAVE_SETNS) || defined(HAVE_UNSHARE)
17721
16
#ifdef CLONE_FS
17722
16
    if (PyModule_AddIntMacro(m, CLONE_FS)) return -1;
17723
16
#endif
17724
16
#ifdef CLONE_FILES
17725
16
    if (PyModule_AddIntMacro(m, CLONE_FILES)) return -1;
17726
16
#endif
17727
16
#ifdef CLONE_NEWNS
17728
16
    if (PyModule_AddIntMacro(m, CLONE_NEWNS)) return -1;
17729
16
#endif
17730
16
#ifdef CLONE_NEWCGROUP
17731
16
    if (PyModule_AddIntMacro(m, CLONE_NEWCGROUP)) return -1;
17732
16
#endif
17733
16
#ifdef CLONE_NEWUTS
17734
16
    if (PyModule_AddIntMacro(m, CLONE_NEWUTS)) return -1;
17735
16
#endif
17736
16
#ifdef CLONE_NEWIPC
17737
16
    if (PyModule_AddIntMacro(m, CLONE_NEWIPC)) return -1;
17738
16
#endif
17739
16
#ifdef CLONE_NEWUSER
17740
16
    if (PyModule_AddIntMacro(m, CLONE_NEWUSER)) return -1;
17741
16
#endif
17742
16
#ifdef CLONE_NEWPID
17743
16
    if (PyModule_AddIntMacro(m, CLONE_NEWPID)) return -1;
17744
16
#endif
17745
16
#ifdef CLONE_NEWNET
17746
16
    if (PyModule_AddIntMacro(m, CLONE_NEWNET)) return -1;
17747
16
#endif
17748
#ifdef CLONE_NEWTIME
17749
    if (PyModule_AddIntMacro(m, CLONE_NEWTIME)) return -1;
17750
#endif
17751
16
#ifdef CLONE_SYSVSEM
17752
16
    if (PyModule_AddIntMacro(m, CLONE_SYSVSEM)) return -1;
17753
16
#endif
17754
16
#ifdef CLONE_THREAD
17755
16
    if (PyModule_AddIntMacro(m, CLONE_THREAD)) return -1;
17756
16
#endif
17757
16
#ifdef CLONE_SIGHAND
17758
16
    if (PyModule_AddIntMacro(m, CLONE_SIGHAND)) return -1;
17759
16
#endif
17760
16
#ifdef CLONE_VM
17761
16
    if (PyModule_AddIntMacro(m, CLONE_VM)) return -1;
17762
16
#endif
17763
16
#endif
17764
17765
16
#endif
17766
17767
16
#ifdef USE_XATTRS
17768
16
    if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
17769
16
    if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
17770
16
    if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
17771
16
#endif
17772
17773
16
#if HAVE_DECL_RTLD_LAZY
17774
16
    if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
17775
16
#endif
17776
16
#if HAVE_DECL_RTLD_NOW
17777
16
    if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
17778
16
#endif
17779
16
#if HAVE_DECL_RTLD_GLOBAL
17780
16
    if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
17781
16
#endif
17782
16
#if HAVE_DECL_RTLD_LOCAL
17783
16
    if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
17784
16
#endif
17785
16
#if HAVE_DECL_RTLD_NODELETE
17786
16
    if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
17787
16
#endif
17788
16
#if HAVE_DECL_RTLD_NOLOAD
17789
16
    if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
17790
16
#endif
17791
16
#if HAVE_DECL_RTLD_DEEPBIND
17792
16
    if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
17793
16
#endif
17794
#if HAVE_DECL_RTLD_MEMBER
17795
    if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
17796
#endif
17797
17798
16
#ifdef HAVE_GETRANDOM_SYSCALL
17799
16
    if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
17800
16
    if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
17801
16
#endif
17802
16
#ifdef HAVE_MEMFD_CREATE
17803
16
    if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
17804
16
    if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
17805
16
#ifdef MFD_HUGETLB
17806
16
    if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
17807
16
#endif
17808
16
#ifdef MFD_HUGE_SHIFT
17809
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
17810
16
#endif
17811
16
#ifdef MFD_HUGE_MASK
17812
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
17813
16
#endif
17814
16
#ifdef MFD_HUGE_64KB
17815
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
17816
16
#endif
17817
16
#ifdef MFD_HUGE_512KB
17818
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
17819
16
#endif
17820
16
#ifdef MFD_HUGE_1MB
17821
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
17822
16
#endif
17823
16
#ifdef MFD_HUGE_2MB
17824
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
17825
16
#endif
17826
16
#ifdef MFD_HUGE_8MB
17827
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
17828
16
#endif
17829
16
#ifdef MFD_HUGE_16MB
17830
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
17831
16
#endif
17832
16
#ifdef MFD_HUGE_32MB
17833
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
17834
16
#endif
17835
16
#ifdef MFD_HUGE_256MB
17836
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
17837
16
#endif
17838
16
#ifdef MFD_HUGE_512MB
17839
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
17840
16
#endif
17841
16
#ifdef MFD_HUGE_1GB
17842
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
17843
16
#endif
17844
16
#ifdef MFD_HUGE_2GB
17845
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
17846
16
#endif
17847
16
#ifdef MFD_HUGE_16GB
17848
16
    if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
17849
16
#endif
17850
16
#endif /* HAVE_MEMFD_CREATE */
17851
17852
16
#if defined(HAVE_EVENTFD) && defined(EFD_CLOEXEC)
17853
16
    if (PyModule_AddIntMacro(m, EFD_CLOEXEC)) return -1;
17854
16
#ifdef EFD_NONBLOCK
17855
16
    if (PyModule_AddIntMacro(m, EFD_NONBLOCK)) return -1;
17856
16
#endif
17857
16
#ifdef EFD_SEMAPHORE
17858
16
    if (PyModule_AddIntMacro(m, EFD_SEMAPHORE)) return -1;
17859
16
#endif
17860
16
#endif  /* HAVE_EVENTFD && EFD_CLOEXEC */
17861
17862
#if defined(__APPLE__)
17863
    if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
17864
    if (PyModule_AddIntConstant(m, "_COPYFILE_STAT", COPYFILE_STAT)) return -1;
17865
    if (PyModule_AddIntConstant(m, "_COPYFILE_ACL", COPYFILE_ACL)) return -1;
17866
    if (PyModule_AddIntConstant(m, "_COPYFILE_XATTR", COPYFILE_XATTR)) return -1;
17867
#endif
17868
17869
#ifdef MS_WINDOWS
17870
    if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
17871
    if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
17872
    if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
17873
    if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
17874
    if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
17875
#endif
17876
17877
16
    return 0;
17878
16
}
17879
17880
17881
17882
#define PROBE(name, test) \
17883
   static int name(void)  \
17884
272
   {                      \
17885
272
      if (test) {        \
17886
272
          return 1;       \
17887
272
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
272
   }
posixmodule.c:probe_faccessat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_fchmodat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_fchownat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_fdopendir
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_fstatat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_futimens
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_linkat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_mkdirat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_mkfifoat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_mknodat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_openat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_readlinkat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_renameat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_symlinkat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_unlinkat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_utimensat
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
posixmodule.c:probe_ptsname_r
Line
Count
Source
17884
16
   {                      \
17885
16
      if (test) {        \
17886
16
          return 1;       \
17887
16
      } else {            \
17888
0
          return 0;       \
17889
0
      }                   \
17890
16
   }
17891
17892
#ifdef HAVE_FSTATAT
17893
PROBE(probe_fstatat, HAVE_FSTATAT_RUNTIME)
17894
#endif
17895
17896
#ifdef HAVE_FACCESSAT
17897
PROBE(probe_faccessat, HAVE_FACCESSAT_RUNTIME)
17898
#endif
17899
17900
#ifdef HAVE_FCHMODAT
17901
PROBE(probe_fchmodat, HAVE_FCHMODAT_RUNTIME)
17902
#endif
17903
17904
#ifdef HAVE_FCHOWNAT
17905
PROBE(probe_fchownat, HAVE_FCHOWNAT_RUNTIME)
17906
#endif
17907
17908
#ifdef HAVE_LINKAT
17909
PROBE(probe_linkat, HAVE_LINKAT_RUNTIME)
17910
#endif
17911
17912
#ifdef HAVE_FDOPENDIR
17913
PROBE(probe_fdopendir, HAVE_FDOPENDIR_RUNTIME)
17914
#endif
17915
17916
#ifdef HAVE_MKDIRAT
17917
PROBE(probe_mkdirat, HAVE_MKDIRAT_RUNTIME)
17918
#endif
17919
17920
#ifdef HAVE_MKFIFOAT
17921
PROBE(probe_mkfifoat, HAVE_MKFIFOAT_RUNTIME)
17922
#endif
17923
17924
#ifdef HAVE_MKNODAT
17925
PROBE(probe_mknodat, HAVE_MKNODAT_RUNTIME)
17926
#endif
17927
17928
#ifdef HAVE_RENAMEAT
17929
PROBE(probe_renameat, HAVE_RENAMEAT_RUNTIME)
17930
#endif
17931
17932
#ifdef HAVE_UNLINKAT
17933
PROBE(probe_unlinkat, HAVE_UNLINKAT_RUNTIME)
17934
#endif
17935
17936
#ifdef HAVE_OPENAT
17937
PROBE(probe_openat, HAVE_OPENAT_RUNTIME)
17938
#endif
17939
17940
#ifdef HAVE_READLINKAT
17941
PROBE(probe_readlinkat, HAVE_READLINKAT_RUNTIME)
17942
#endif
17943
17944
#ifdef HAVE_SYMLINKAT
17945
PROBE(probe_symlinkat, HAVE_SYMLINKAT_RUNTIME)
17946
#endif
17947
17948
#ifdef HAVE_FUTIMENS
17949
PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME)
17950
#endif
17951
17952
#ifdef HAVE_UTIMENSAT
17953
PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME)
17954
#endif
17955
17956
#ifdef HAVE_PTSNAME_R
17957
PROBE(probe_ptsname_r, HAVE_PTSNAME_R_RUNTIME)
17958
#endif
17959
17960
17961
17962
static const struct have_function {
17963
    const char * const label;
17964
    int (*probe)(void);
17965
} have_functions[] = {
17966
17967
#ifdef HAVE_EVENTFD
17968
    {"HAVE_EVENTFD", NULL},
17969
#endif
17970
17971
#ifdef HAVE_TIMERFD_CREATE
17972
    {"HAVE_TIMERFD_CREATE", NULL},
17973
#endif
17974
17975
#ifdef HAVE_FACCESSAT
17976
    { "HAVE_FACCESSAT", probe_faccessat },
17977
#endif
17978
17979
#ifdef HAVE_FCHDIR
17980
    { "HAVE_FCHDIR", NULL },
17981
#endif
17982
17983
#ifdef HAVE_FCHMOD
17984
    { "HAVE_FCHMOD", NULL },
17985
#endif
17986
17987
#ifdef HAVE_FCHMODAT
17988
    { "HAVE_FCHMODAT", probe_fchmodat },
17989
#endif
17990
17991
#ifdef HAVE_FCHOWN
17992
    { "HAVE_FCHOWN", NULL },
17993
#endif
17994
17995
#ifdef HAVE_FCHOWNAT
17996
    { "HAVE_FCHOWNAT", probe_fchownat },
17997
#endif
17998
17999
#ifdef HAVE_FEXECVE
18000
    { "HAVE_FEXECVE", NULL },
18001
#endif
18002
18003
#ifdef HAVE_FDOPENDIR
18004
    { "HAVE_FDOPENDIR", probe_fdopendir },
18005
#endif
18006
18007
#ifdef HAVE_FPATHCONF
18008
    { "HAVE_FPATHCONF", NULL },
18009
#endif
18010
18011
#ifdef HAVE_FSTATAT
18012
    { "HAVE_FSTATAT", probe_fstatat },
18013
#endif
18014
18015
#ifdef HAVE_FSTATVFS
18016
    { "HAVE_FSTATVFS", NULL },
18017
#endif
18018
18019
#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
18020
    { "HAVE_FTRUNCATE", NULL },
18021
#endif
18022
18023
#ifdef HAVE_FUTIMENS
18024
    { "HAVE_FUTIMENS", probe_futimens },
18025
#endif
18026
18027
#ifdef HAVE_FUTIMES
18028
    { "HAVE_FUTIMES", NULL },
18029
#endif
18030
18031
#ifdef HAVE_FUTIMESAT
18032
    { "HAVE_FUTIMESAT", NULL },
18033
#endif
18034
18035
#ifdef HAVE_LINKAT
18036
    { "HAVE_LINKAT", probe_linkat },
18037
#endif
18038
18039
#ifdef HAVE_LCHFLAGS
18040
    { "HAVE_LCHFLAGS", NULL },
18041
#endif
18042
18043
#ifdef HAVE_LCHMOD
18044
    { "HAVE_LCHMOD", NULL },
18045
#endif
18046
18047
#ifdef HAVE_LCHOWN
18048
    { "HAVE_LCHOWN", NULL },
18049
#endif
18050
18051
#ifdef HAVE_LSTAT
18052
    { "HAVE_LSTAT", NULL },
18053
#endif
18054
18055
#ifdef HAVE_LUTIMES
18056
    { "HAVE_LUTIMES", NULL },
18057
#endif
18058
18059
#ifdef HAVE_MEMFD_CREATE
18060
    { "HAVE_MEMFD_CREATE", NULL },
18061
#endif
18062
18063
#ifdef HAVE_MKDIRAT
18064
    { "HAVE_MKDIRAT", probe_mkdirat },
18065
#endif
18066
18067
#ifdef HAVE_MKFIFOAT
18068
    { "HAVE_MKFIFOAT", probe_mkfifoat },
18069
#endif
18070
18071
#ifdef HAVE_MKNODAT
18072
    { "HAVE_MKNODAT", probe_mknodat },
18073
#endif
18074
18075
#ifdef HAVE_OPENAT
18076
    { "HAVE_OPENAT", probe_openat },
18077
#endif
18078
18079
#ifdef HAVE_READLINKAT
18080
    { "HAVE_READLINKAT", probe_readlinkat },
18081
#endif
18082
18083
#ifdef HAVE_RENAMEAT
18084
    { "HAVE_RENAMEAT", probe_renameat },
18085
#endif
18086
18087
#ifdef HAVE_SYMLINKAT
18088
    { "HAVE_SYMLINKAT", probe_symlinkat },
18089
#endif
18090
18091
#ifdef HAVE_UNLINKAT
18092
    { "HAVE_UNLINKAT", probe_unlinkat },
18093
#endif
18094
18095
#ifdef HAVE_UTIMENSAT
18096
    { "HAVE_UTIMENSAT", probe_utimensat },
18097
#endif
18098
18099
#ifdef HAVE_PTSNAME_R
18100
    { "HAVE_PTSNAME_R", probe_ptsname_r },
18101
#endif
18102
18103
#ifdef MS_WINDOWS
18104
    { "MS_WINDOWS", NULL },
18105
#endif
18106
18107
    { NULL, NULL }
18108
};
18109
18110
18111
static int
18112
posixmodule_exec(PyObject *m)
18113
16
{
18114
16
    _posixstate *state = get_posix_state(m);
18115
18116
16
#if defined(HAVE_PWRITEV)
18117
16
    if (HAVE_PWRITEV_RUNTIME) {} else {
18118
0
        PyObject* dct = PyModule_GetDict(m);
18119
18120
0
        if (dct == NULL) {
18121
0
            return -1;
18122
0
        }
18123
18124
0
        if (PyDict_PopString(dct, "pwritev", NULL) < 0) {
18125
0
            return -1;
18126
0
        }
18127
0
        if (PyDict_PopString(dct, "preadv", NULL) < 0) {
18128
0
            return -1;
18129
0
        }
18130
0
    }
18131
16
#endif
18132
18133
    /* Initialize environ dictionary */
18134
16
    if (PyModule_Add(m, "environ", convertenviron()) != 0) {
18135
0
        return -1;
18136
0
    }
18137
18138
16
    if (all_ins(m))
18139
0
        return -1;
18140
18141
16
    if (setup_confname_tables(m))
18142
0
        return -1;
18143
18144
16
    if (PyModule_AddObjectRef(m, "error", PyExc_OSError) < 0) {
18145
0
        return -1;
18146
0
    }
18147
18148
16
#if defined(HAVE_WAITID)
18149
16
    waitid_result_desc.name = MODNAME ".waitid_result";
18150
16
    state->WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
18151
16
    if (PyModule_AddObjectRef(m, "waitid_result", state->WaitidResultType) < 0) {
18152
0
        return -1;
18153
0
    }
18154
16
#endif
18155
18156
16
    stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
18157
16
    stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
18158
16
    stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
18159
16
    stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
18160
16
    state->StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
18161
16
    if (PyModule_AddObjectRef(m, "stat_result", state->StatResultType) < 0) {
18162
0
        return -1;
18163
0
    }
18164
16
    state->statresult_new_orig = ((PyTypeObject *)state->StatResultType)->tp_new;
18165
16
    ((PyTypeObject *)state->StatResultType)->tp_new = statresult_new;
18166
18167
16
    statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
18168
16
    state->StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
18169
16
    if (PyModule_AddObjectRef(m, "statvfs_result", state->StatVFSResultType) < 0) {
18170
0
        return -1;
18171
0
    }
18172
18173
16
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
18174
16
    sched_param_desc.name = MODNAME ".sched_param";
18175
16
    state->SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
18176
16
    if (PyModule_AddObjectRef(m, "sched_param", state->SchedParamType) < 0) {
18177
0
        return -1;
18178
0
    }
18179
16
    ((PyTypeObject *)state->SchedParamType)->tp_new = os_sched_param;
18180
16
    if (_PyType_AddMethod((PyTypeObject *)state->SchedParamType,
18181
16
                          &os_sched_param_reduce_method) < 0)
18182
0
    {
18183
0
        return -1;
18184
0
    }
18185
16
    PyType_Modified((PyTypeObject *)state->SchedParamType);
18186
16
#endif
18187
18188
    /* initialize TerminalSize_info */
18189
16
    state->TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
18190
16
    if (PyModule_AddObjectRef(m, "terminal_size", state->TerminalSizeType) < 0) {
18191
0
        return -1;
18192
0
    }
18193
18194
    /* initialize scandir types */
18195
16
    PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL);
18196
16
    if (ScandirIteratorType == NULL) {
18197
0
        return -1;
18198
0
    }
18199
16
    state->ScandirIteratorType = ScandirIteratorType;
18200
18201
16
    state->DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
18202
16
    if (PyModule_AddObjectRef(m, "DirEntry", state->DirEntryType) < 0) {
18203
0
        return -1;
18204
0
    }
18205
18206
16
    times_result_desc.name = MODNAME ".times_result";
18207
16
    state->TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
18208
16
    if (PyModule_AddObjectRef(m, "times_result", state->TimesResultType) < 0) {
18209
0
        return -1;
18210
0
    }
18211
18212
16
    state->UnameResultType = (PyObject *)PyStructSequence_NewType(&uname_result_desc);
18213
16
    if (PyModule_AddObjectRef(m, "uname_result", state->UnameResultType) < 0) {
18214
0
        return -1;
18215
0
    }
18216
18217
16
    if ((state->billion = PyLong_FromLong(1000000000)) == NULL)
18218
0
        return -1;
18219
16
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
18220
16
    state->struct_rusage = PyUnicode_InternFromString("struct_rusage");
18221
16
    if (state->struct_rusage == NULL)
18222
0
        return -1;
18223
16
#endif
18224
16
    state->st_mode = PyUnicode_InternFromString("st_mode");
18225
16
    if (state->st_mode == NULL)
18226
0
        return -1;
18227
18228
    /* suppress "function not used" warnings */
18229
16
    {
18230
16
    int ignored;
18231
16
    fd_specified("", -1);
18232
16
    follow_symlinks_specified("", 1);
18233
16
    dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
18234
16
    dir_fd_converter(Py_None, &ignored);
18235
16
    dir_fd_unavailable(Py_None, &ignored);
18236
16
    }
18237
18238
    /*
18239
     * provide list of locally available functions
18240
     * so os.py can populate support_* lists
18241
     */
18242
16
    PyObject *list = PyList_New(0);
18243
16
    if (!list) {
18244
0
        return -1;
18245
0
    }
18246
528
    for (const struct have_function *trace = have_functions; trace->label; trace++) {
18247
512
        PyObject *unicode;
18248
512
        if (trace->probe && !trace->probe()) continue;
18249
512
        unicode = PyUnicode_DecodeASCII(trace->label, strlen(trace->label), NULL);
18250
512
        if (!unicode)
18251
0
            return -1;
18252
512
        if (PyList_Append(list, unicode))
18253
0
            return -1;
18254
512
        Py_DECREF(unicode);
18255
512
    }
18256
18257
16
#ifndef MS_WINDOWS
18258
16
    if (_Py_GetTicksPerSecond(&state->ticks_per_second) < 0) {
18259
0
        PyErr_SetString(PyExc_RuntimeError,
18260
0
                        "cannot read ticks_per_second");
18261
0
        return -1;
18262
0
    }
18263
16
    assert(state->ticks_per_second >= 1);
18264
16
#endif
18265
18266
16
    return PyModule_Add(m, "_have_functions", list);
18267
16
}
18268
18269
18270
static PyModuleDef_Slot posixmodile_slots[] = {
18271
    {Py_mod_exec, posixmodule_exec},
18272
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
18273
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
18274
    {0, NULL}
18275
};
18276
18277
static struct PyModuleDef posixmodule = {
18278
    PyModuleDef_HEAD_INIT,
18279
    .m_name = MODNAME,
18280
    .m_doc = posix__doc__,
18281
    .m_size = sizeof(_posixstate),
18282
    .m_methods = posix_methods,
18283
    .m_slots = posixmodile_slots,
18284
    .m_traverse = _posix_traverse,
18285
    .m_clear = _posix_clear,
18286
    .m_free = _posix_free,
18287
};
18288
18289
PyMODINIT_FUNC
18290
INITFUNC(void)
18291
16
{
18292
16
    return PyModuleDef_Init(&posixmodule);
18293
16
}