Re: [PATCH] fix util_lookup_group to handle large groups

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Some more issues:
1) multi-threading
Assuming util_lookup_group() has to be thread save
my solution wont work reliable, found 
> The getgrnam() function need not be thread-safe.
so we have to the reentrant version.

2) errno
Lennart Poettering <lennart@xxxxxxxxxxxxxx> wrote:
> One little comment here: on POSIX getrnam_r() doesn't touch
> errno. Instead it returns the error value as return value.

3) sysconf(_SC_GETGR_R_SIZE_MAX)
obviously wrong documented, its return value can only be used as hint.
IMHO its worth to initiate with some usefull defaults, because
repeatedly calling getgrnam_r does'nt come for free.

4) realloc
if we additionally want to catch realloc failure
this one should do:

gid_t util_lookup_group(struct udev *udev, const char *group)
{
	char *endptr;
	int buflen;
	struct group grbuf;
	struct group *gr;
	char *mem;
	char *buf = NULL;
	gid_t gid = 0;

	if (strcmp(group, "root") == 0)
		return 0;
	gid = strtoul(group, &endptr, 10);
	if (endptr[0] == '\0')
		return gid;

	buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
	if (buflen < 32)
		buflen = 1024;
	for (;mem = realloc(buf, buflen); buflen *=2 )
		{
		buf=mem;
		errno=getgrnam_r(group, &grbuf, buf, buflen, &gr);
		if (gr != NULL)
			gid = gr->gr_gid;
		else if (errno == ERANGE) 
			continue;
		else if (errno == 0 || errno == ENOENT || errno == ESRCH)
			err(udev, "specified group '%s' unknown\n", group);
		else
			err(udev, "error resolving group '%s': %m\n", group);
		break;
	}
	if (!mem)
		err(udev, "error resolving group '%s': allocating %d bytes failed\n", group,buflen);
	if (buf)
		free(buf);
	return gid;
}

sorry, no patch yet.
currently using numeric gids in udev rules to avoid ERANGE error.
BTW util_lookup_user needs to be checked too.
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel]     [Linux DVB]     [Asterisk Internet PBX]     [DCCP]     [Netdev]     [X.org]     [Util Linux NG]     [Fedora Women]     [ALSA Devel]     [Linux USB]

  Powered by Linux