예제 #1
0
파일: userns03.c 프로젝트: xingdl2007/ltp
int main(int argc, char *argv[])
{
	pid_t cpid2;
	char path[BUFSIZ];
	int lc;
	int fd;

	tst_parse_opts(argc, argv, NULL, NULL);
	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		tst_count = 0;

		parentuid = geteuid();
		parentgid = getegid();

		cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
			(void *)child_fn1, NULL);
		if (cpid1 < 0)
			tst_brkm(TBROK | TERRNO, cleanup,
				"cpid1 clone failed");

		cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
			(void *)child_fn2, NULL);
		if (cpid2 < 0)
			tst_brkm(TBROK | TERRNO, cleanup,
				"cpid2 clone failed");

		if (setgroupstag == false) {
			sprintf(path, "/proc/%d/setgroups", cpid1);
			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
			SAFE_CLOSE(cleanup, fd);

			sprintf(path, "/proc/%d/setgroups", cpid2);
			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
			SAFE_CLOSE(cleanup, fd);
		}

		updatemap(cpid1, UID_MAP, CHILD1UID, parentuid, cleanup);
		updatemap(cpid2, UID_MAP, CHILD2UID, parentuid, cleanup);

		updatemap(cpid1, GID_MAP, CHILD1GID, parentuid, cleanup);
		updatemap(cpid2, GID_MAP, CHILD2GID, parentuid, cleanup);

		TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(cleanup, 1);

		tst_record_childstatus(cleanup, cpid1);
		tst_record_childstatus(cleanup, cpid2);
	}
	cleanup();
	tst_exit();
}
예제 #2
0
파일: fire.c 프로젝트: ckkashyap/pedigree
void loadcolors(unsigned int which)
{
int i,j;
int r,g,b;

	which%=11;
	for(i=0;i<256;i++)
	{
		switch(which)
		{
		case 0:
			if(i<64) loadcolor(i,0,0,0);
			else if(i<128)	loadcolor(i,i-64,0,0);
			else if(i<192) loadcolor(i,63,i-128,0);
			else loadcolor(i,63,63,i-192);
			break;
		case 1:
			if(i<64) loadcolor(i,0,0,0);
			else if(i<128)	loadcolor(i,0,0,i-64);
			else loadcolor(i,(i-128)>>1,(i-128)>>1,63);
			break;
		case 2:
			loadcolor(i,i>>2,i>>2,i>>2);
			break;
		case 3:
			r=rand()&0x3f;
			g=rand()&0x3f;
			b=rand()&0x3f;
			loadcolor(i,r*i>>8,g*i>>8,b*i>>8);
			break;
		case 4:
			loadcolor(i,i>>2,0,0);
			break;
		case 5:
			loadcolor(i,0,i>>2,0);
			break;
		case 6:
			loadcolor(i,0,0,i>>2);
			break;
		case 7:
			j=i&15;
			if(i&16) j=15-j;
			j=(i>>2)*j/16;
			loadcolor(i,j,j,j);
			break;
		case 8:
			j=0;
			if(i>8 && i<128) j=63;
			loadcolor(i,j,j,j);
			break;
		case 9:
			j=31-(i&31)<<1;
			r=i&32 ? j : 0;
			g=i&64 ? j : 0;
			b=i&128 ? j : 0;
			loadcolor(i,r,g,b);
			break;
		case 10:
			j=(i&15)<<2;
			if(i&16) j=63-j;
			r=i&32 ? j : 0;
			g=i&64 ? j : 0;
			b=i&128 ? j : 0;
			loadcolor(i,r,g,b);
			break;
		}
	}
	updatemap();
}
예제 #3
0
int main(int argc, char **argv) {
    int i,j,maxn,maxm;
    const char str[]="../test/2/input20.txt";//14 15
    char bool_show=1;
    freopen(str,"rt",stdin);
    scanf("%d%d\n",&maxn,&maxm);
    char **map;
    map=(char **)malloc(maxn*sizeof(char *));
    for (i=0;i<maxn;i++) map[i]=(char *)malloc(maxm*sizeof(char));
    ELEM_POINT *Gold, *Monster, *Player;
    int cgolds=0,cmonsters=0,cplayers=0;
    for (i=0;i<maxn;i++) {
        char *string=(char *)malloc((maxm+2)*sizeof(char));
        gets(string);
        for (j=0;j<maxm;j++) {
            map[i][j]=string[j];
            if (map[i][j]>='0' && map[i][j]<='9') cplayers++;
            if (map[i][j]=='*') cgolds++;
            if (map[i][j]=='@') cmonsters++;
        }
    }
    fclose(stdin);
    Player=(ELEM_POINT *)malloc(cplayers*sizeof(ELEM_POINT));
    Gold=(ELEM_POINT *)malloc(cgolds*sizeof(ELEM_POINT));
    Monster=(ELEM_POINT *)malloc(cmonsters*sizeof(ELEM_POINT));
    int *score=(int *)malloc(cplayers*sizeof(int));
    for (i=0;i<cplayers;i++) score[i]=0;
    int numbergame;
    for (numbergame=0;numbergame<factorial(cplayers);numbergame++) {
        printf("Number of game = %d\n",numbergame);
        //usleep(3*1000*1000);

        int move=0,indexgold,indexmonster,indexplayer;

        //set position
        int iteration=-1;
        int number_of_iteration=0;
        int *massive_iteration=(int *)malloc(cplayers*sizeof(int)) ;
        while (iteration!=numbergame) {
            iteration++;
            while (1) {
                int index_of_iteration, tmp_number_of_iteration=number_of_iteration;
                for (index_of_iteration=cplayers-1;index_of_iteration>=0;index_of_iteration--) {
                    massive_iteration[index_of_iteration]=tmp_number_of_iteration%cplayers;
                    tmp_number_of_iteration/=cplayers;
                }
                int flagin=1;
                for (i=0;i<cplayers-1;i++) {
                    for (j=i+1;j<cplayers;j++) {
                        if (massive_iteration[i]==massive_iteration[j]) {
                            flagin=0;
                            break;
                        }
                    }
                    if (flagin==0) break;
                }
                number_of_iteration++;
                if (flagin==1) break;
            }
        }

        freopen(str,"rt",stdin);
        scanf("%d%d\n",&maxn,&maxm);
        int cgolds=0,cmonsters=0,cplayers=0;
        for (i=0;i<maxn;i++) {
            for (j=0;j<maxm;j++) {
                map[i][j]='.';
            }
        }

        for (i=0;i<maxn;i++) {
            char *string=(char *)malloc((maxm+2)*sizeof(char));
            gets(string);
            for (j=0;j<maxm;j++) {
                map[i][j]=string[j];
                if (map[i][j]>='0' && map[i][j]<='9') {
                    cplayers++;
                    Player[ map[i][j]-'0' ].y=i;
                    Player[ map[i][j]-'0' ].x=j;
                    Player[ map[i][j]-'0' ].alive=map[i][j];
                }
                if (map[i][j]=='*') cgolds++;
                if (map[i][j]=='@') cmonsters++;
            }
        }
        fclose(stdin);

        for (i=0;i<cplayers;i++) {
            map[Player[i].y][Player[i].x]=Player[massive_iteration[i]].alive;
        }
        free(massive_iteration);

        while (1) {
            cgolds=0;
            cmonsters=0;
            indexgold=0;
            indexmonster=0;
            indexplayer=0;
            system("clear");
            for (i=0;i<maxn;i++) {
                for (j=0;j<maxm;j++) {

                    if (bool_show) {


                        if (map[i][j]>='0' && map[i][j]<='9') printf("\033[33m%c",map[i][j]);
                        if (map[i][j]=='#') printf("\033[35m%c",map[i][j]);
                        if (map[i][j]=='*') printf("\033[31m%c",map[i][j]);
                        if (map[i][j]=='@') printf("\033[36m%c",map[i][j]);
                        if (map[i][j]=='.') printf("\033[37m%c",map[i][j]);//37
                        printf("\033[37m");

                        //printf("%c",map[i][j]);
                    }
                    if (map[i][j]>='0' && map[i][j]<='9') {
                        Player[ map[i][j]-'0' ].y=i;
                        Player[ map[i][j]-'0' ].x=j;
                        Player[ map[i][j]-'0' ].alive=map[i][j];
                    }
                    if (map[i][j]=='*') {
                        Gold[indexgold].y=i;
                        Gold[indexgold].x=j;
                        Gold[indexgold].alive=1;
                        indexgold++;
                    }
                    if (map[i][j]=='@') {
                        Monster[indexmonster].y=i;
                        Monster[indexmonster].x=j;
                        Monster[indexmonster].alive=1;
                        indexmonster++;
                    }
                }
                if (bool_show) printf("\n");
            }
            //if (move==0) usleep(1000*1000);
            cgolds=indexgold;
            cmonsters=indexmonster;
            if (move>=10000 || cgolds==0) break;

            int command;
            clock_t time;

            printf("Players think ");
            //0
            if (cplayers>-1) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=0;
                    time=clock();
                    get_player1(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            //1
            if (cplayers>0) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=1;
                    time=clock();
                    get_player2(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            /*
            //2
            if (cplayers>0) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=2;
                    time=clock();
                    get_player3(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            //3
            if (cplayers>1) {
                int isGold=0;
                for (i=0;i<maxn;i++) {
                    for (j=0;j<maxm;j++) {
                        if (map[i][j]=='*') {
                            isGold=1;
                            break;
                        }
                    }
                    if (isGold) break;
                }
                if (isGold) {
                    indexplayer=3;
                    time=clock();
                    get_player4(map,maxn,maxm,Player[indexplayer].y,Player[indexplayer].x,&command);
                    time = clock()-time;
                    printf("%3.0f ms ", 1000*(double)time/CLOCKS_PER_SEC);
                    if (1000*(double)time/CLOCKS_PER_SEC<1000) {
                        switch (command) {
                            case 0: break;
                            case 1: if (Player[indexplayer].x>0 && map[Player[indexplayer].y][Player[indexplayer].x-1]!='#') Player[indexplayer].x--; break;
                            case 2: if (Player[indexplayer].y>0 && map[Player[indexplayer].y-1][Player[indexplayer].x]!='#') Player[indexplayer].y--; break;
                            case 3: if (Player[indexplayer].x<maxm-1 && map[Player[indexplayer].y][Player[indexplayer].x+1]!='#') Player[indexplayer].x++; break;
                            case 4: if (Player[indexplayer].y<maxn-1 && map[Player[indexplayer].y+1][Player[indexplayer].x]!='#') Player[indexplayer].y++; break;
                            default : break;
                        }
                    }
                    updatemap(map,maxn,maxm,Monster,cmonsters,Gold,cgolds,Player,cplayers,indexplayer,score);
                }
            }
            */

            printf("\nScores : ");
            //results
            for (indexplayer=0;indexplayer<cplayers;indexplayer++) {
                printf("%d | ",score[indexplayer]);
            }

            //monsters
            for (indexmonster=0;indexmonster<cmonsters;indexmonster++) {
                command = get_monster(map,maxn,maxm,Monster[indexmonster].y,Monster[indexmonster].x);
                switch (command) {
                    case 0: break;
                    case 1: Monster[indexmonster].x--; break;
                    case 2: Monster[indexmonster].y--; break;
                    case 3: Monster[indexmonster].x++; break;
                    case 4: Monster[indexmonster].y++; break;
                    default : break;
                }
                updatemap2(map,maxn,maxm,Monster,cmonsters,Gold, cgolds, Player, cplayers, score);
            }
            printf("move = %d\n",move);
            usleep(timesleep*1000);
            move++;
        }
        for (indexplayer=0;indexplayer<cplayers;indexplayer++) {
            printf("Player %d : scores %d\n",indexplayer,score[indexplayer]);
        }
        printf("move = %d\n",move);
    }
    free(Player);
    free(Gold);
    free(Monster);
    for (i=0;i<maxn;i++) free(map[i]);
    free(map);


    //RATING ELO
    typedef struct profile {
        char name[100];
        int rating;
    } profile;
    profile data[10], *P=(profile *)malloc(cplayers*sizeof(profile));
    int cdata=0;
    //current players
    freopen("../test/cur_game.txt","rt",stdin);
    for (i=0;i<cplayers;i++) {
        scanf("%s\n",P[i].name);
    }
    fclose(stdin);
    freopen("../test/rating.txt","rt",stdin);
    int *ind=(int *)malloc(cplayers*sizeof(int)); //indexs of players in data
    while (1) {
        char st[100];
        scanf("%s\n",st);
        st[(int)strlen(st)]='\0';
        if (!strcmp(st,"0")) break;
        int rate;
        scanf("%d\n",&rate);
        for (i=0;i<(int)strlen(st)+1;i++) data[cdata].name[i]=st[i];
        data[cdata].rating=rate;
        for (i=0;i<cplayers;i++) {
            if (!strcmp(data[cdata].name,P[i].name)) {
                ind[i]=cdata;
            }
        }
        cdata++;
    }
    fclose(stdin);
    printf("BEFORE : \n");
    double *addition=(double *)malloc(cplayers*sizeof(double));
    for (i=0;i<cplayers;i++) {
        addition[i]=0;
    }
    for (i=0;i<cplayers-1;i++) {
        for (j=i+1;j<cplayers;j++) {
            double x= (score[i]>score[j]) ? 1.0 : (score[i]==score[j]) ? 0.5 : 0.0;
            double E1=1.0/(1.0+pow(10.0,(double)(data[ind[j]].rating-data[ind[i]].rating)/400.0)),
                    E2=1.0/(1.0+pow(10.0,(double)(data[ind[i]].rating-data[ind[j]].rating)/400.0));
            addition[i]+=x-E1;
            addition[j]+=1.0-x-E2;
        }
    }

    for (i=0;i<cplayers;i++) {
        data[ind[i]].rating+=(int)(30.0*addition[i]);
    }
    free(P);
    free(addition);


    //HISTORY OF GAMES
    char history[1000][100];
    int indexhistory=0,chistory=0;
    freopen("../test/result_games.txt","rt",stdin);
    while (1) {
        gets(history[indexhistory]);
        if (!strcmp(history[indexhistory],"\0")) break;
        indexhistory++;
    }
    chistory=indexhistory;
    fclose(stdin);
    freopen("../test/result_games.txt","wt",stdout);
    for (indexhistory=0;indexhistory<chistory;indexhistory++) {
        printf("%s\n",history[indexhistory]);
    }
    int indexplayer;
    printf("%s\n",str);
    for (indexplayer=0;indexplayer<cplayers;indexplayer++) {
        printf("%d player = %d\n",indexplayer,score[indexplayer]);
    }
    fclose(stdout);
    free(score);

    printf("AFTER : \n");
    for (i=0;i<cdata;i++) {
        printf("%10s %4d\n",data[i].name,data[i].rating);
    }

    freopen("../test/rating.txt","wt",stdout);
    for (i=0;i<cdata;i++) {
        printf("%s\n%d\n",data[i].name,data[i].rating);
    }
    printf("0");
    fclose(stdout);

    return 0;
}
예제 #4
0
파일: fire.c 프로젝트: klange/firesdl
main(int argc, char *argv[])
{
int i,k;
char *remap,*remap2;
unsigned char *p1, *p2;
long frames;
int flash;
int whichmap;
int key;
int ispaused;
unsigned long videoflags;
int done;
int now;
SDL_Event event;
long starttime;
int buttonstate;

	srand(time(NULL));
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
	{
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		exit(1);
	}
	videoflags = SDL_SWSURFACE|SDL_HWPALETTE|SDL_RESIZABLE;

	thescreen = SDL_SetVideoMode(XSIZE, YSIZE, 8, videoflags);
	if ( thescreen == NULL )
	{
		fprintf(stderr, "Couldn't set display mode: %s\n",
							SDL_GetError());
		SDL_Quit();
		exit(5);
	}

	vmem1=NULL;
	vmem2=malloc(XSIZE*YSIZE);
	if(!vmem2) nomem();
	mul640=malloc(YSIZE*sizeof(char *));
	if(!mul640) nomem();
	remap=malloc(16384);
	if(!remap) nomem();
	remap2=malloc(16384);
	if(!remap2) nomem();
	blobs=malloc(MAXBLOBS*sizeof(struct blob));
	if(!blobs) nomem();

	puts("Fire demo by David Ashley ([email protected])");
	puts("1 = Change color map");
	puts("2 = Randomly change color map");
	puts("p = Pause");
	puts("spc = Fire");
	puts("esc = Exit");
	puts("Left mouse button = paint");
	puts("Right mouse button, CR = ignite atmosphere");

	freeblobs=activeblobs=0;
	for(i=0;i<MAXBLOBS;i++)
	{
		blobs[i].blobnext=freeblobs;
		freeblobs=blobs+i;
	}

	normal(remap);
	bright(remap2);


	flash=0;
	whichmap=0;
	loadcolors(whichmap);
	frames=0;
	ispaused=0;
	addblob();
	done = 0;
	now=0;
	starttime=SDL_GetTicks();
	buttonstate=0;
	mousex=mousey=0;

	while(!done)
	{
ohno:
		if ( scrlock() < 0 ) continue;
		frames++;
		if ( vmem1 != (unsigned char *)thescreen->pixels )
		{
			p1=vmem1=thescreen->pixels;
			for (i=0;i<YSIZE;i++)
			{
				mul640[i]=i*thescreen->pitch+vmem1;
				memset(p1,0,XSIZE);
				p1+=thescreen->pitch;
			}
		}
		if(!ispaused)
		{
			now++;
			if(!flash)
			{
				if(explodenum>96 && explodenum<160 && !(rand()&511) || (buttonstate&8))
					flash=60;
			} else --flash;
			explodenum=(now>>4)+1;if(explodenum==320) now=0;
			if(explodenum>256) explodenum=256;
			if(!(rand()&31))
				addblob();
			moveblobs();
			putblobs();
			if(buttonstate&2) trydisk();
			p1=vmem1;
			p2=vmem2;
			k=thescreen->pitch;
			for(i=0;i<YSIZE;i++)
			{
				memcpy(p2,p1,XSIZE);
				p2+=XSIZE;
				p1+=k;
			}
			fire(vmem2,vmem1,k,flash ? remap2 :remap);
		}
		scrunlock();

		while(SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_VIDEORESIZE:
				XSIZE = event.resize.w;
				YSIZE = event.resize.h;
				vmem1=NULL;
				vmem2=malloc(XSIZE*YSIZE);
				mul640=malloc(YSIZE*sizeof(char *));
				thescreen = SDL_SetVideoMode(XSIZE, YSIZE, 8, videoflags);
				updatemap();
				goto ohno;
				break;

			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				if ( event.button.state == SDL_PRESSED )
					buttonstate|=1<<event.button.button;
				else
					buttonstate&=~(1<<event.button.button);
				mousex=event.button.x;
				mousey=event.button.y;
				if(!ispaused && buttonstate&2) trydisk();
				break;
			case SDL_MOUSEMOTION:
				mousex=event.motion.x;
				mousey=event.motion.y;
				if(!ispaused && buttonstate&2) trydisk();
				break;
			case SDL_KEYDOWN:
				key=event.key.keysym.sym;
				if(key==SDLK_RETURN) {flash=60;break;}
				if(key==SDLK_1 || key==SDLK_2)
				{
					if(key==SDLK_1)
						++whichmap;
					else
						whichmap=rand();
					loadcolors(whichmap);
					break;
				}
				if(key==SDLK_ESCAPE) {done=1;break;}
				if(key==SDLK_SPACE && !ispaused) {addblob();break;}
				if(key==SDLK_p) {ispaused=!ispaused;break;}
				break;
			case SDL_QUIT:
				done = 1;
				break;
			default:
				break;
			}
		}
	}
예제 #5
0
파일: userns03.c 프로젝트: AiprNick/ltp
int main(int argc, char *argv[])
{
	pid_t cpid2;
	char path[BUFSIZ];
	int lc;
	int fd;
	int ret;

	tst_parse_opts(argc, argv, NULL, NULL);
	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		tst_count = 0;

		parentuid = geteuid();
		parentgid = getegid();

		cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
			(void *)child_fn1, NULL);
		if (cpid1 < 0)
			tst_brkm(TBROK | TERRNO, cleanup,
				"cpid1 clone failed");

		cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
			(void *)child_fn2, NULL);
		if (cpid2 < 0)
			tst_brkm(TBROK | TERRNO, cleanup,
				"cpid2 clone failed");

		if (access("/proc/self/setgroups", F_OK) == 0) {
			sprintf(path, "/proc/%d/setgroups", cpid1);
			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
			SAFE_CLOSE(cleanup, fd);
			/* If the setgroups file has the value "deny",
			 * then the setgroups(2) system call can't
			 * subsequently be reenabled (by writing "allow" to
			 * the file) in this user namespace.  (Attempts to
			 * do so will fail with the error EPERM.)
			*/

			/* test that setgroups can't be re-enabled */
			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
			ret = write(fd, "allow", 5);

			if (ret != -1) {
				tst_brkm(TBROK | TERRNO, cleanup,
					"write action should fail");
			} else if (errno != EPERM) {
				tst_brkm(TBROK | TERRNO, cleanup,
					"unexpected error: \n");
			}
			SAFE_CLOSE(cleanup, fd);
			tst_resm(TPASS, "setgroups can't be re-enabled");

			sprintf(path, "/proc/%d/setgroups", cpid2);
			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
			SAFE_CLOSE(cleanup, fd);
		}

		updatemap(cpid1, UID_MAP, CHILD1UID, parentuid, cleanup);
		updatemap(cpid2, UID_MAP, CHILD2UID, parentuid, cleanup);

		updatemap(cpid1, GID_MAP, CHILD1GID, parentgid, cleanup);
		updatemap(cpid2, GID_MAP, CHILD2GID, parentgid, cleanup);

		TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(cleanup, 1);

		tst_record_childstatus(cleanup, cpid1);
		tst_record_childstatus(cleanup, cpid2);
	}
	cleanup();
	tst_exit();
}