static int shmrmall(void) { int mib[4]; struct shm_sysctl_info *shmsi; size_t i, len; int result = 0; mib[0] = CTL_KERN; mib[1] = KERN_SYSVIPC; mib[2] = KERN_SYSVIPC_INFO; mib[3] = KERN_SYSVIPC_SHM_INFO; if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1) err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)"); if ((shmsi = malloc(len)) == NULL) err(1, "malloc"); if (sysctl(mib, 4, shmsi, &len, NULL, 0) == -1) { free(shmsi); err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)"); } for (i = 0; i < shmsi->shminfo.shmmni; i++) { struct shmid_ds_sysctl *shmptr = &shmsi->shmids[i]; if (shmptr->shm_perm.mode & 0x0800) result -= shmrm((key_t)0, (int)IXSEQ_TO_IPCID(i, shmptr->shm_perm)); } free(shmsi); return result; }
int main(int argc, char **argv) { int c, result, errflg, target_id; key_t target_key; errflg = 0; signal(SIGSYS, not_configured); while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { signaled = 0; switch (c) { case 'q': case 'm': case 's': target_id = atoi(optarg); if (c == 'q') result = msgrm(0, target_id); else if (c == 'm') result = shmrm(0, target_id); else result = semrm(0, target_id); if (result < 0) { errflg++; if (!signaled) warn("%sid(%d)", IPC_TO_STR(toupper(c)), target_id); else warnx("%ss are not configured in the running kernel", IPC_TO_STRING(toupper(c))); } break; case 'Q': case 'M': case 'S': target_key = atol(optarg); if (target_key == IPC_PRIVATE) { warnx("can't remove private %ss", IPC_TO_STRING(c)); continue; } if (c == 'Q') result = msgrm(target_key, 0); else if (c == 'M') result = shmrm(target_key, 0); else result = semrm(target_key, 0); if (result < 0) { errflg++; if (!signaled) warn("%s key %ld aborted", IPC_TO_STR(c), target_key); else warnx("%ss are not configured in the running kernel", IPC_TO_STRING(c)); } break; case ':': warnx("option -%c requires an argument\n", optopt); usage(); case '?': warnx("unrecognized option: -%c\n", optopt); usage(); } } if (optind != argc) { warnx("unknown argument: %s\n", argv[optind]); usage(); } exit(errflg); }
int main(int argc, char *argv[]) { int c, result, target_id; key_t target_key; while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) { signaled = 0; switch (c) { case 'v': rmverbose++; break; case 'y': use_sysctl = 0; break; } } optind = 1; errflg = 0; signal(SIGSYS, not_configured); while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) { signaled = 0; switch (c) { case 'q': case 'm': case 's': target_id = atoi(optarg); if (c == 'q') result = msgrm(0, target_id); else if (c == 'm') result = shmrm(0, target_id); else result = semrm(0, target_id); if (result < 0) { errflg++; if (!signaled) warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); else warnx( "%ss are not configured " "in the running kernel", IPC_TO_STRING(toupper(c))); } break; case 'Q': case 'M': case 'S': target_key = atol(optarg); if (target_key == IPC_PRIVATE) { warnx("can't remove private %ss", IPC_TO_STRING(c)); continue; } if (c == 'Q') result = msgrm(target_key, 0); else if (c == 'M') result = shmrm(target_key, 0); else result = semrm(target_key, 0); if (result < 0) { errflg++; if (!signaled) warn("%ss(%ld): ", IPC_TO_STR(c), target_key); else warnx("%ss are not configured " "in the running kernel", IPC_TO_STRING(c)); } break; case 'v': case 'y': /* Handled in other getopt() loop */ break; case 'W': msgrm(-1, 0); shmrm(-1, 0); semrm(-1, 0); break; case ':': fprintf(stderr, "option -%c requires an argument\n", optopt); usage(); case '?': fprintf(stderr, "unrecognized option: -%c\n", optopt); usage(); } } if (optind != argc) { fprintf(stderr, "unknown argument: %s\n", argv[optind]); usage(); } exit(errflg); }
// Application entry point. // // Gets a list of SHMs on the system. // Then, removes them. // Gets a list of sems on the system. // Then, removes them. // Prints out totals of removed objects. int main(int argc, char **argv) { int procs,rank; int shm_rm=0,sem_rm=0,shm_fl=0; int *rbuf=NULL; int i; // Initialize MPI if( MPI_Init(&argc,&argv) != MPI_SUCCESS ) { fprintf(stderr,"Error starting MPI. Terminating.\n"); return 1; } MPI_Comm_size(MPI_COMM_WORLD,&procs); MPI_Comm_rank(MPI_COMM_WORLD,&rank); // Remove all SHMs { struct shm_info shm_info; struct shmid_ds shmseg; int maxid, shmid, id; // Get the largest SHM id on the system maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info); if( maxid < 0 ) { fprintf(stderr,"shmctl() says shared memory is not supported.\n"); MPI_Abort(1,MPI_COMM_WORLD); } // Check each id less than the max for(id=shm_rm=shm_fl=0; id <= maxid; id++) { // Get info on the SHM shmid = shmctl(id, SHM_STAT, &shmseg); if( shmid < 0 ) { // No SHM with this id exists; keep trying the others. continue; } // Remove the SHM if it is not the system SHM (id == 0) if( shmseg.shm_perm.__key ) { if( shmrm(shmseg.shm_perm.__key) < 0 ) { // The SHM could not be removed for some reason... shm_fl++; continue; } // The SHM was removed shm_rm++; } } } // Remove all semaphores { struct semid_ds semary; struct seminfo seminfo; union semun arg; int maxid, semid, id; // Get the largest sem id on the system arg.array = (unsigned short *) (void *) &seminfo; maxid = semctl(0, 0, SEM_INFO, arg); if( maxid < 0 ) { fprintf(stderr,"semctl() says semaphores are not supported.\n"); MPI_Abort(1,MPI_COMM_WORLD); } // Check each id less than the max for(id=sem_rm=0; id <= maxid; id++) { // Get info on the sem arg.buf = (struct semid_ds *) &semary; semid = semctl(id, 0, SEM_STAT, arg); if( semid < 0 ) { // No semaphore with this id exists; keep trying the others. continue; } // Remove the semaphore set if( semrm(semary.sem_perm.__key) < 0 ) { // The semaphore set could not be removed for some reason... continue; } // The semaphore set was removed sem_rm++; } } // Only root needs the array (entry per node) if( !rank ) { if( !(rbuf=(int*)malloc(procs*sizeof(int))) ) { // Malloc failed MPI_Abort(1,MPI_COMM_WORLD); } } // Gather all removed SHM counts MPI_Gather(&shm_rm, 1, MPI_INT, rbuf, 1, MPI_INT, 0, MPI_COMM_WORLD); // Only root sums for total count if( !rank ) { for(i=shm_rm=0; i<procs; i++) { shm_rm += rbuf[i]; } } // Gather all removed sem counts MPI_Gather(&sem_rm, 1, MPI_INT, rbuf, 1, MPI_INT, 0, MPI_COMM_WORLD); // Only root sums for total count if( !rank ) { for(i=sem_rm=0; i<procs; i++) { sem_rm += rbuf[i]; } } // Gather all not removed shm counts MPI_Gather(&shm_fl, 1, MPI_INT, rbuf, 1, MPI_INT, 0, MPI_COMM_WORLD); // Only root sums for total count if( !rank ) { for(i=shm_fl=0; i<procs; i++) { shm_fl += rbuf[i]; } } // Only root needs to report stats if( !rank ) { // Print out number of removed segments printf("Removed a total of %d SHMs for %d nodes.\n",shm_rm,procs); printf("Removed a total of %d sems for %d nodes.\n",sem_rm,procs); // Print out any failures if( shm_fl ) { printf("\n!! Failed to remove %d SHMs for %d nodes:\n",shm_fl,procs); for(i=0; i<procs; i++) { if( rbuf[i] ) { printf("Rank %d still has %d SHMs.\n",i,rbuf[i]); } } } // Cleanup free(rbuf); } // Mark that we have cleaned if( !rank ) { FILE *f; if( !(f=fopen("cleaned","w")) ) { MPI_Abort(1,MPI_COMM_WORLD); } fclose(f); } // Done with MPI MPI_Finalize(); // Return success return 0; }
int main(int argc, char *argv[]) { int c, result, errflg, target_id; key_t target_key; setprogname(argv[0]); errflg = 0; (void)signal(SIGSYS, not_configured); while ((c = getopt(argc, argv, "q:m:s:Q:M:S:")) != -1) { signaled = 0; target_id = 0; target_key = 0; result = 0; if (optarg != NULL && strcmp(optarg, "all") == 0) { switch (c) { case 'm': case 'M': result = shmrmall(); break; case 'q': case 'Q': result = msgrmall(); break; case 's': case 'S': result = semrmall(); break; default: usage(); } } else { switch (c) { case 'q': case 'm': case 's': target_id = atoi(optarg); break; case 'Q': case 'M': case 'S': target_key = atol(optarg); if (target_key == IPC_PRIVATE) { warnx("can't remove private %ss", IPC_TO_STRING(c)); continue; } break; default: usage(); } switch (c) { case 'q': result = msgrm((key_t)0, target_id); break; case 'm': result = shmrm((key_t)0, target_id); break; case 's': result = semrm((key_t)0, target_id); break; case 'Q': result = msgrm(target_key, 0); break; case 'M': result = shmrm(target_key, 0); break; case 'S': result = semrm(target_key, 0); break; } } if (result < 0) { if (!signaled) { if (target_id) { warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); errflg++; } else if (target_key) { warn("%skey(%ld): ", IPC_TO_STR(c), (long)target_key); errflg++; } } else { errflg++; warnx("%ss are not configured in " "the running kernel", IPC_TO_STRING(toupper(c))); } } } if (optind != argc) { warnx("Unknown argument: %s", argv[optind]); usage(); } return errflg; }