Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/gen/initgroups.c
| Show All 25 Lines | |||||
| * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||
| * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||
| * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
| * SUCH DAMAGE. | * SUCH DAMAGE. | ||||
| */ | */ | ||||
| #include <sys/param.h> | #include <sys/param.h> | ||||
| #include "namespace.h" | |||||
| #include <err.h> | |||||
| #include "un-namespace.h" | |||||
| #include <errno.h> | #include <errno.h> | ||||
| #include <stdio.h> | |||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <unistd.h> | #include <unistd.h> | ||||
| int | int | ||||
| initgroups(const char *uname, gid_t agroup) | initgroups(const char *uname, gid_t agroup) | ||||
| { | { | ||||
| int ngroups, ret; | int ngroups, ret; | ||||
| long ngroups_max; | long ngroups_max; | ||||
| gid_t *groups; | gid_t *groups; | ||||
| /* | /* | ||||
| * Provide space for one group more than possible to allow | * Provide space for one group more than possible to allow | ||||
| * setgroups to fail and set errno. | * setgroups to fail and set errno. | ||||
| */ | */ | ||||
| ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2; | ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2; | ||||
| if ((groups = malloc(sizeof(*groups) * ngroups_max)) == NULL) | groups = malloc(sizeof(*groups) * ngroups_max); | ||||
| return (ENOMEM); | if (groups == NULL) | ||||
| return (-1); /* malloc() set 'errno'. */ | |||||
| ngroups = (int)ngroups_max; | ngroups = (int)ngroups_max; | ||||
| getgrouplist(uname, agroup, groups, &ngroups); | getgrouplist(uname, agroup, groups, &ngroups); | ||||
| ret = setgroups(ngroups, groups); | ret = setgroups(ngroups, groups); | ||||
| free(groups); | free(groups); | ||||
| return (ret); | return (ret); /* setgroups() set 'errno'. */ | ||||
| } | } | ||||