Repository: mesonbuild/meson

Issue:
'getpwuid(): uid not found: 1001'
**Describe the bug**
I am install dbus-python and this package require the meson. the install will raise an exception with 'getpwuid(): uid not found: 1001'

**To Reproduce**
Use an docker to install the dbus-python with the command.
sudo pip3 install dbus-python==1.3.2
```
INFO: autodetecting backend as ninja
      INFO: calculating backend command to run: /tmp/pip-build-env-v1swhhzx/overlay/bin/ninja
      Traceback (most recent call last):
        File "/tmp/pip-build-env-v1swhhzx/overlay/lib/python3.9/site-packages/mesonbuild/mesonmain.py", line 194, in run
          return options.run_func(options)
        File "/tmp/pip-build-env-v1swhhzx/overlay/lib/python3.9/site-packages/mesonbuild/minstall.py", line 823, in run
          if not rebuild_all(opts.wd, backend):
        File "/tmp/pip-build-env-v1swhhzx/overlay/lib/python3.9/site-packages/mesonbuild/minstall.py", line 803, in rebuild_all
          env, preexec_fn = drop_privileges()
        File "/tmp/pip-build-env-v1swhhzx/overlay/lib/python3.9/site-packages/mesonbuild/minstall.py", line 779, in drop_privileges
          homedir = pwd.getpwuid(int(orig_uid)).pw_dir
      KeyError: 'getpwuid(): uid not found: 1001'
```


**Expected behavior**
install successful

**system parameters**
python 3.9.9 with meson 1.1.0

I check it successful yesterday. I guess it should be the new issue in 1.1.0 since it is a new release version.
I found it with https://github.com/mesonbuild/meson/commit/ecb32bf457ed27b75c9b4386ca1acfbd4b63869e



Discussion:
This is a... fascinating... error. Why does docker think your current (pre-sudo) user is user number 1001 but that user doesn't exist?
I cannot actually replicate this. Becoming root, then using setgid/setuid by hand to a nonexistent uid, "works", as in I can execute a shell and my current uid is a nonexistent user.

Upon doing so, sudo refuses to work, and it refuses in an incredibly dramatic manner:
```
$ sudo --help
sudo: you do not exist in the passwd database
$ sudo --version
sudo: you do not exist in the passwd database
```

It should not be possible to end up in this situation where your uid is not found in the password database.

Resolving patch:
diff --git a/mesonbuild/minstall.py b/mesonbuild/minstall.py
--- a/mesonbuild/minstall.py
+++ b/mesonbuild/minstall.py
@@ -778,16 +778,28 @@ def drop_privileges() -> T.Tuple[T.Optional[EnvironOrDict], T.Optional[T.Callabl
                 orig_user = env.pop('SUDO_USER')
                 orig_uid = env.pop('SUDO_UID', 0)
                 orig_gid = env.pop('SUDO_GID', 0)
-                homedir = pwd.getpwuid(int(orig_uid)).pw_dir
+                try:
+                    homedir = pwd.getpwuid(int(orig_uid)).pw_dir
+                except KeyError:
+                    # `sudo chroot` leaves behind stale variable and builds as root without a user
+                    return None, None
             elif os.environ.get('DOAS_USER') is not None:
                 orig_user = env.pop('DOAS_USER')
-                pwdata = pwd.getpwnam(orig_user)
+                try:
+                    pwdata = pwd.getpwnam(orig_user)
+                except KeyError:
+                    # `doas chroot` leaves behind stale variable and builds as root without a user
+                    return None, None
                 orig_uid = pwdata.pw_uid
                 orig_gid = pwdata.pw_gid
                 homedir = pwdata.pw_dir
             else:
                 return None, None
 
+            if os.stat(os.path.join(wd, 'build.ninja')).st_uid != int(orig_uid):
+                # the entire build process is running with sudo, we can't drop privileges
+                return None, None
+
             env['USER'] = orig_user
             env['HOME'] = homedir
 


Test patch: