#include #include #include #include #include #include #include void failed(int code) { fputs("Failed\n", stderr) ; exit(code) ; } int main(int argc, char *argv[], char *envp[]) { uid_t yourID ; pid_t ppid ; struct passwd *yourPWinfo ; char *yourName, *shmIDstr, IDbuff[256] ; int shmID ; void *shmAddr ; yourID = getuid() ; if ((yourPWinfo = getpwuid(yourID)) == NULL) { fprintf(stdout, "Unable to find PW entry for %d\n", yourID) ; exit(1) ; } yourName = yourPWinfo->pw_name ; ppid = getppid() ; fprintf(stdout, "Called by process %d run by %s\n", ppid, yourName) ; if (strcmp(argv[1], "csci331")) failed(101) ; if (getenv("CLASS") == NULL) failed(102) ; if (strcmp(getenv("CLASS"), "csci331")) failed(103) ; if ((shmIDstr=getenv("SHMID")) == NULL) failed(104) ; shmID = 0 ; while (*shmIDstr) { if (('0' <= *shmIDstr) && (*shmIDstr <= '9')) shmID = 10*shmID + *shmIDstr - '0' ; else failed(105) ; ++shmIDstr ; } if ((shmAddr = shmat(shmID, (void *)NULL, 0)) == (void *)-1) failed(106) ; if (((int *)shmAddr)[0] != yourID) failed(107) ; if (((int *)shmAddr)[1] != ppid) failed(108) ; sprintf(IDbuff, "I am %s at %d", yourName, ppid) ; fprintf(stdout, "%s\n", IDbuff) ; if (strcmp((char *)(((int *)shmAddr)+2), IDbuff)) failed(109) ; exit(0) ; }