samedi 28 février 2015

Pages getting (deleted) when using shmget and shmat

I wrote the following 2 test programs, one that uses shmat and another that uses shmget and execve's the first binary.


The code is as follows :-



int main(int argc, char **argv) {
void *retval;
long shmid = atol(argv[1]);

retval = shmat(shmid, NULL, SHM_RDONLY);
printf("%p\n", retval);
}


And the wrapper file,



int setupshm(char *name) {
int shmid;
shmid = shmget(IPC_PRIVATE, 100, IPC_CREAT|0666);
return shmid;
}

int main() {

int pid = fork();
if (pid == 0) {
char **envp = NULL;
char *argv[3];
char num[10];

sprintf(num, "%d", setupshm("whatever"));

argv[1] = "./test";
argv[2] = num;
argv[3] = NULL;

execve("./test", argv, envp);
}

else { int status; wait(pid, &status, 0); }
}


I run the wrapper program in gdb, intercept the execve, debug the test program, see that shmget succeeds; then inspect its /proc/pid/maps and I see that the page corresponding to the shmget shows up as "(Deleted)". Why does this happen? What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire