/* redirects the output of ipcs to grep "" redirected to "ipc.dat"(temporary) file Then, redirects output of whoami to whoami.dat (temp file). the ipc.dat file is read and contents analyzed and a commandline for ipcsrm is prepared and then a system call to ipcsrm is done */ #include char str[300]; char line[300]; char cline[300]; char dline[300]; FILE *fin; int i; main(int argc, char *argv[]) {system("whoami > whoami.dat"); fin = fopen("whoami.dat","r"); fscanf(fin,"%s\n",dline); fclose(fin); unlink("whoami.dat"); sprintf( str, "ipcs |grep %s >ipc.dat",dline); system( str ); fin = fopen("ipc.dat", "r"); if (fin==NULL) { printf("Error in ipcs piping \n"); exit(0); } strcpy(cline, "ipcrm"); i=0; while( !feof(fin)) { i++; fscanf(fin, "%s\n", line ); /* m or s */ if (feof(fin)) break; strcat(cline, " -"); strcat( cline, line ); fscanf( fin, "%s\n", line ); /* shmid, semid */ strcat( cline, " "); strcat( cline, line ); fscanf( fin, "%s\n", line); /* uid / key */ fscanf( fin, "%s", line ); /* mode */ fscanf( fin, "%s", line ); /* owner */ fscanf( fin, "%s", line ); /* group */ } if (i==1) { printf("No IPC resources present for user %s\n",dline); unlink("ipc.dat"); exit(0); } printf("\n Calling : %s \n", cline ); system( cline ); unlink("ipc.dat"); }