initgroups function

void initgroups(
  1. String user, {
  2. int? group,
})

Initialize the group set for the current user by reading the group database and using all groups of which USER is a member. Also adds group to the list of groups if passed.

This function is not part of POSIX and therefore no official cancellation point. But due to similarity with an POSIX interface or due to the implementation it is a cancellation point and therefore not marked with __THROW.

The initgroups() function returns 0 on success. On error, -1 is returned, and errno is set appropriately.

Implementation

void initgroups(String user, {int? group}) {
  // If the user doesn't pass a group we use getegid as the list of
  // groups should already have the users effective group.
  // We do this as we can't find any doco on what group should
  // be set to if you don't wont' to add a group (-1 doesn't work).
  group ??= getegid();
  final cUser = user.toNativeUtf8();

  _initgroups ??= Libc().dylib.lookupFunction<
      ffi.Int32 Function(ffi.Pointer<Utf8>, ffi.Uint32),
      _dart_initgroups>('initgroups');

  final err = _initgroups!(
    cUser,
    group,
  );
  malloc.free(cUser);

  if (err != 0) {
    throw PosixException('initgroups failed for $user', errno());
  }
}