Compare commits

...

4 Commits

Author SHA1 Message Date
Mike Yuan dc9c833ad9
Merge da80af70d2 into 0e44a351ea 2024-11-25 00:13:33 +00:00
Daan De Meyer 0e44a351ea mkosi: Make sure mkosi.clangd always runs on the host
If the editor that invokes mkosi.clangd is a flatpak, let's make sure
that mkosi is run on the host and not in the flatpak sandbox since it
won't be installed there.
2024-11-25 00:21:10 +01:00
Mike Yuan da80af70d2
basic/user-util: modernize getgroups_alloc() a bit
- Make sure ret is initialized if we return >= 0
- Reduce variable scope
2024-11-21 17:02:15 +01:00
Mike Yuan 62d6972874
basic/user-util: use FOREACH_ARRAY at one more place 2024-11-21 15:23:26 +01:00
4 changed files with 33 additions and 27 deletions

View File

@ -1,12 +1,18 @@
#!/bin/bash #!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1-or-later
MKOSI_CONFIG="$(mkosi --json summary | jq -r .Images[-1])" if command -v flatpak-spawn >/dev/null; then
SPAWN=(flatpak-spawn --host)
else
SPAWN=()
fi
MKOSI_CONFIG="$("${SPAWN[@]}" --host mkosi --json summary | jq -r .Images[-1])"
DISTRIBUTION="$(jq -r .Distribution <<< "$MKOSI_CONFIG")" DISTRIBUTION="$(jq -r .Distribution <<< "$MKOSI_CONFIG")"
RELEASE="$(jq -r .Release <<< "$MKOSI_CONFIG")" RELEASE="$(jq -r .Release <<< "$MKOSI_CONFIG")"
ARCH="$(jq -r .Architecture <<< "$MKOSI_CONFIG")" ARCH="$(jq -r .Architecture <<< "$MKOSI_CONFIG")"
exec mkosi \ exec "${SPAWN[@]}" mkosi \
--incremental=strict \ --incremental=strict \
--build-sources-ephemeral=no \ --build-sources-ephemeral=no \
--format=none \ --format=none \

View File

@ -467,9 +467,12 @@ char* gid_to_name(gid_t gid) {
} }
static bool gid_list_has(const gid_t *list, size_t size, gid_t val) { static bool gid_list_has(const gid_t *list, size_t size, gid_t val) {
for (size_t i = 0; i < size; i++) assert(list || size == 0);
if (list[i] == val)
FOREACH_ARRAY(i, list, size)
if (*i == val)
return true; return true;
return false; return false;
} }
@ -526,20 +529,24 @@ int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t
return (int)nresult; return (int)nresult;
} }
int getgroups_alloc(gid_t** gids) { int getgroups_alloc(gid_t **ret) {
gid_t *allocated;
_cleanup_free_ gid_t *p = NULL;
int ngroups = 8; int ngroups = 8;
unsigned attempt = 0;
allocated = new(gid_t, ngroups); assert(ret);
if (!allocated)
for (unsigned attempt = 0;;) {
_cleanup_free_ gid_t *p = NULL;
p = new(gid_t, ngroups);
if (!p)
return -ENOMEM; return -ENOMEM;
p = allocated;
for (;;) {
ngroups = getgroups(ngroups, p); ngroups = getgroups(ngroups, p);
if (ngroups >= 0) if (ngroups > 0) {
*ret = TAKE_PTR(p);
return ngroups;
}
if (ngroups == 0)
break; break;
if (errno != EINVAL) if (errno != EINVAL)
return -errno; return -errno;
@ -554,17 +561,11 @@ int getgroups_alloc(gid_t** gids) {
if (ngroups < 0) if (ngroups < 0)
return -errno; return -errno;
if (ngroups == 0) if (ngroups == 0)
return false; break;
free(allocated);
p = allocated = new(gid_t, ngroups);
if (!allocated)
return -ENOMEM;
} }
*gids = TAKE_PTR(p); *ret = NULL;
return ngroups; return 0;
} }
int get_home_dir(char **ret) { int get_home_dir(char **ret) {

View File

@ -64,7 +64,7 @@ int in_gid(gid_t gid);
int in_group(const char *name); int in_group(const char *name);
int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **result); int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **result);
int getgroups_alloc(gid_t** gids); int getgroups_alloc(gid_t **ret);
int get_home_dir(char **ret); int get_home_dir(char **ret);
int get_shell(char **ret); int get_shell(char **ret);

View File

@ -444,9 +444,8 @@ TEST(gid_lists_ops) {
assert_se(nresult >= 0); assert_se(nresult >= 0);
assert_se(memcmp_nn(result2, ELEMENTSOF(result2), res4, nresult) == 0); assert_se(memcmp_nn(result2, ELEMENTSOF(result2), res4, nresult) == 0);
nresult = getgroups_alloc(&gids); ASSERT_OK(nresult = getgroups_alloc(&gids));
assert_se(nresult >= 0 || nresult == -EINVAL || nresult == -ENOMEM); assert_se(gids || nresult == 0);
assert_se(gids);
} }
TEST(parse_uid_range) { TEST(parse_uid_range) {