Fedora Linux Support Community & Resources Center
  #1  
Old 21st July 2010, 02:54 PM
merkava Offline
Registered User
 
Join Date: Jul 2010
Location: Tabriz, Iran
Age: 24
Posts: 75
linuxfedorafirefox
getgroups function

Hi guys.

I have wrote a simple program to test getgroups functionality:
Here is my group configuration:

Code:
group1:501:test_user
group2:502:test_user
group3:503:test_user
group4:504:test_user
group5:505:test_user
test_user is member of all groups. and here is the program:

int main(void)
{
//I get number of supplementary groups
int number_of_groups = getgroups(0, NULL);
printf("%d\n", number_of_groups);
return EXIT_SUCCESS;
}

This program is running with user id of test_user. But the problem is this:

'number_of_groups' is always '1'
__________________
Best Regards
MERKAVA
Reply With Quote
  #2  
Old 22nd July 2010, 10:24 AM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: getgroups function

If you access the info documentation from the command line for getgroups, you'll get a usage example, from which the following
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct s S_STR;
#define S_STR struct s
struct s {
    int counter;
    gid_t *g;
};

S_STR *read_all_groups();

int main(int argc, char *argv[]) {

    int i = 0;
    S_STR *p = NULL;

    if((p = (S_STR *)read_all_groups()) == NULL) {
        perror("Could not determine process group(s)");
        exit(1);
    }

    for(i = 0; i < p->counter; p->g++, i++)
        printf("Group: %d\n", *p->g);

    return 0;
}

S_STR *read_all_groups(void) {

    S_STR *p = NULL;
    int ngroups = getgroups(0, NULL);

    if((p = (S_STR *)malloc(sizeof(S_STR))) == NULL) {
        perror("Error allocating memory for S_STR struct");
        exit(1);
    }
    else
        p->g = NULL;

    if((p->g = (gid_t *)malloc(ngroups * sizeof(gid_t))) == NULL) {
        perror("Error allocating memory for groups array");
        exit(1);
    }
    p->counter = getgroups(ngroups, p->g);
    if(p->counter < 0) {
        if(p->g != NULL) {
            free(p->g);
            p->g = NULL;
        }
        return NULL;
    }

    return p;
}

Last edited by giulix; 22nd July 2010 at 06:13 PM.
Reply With Quote
Reply

Tags
function, getgroups

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fc8 Function keys Fittersman Using Fedora 1 23rd November 2007 08:54 AM
keyboard not function irbadguyzs Using Fedora 3 6th November 2007 01:19 AM
MP3 and DVD does not function Rezwan Mahbub Using Fedora 0 7th March 2007 06:51 PM
some function the curse Programming & Packaging 2 26th November 2006 08:01 AM


Current GMT-time: 09:41 (Sunday, 23-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Yangliuqing Photos on Instagram - Meihekou - Gennevilliers Photos on Instagram