void SymTab::typePrintST(ostream& os, int indent, char leftdelim, char rightdelim, bool linebreak, int first, int last) const { int i; SymTab::const_iterator it = begin(); int n_printed=0; //os << "printST(" << indent << ")"; if ((first == 0) && (last == 0)) last = 1000000; for (i=0; (it != end()) && (i < last); i++, ++it) { SymTabEntry *ste = (SymTabEntry *)(*it); if (i >= first) { if ((ste->kind() != SymTabEntry::RULE_BLOCK_KIND) && (ste->kind() != SymTabEntry::EVENT_BLOCK_KIND) && (ste->name() != "state") && (ste->name() != "other") && (ste->name() != "any")) { n_printed++; } } } if (leftdelim != '\0') { os << leftdelim; if ((n_printed > 0) && (linebreak)) prtln(os, indent+STEP_INDENT); } for (i=0, it=begin(); (it != end()) && (i < last); i++, ++it) { SymTabEntry *ste = (SymTabEntry *)(*it); if (i >= first) { if ((ste->kind() != SymTabEntry::RULE_BLOCK_KIND) && (ste->kind() != SymTabEntry::EVENT_BLOCK_KIND) && (ste->name() != "state") && (ste->name() != "other") && (ste->name() != "any")) { ste->typePrint(os,indent+STEP_INDENT); if ((leftdelim == '\0') && (rightdelim != '\0')) os << rightdelim; if (--n_printed > 0) { if (linebreak) prtln(os,indent+STEP_INDENT); else os << ", "; } else if (linebreak) prtln(os,indent); } } } if (leftdelim != '\0') // This is not a typo -- we shd check leftdelim os << rightdelim; //if (linebreak) //prtln(os, indent); }
void GlobalEntry::typePrint(ostream& out, int indent) const { prtSpace(out, indent); typePrintST(out, indent, '\0', ';', true); prtln(out, indent); const vector<RuleNode*> pr = GlobalEntry::rules(); if(pr.size() == 0 ) prtln(out, indent); for(vector<RuleNode*>::const_iterator it = pr.begin(); it != pr.end(); it++) { (*it)->typePrint(out, indent + STEP_INDENT); endln(out, indent); } prtSpace(out, indent + STEP_INDENT); }
int areq(struct sockaddr* IPaddr, socklen_t sockaddrlen, struct hwaddr *HWaddr) { unsigned char data[UXDOM_BUF_SIZE]; char IP[IP_STR_LEN]; marshalAreq(data, IPaddr, HWaddr); struct sockaddr_un suArpAddr; int iSock = Socket(AF_LOCAL, SOCK_STREAM, 0); bzero(&suArpAddr, sizeof(suArpAddr)); suArpAddr.sun_family = AF_LOCAL; strcpy(suArpAddr.sun_path, ARP_SUN_PATH); Connect(iSock, (SA*)&suArpAddr, sizeof(suArpAddr)); inet_ntop(AF_INET, &((struct sockaddr_in*)IPaddr)->sin_addr, IP, IP_STR_LEN); prtln("\nareq() called, targetIP: %s", IP); write(iSock, data, sizeof(data)); fd_set fsRead; FD_ZERO(&fsRead); FD_SET(iSock, &fsRead); struct timeval tv; tv.tv_sec = AREQ_TIMEOUT; tv.tv_usec = 0; int nReady = select(iSock + 1, &fsRead, NULL, NULL, &tv); if (nReady > 0) { if (FD_ISSET(iSock, &fsRead)) { read(iSock, data, sizeof(data)); unmarshalAreq(IP, HWaddr, data); char mac[MAC_STR_LEN]; sprtMac(mac, HWaddr->sll_addr); prtln("Received Response:"); prtln(" Ethernet Addr: %s", mac); prtln(" Interface Index: %d", HWaddr->sll_ifindex); prtln(" Hard Type: %u", HWaddr->sll_hatype); inet_pton(AF_INET, IP, &((struct sockaddr_in*)IPaddr)->sin_addr); return 0; } } close(iSock); #ifdef DEBUG prtln("areq() time out! Connection to ARP closed!"); #endif return 1; }
/* * This is the meat and potatoes of the program. Spawn creates a tree * of processes with Dval depth and Bval breadth. Each parent will spawn * Bval children. Each child will store information about themselves * in shared memory. The leaf nodes will communicate the existence * of one another through message queues, once each leaf node has * received communication from all of her siblings she will reduce * the semaphore count and exit. Meanwhile all parents are waiting * to hear from their children through the use of semaphores. When * the semaphore count reaches zero then the parent knows all the * children have talked to one another. Locking of the connter semaphore * is provided by the use of another (binary) semaphore. */ int spawn(int val) { extern int sem_count; /* used to keep track of childern */ extern int sem_lock; /* used to lock access to sem_count semaphore */ int i; /* Breadth counter */ static int level = 0; /* level counter */ int lvlflg = 0; /* level toggle, limits parental spawning to one generation */ int pslot = 0; #ifdef __64LDT__ pid_t pid; /* pid of child process */ #else int pid; /* pid of child process */ #endif Pinfo *pinfo; /* pointer to process information in shared mem */ int semval; /* value of semaphore ( equals BVAL initially */ static int tval = 1; /* tree node value of child. */ char foo[1024]; level++; for (i = 1; i <= BVAL; i++) { tval = (val * BVAL) + i; if (!lvlflg) { pid = fork(); if (!pid) { /* CHILD */ if (AUSDEBUG) { sprintf(foo, "%sslot%d", SLOTDIR, tval); debugfp = fopen(foo, "a+"); } pinfo = put_proc_info(tval); debugout ("pid: %-6d ppid: %-6d lev: %-2d i: %-2d val: %-3d\n", pinfo->pid, pinfo->ppid, level, i, tval); set_timer(); /* set up signal handlers and initialize pgrp */ if (level < DVAL) { if (spawn(tval) == -1) { pslot = semoper(tval, sem_lock, -1); semarg.val = 0; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ semval = semctl(sem_count, pslot, GETVAL, semarg); semarg.val = --semval; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ semctl(sem_count, pslot, SETVAL, semarg); semarg.val = 1; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ semctl(sem_lock, pslot, SETVAL, semarg); } lvlflg++; } else { /* leaf node */ notify(tval); return (-1); } } #ifdef __64LDT__ else if (pid > 0 && i >= BVAL) { /* PARENT */ #else else if (pid > (pid_t) 0 && i >= BVAL) { /* PARENT */ #endif pslot = semoper(tval, sem_count, 0); pslot = semoper(pslot, sem_lock, -1); semarg.val = 0; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ semval = semctl(sem_count, pslot, GETVAL, semarg); semarg.val = --semval; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ semctl(sem_count, pslot, SETVAL, semarg); semarg.val = 1; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ semctl(sem_lock, pslot, SETVAL, semarg); (shmaddr + val)->msg++; } #ifdef __64LDT__ else if (pid < (pid_t) 0) { #else else if (pid < 0) { #endif perror("spawn: fork failed"); severe ("spawn: fork failed, exiting with errno %d\n", errno); exit(1); } else (shmaddr + val)->msg++; } } return (pslot); } /* * Allocate message queues. */ void setup_msgqueue(void) { extern int msgid; extern int msgerr; msgid = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (msgid == -1) { perror("msgget msgid failed"); fprintf(stderr, " SEVERE : msgget msgid failed: errno %d\n", errno); exit(1); } msgerr = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (msgerr == -1) { perror("msgget msgerr failed"); fprintf(stderr, " SEVERE : msgget msgerr failed: errno %d\n", errno); exit(1); } } /* * Set up and initialize all semaphores */ void setup_semaphores(void) { extern int sem_count; extern int sem_lock; int i; int rc; prtln(); sem_lock = semget(IPC_PRIVATE, nodesum - 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); dprt("nodesum = %d, sem_lock = %d\n", nodesum, sem_lock); prtln(); if (sem_lock == -1) { perror("semget failed for sem_lock"); fprintf(stderr, " SEVERE : semget failed for sem_lock, errno: %d\n", errno); rm_shmseg(); exit(1); } prtln(); sem_count = semget(IPC_PRIVATE, nodesum - 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (sem_count == -1) { perror("semget failed for sem_count"); fprintf(stderr, " SEVERE : semget failed for sem_count, errno: %d\n", errno); rm_shmseg(); exit(1); } prtln(); for (i = 0; i < (nodesum - 1); i++) { semarg.val = 1; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ rc = semctl(sem_lock, i, SETVAL, semarg); prtln(); if (rc == -1) { perror("semctl failed for sem_lock failed"); fprintf(stderr, " SEVERE : semctl failed for sem_lock, errno: %d\n", errno); rm_shmseg(); exit(1); } semarg.val = BVAL; /* to fix problem with 4th arg of semctl in 64 bits MARIOG */ rc = semctl(sem_count, i, SETVAL, semarg); prtln(); if (rc == -1) { perror("semctl failed for sem_lock failed"); fprintf(stderr, " SEVERE : semctl failed for sem_lock, errno: %d\n", errno); rm_shmseg(); exit(1); } } } /* * Set up and allocate shared memory. */ void setup_shm(void) { extern int nodesum; /* global shared memory id */ extern int shmid; /* global shared memory id */ extern Pinfo *shmaddr; int i, j; /* counters */ Pinfo *shmad = NULL; /* ptr to start of shared memory. */ Pinfo *pinfo = NULL; /* ptr to struct in shared memory. */ debugout("size = %d, size (in hex) = %#x nodes: %d\n", sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)), sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)), nodesum); /* Get shared memory id */ shmid = shmget(IPC_PRIVATE, sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)), IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (shmid < 0) { perror("shmget failed"); fprintf(stderr, " SEVERE : shmget failed: errno %d\n", errno); exit(1); } /* allocate shared memory */ if ((shmad = shmat(shmid, (char *)shmad, 0)) == MAP_FAILED) { printf("SEVERE : shmat failed\n"); exit(1); } else { shmctl(shmid, IPC_RMID, NULL); } /* set all fields in shared memory to -1 */ for (pinfo = shmad, i = 0; i < nodesum; i++, pinfo++) { #ifdef __64LDT__ pinfo->pid = (pid_t) - 1; pinfo->ppid = (pid_t) - 1; #else pinfo->pid = -1; pinfo->ppid = -1; #endif pinfo->msg = -1; pinfo->err = -1; /* Changed 10/9/97 */ /* pinfo->list = (int *)((ulong)shmad + nodesum * sizeof(Pinfo) + (sizeof(int) * BVAL * i)); */ pinfo->list = (int *)((long)shmad + nodesum * sizeof(Pinfo) + (sizeof(int) * BVAL * i)); for (j = 0; j < BVAL; j++) *(pinfo->list + j) = -1; } shmaddr = shmad; } /* * Set up Signal handler and which signals to catch */ void set_signals(void *sighandler()) { int i; int rc; struct sigaction action; /* list of signals we want to catch */ static struct signalinfo { int signum; char *signame; } siginfo[] = { { SIGHUP, "SIGHUP"}, { SIGINT, "SIGINT"}, { SIGQUIT, "SIGQUIT"}, { SIGABRT, "SIGABRT"}, { SIGBUS, "SIGBUS"}, { SIGSEGV, "SIGSEGV"}, { SIGALRM, "SIGALRM"}, { SIGUSR1, "SIGUSR1"}, { SIGUSR2, "SIGUSR2"}, { -1, "ENDSIG"} }; char tmpstr[1024]; action.sa_handler = (void *)sighandler; #ifdef _LINUX sigfillset(&action.sa_mask); #else SIGINITSET(action.sa_mask); #endif action.sa_flags = 0; /* Set the signal handler up */ #ifdef _LINUX sigaddset(&action.sa_mask, SIGTERM); #else SIGADDSET(action.sa_mask, SIGTERM); #endif for (i = 0; siginfo[i].signum != -1; i++) { #ifdef _LINUX sigaddset(&action.sa_mask, siginfo[i].signum); #else SIGADDSET(action.sa_mask, siginfo[i].signum); #endif rc = sigaction(siginfo[i].signum, &action, NULL); if (rc == -1) { sprintf(tmpstr, "sigaction: %s\n", siginfo[i].signame); perror(tmpstr); fprintf(stderr, " SEVERE : Could not set %s signal action, errno=%d.", siginfo[i].signame, errno); exit(1); } } }