/* * open ndbfile as db if not already open. also check for stale data * and reload as needed. */ static Ndb * opendb(void) { static ulong lastcheck; /* check no more often than once every minute */ if(db == nil) { db = ndbopen(ndbfile); if(db != nil) lastcheck = now; } else if(now >= lastcheck + 60) { if (ndbchanged(db)) ndbreopen(db); lastcheck = now; } return db; }
/* * open a single file */ static struct ndb* doopen(char *file) { struct ndb *db; db = (struct ndb*)calloc(sizeof(struct ndb), 1); if(db == 0) return 0; memset(db, 0, sizeof(struct ndb)); strncpy(db->file, file, sizeof(db->file)-1); if(ndbreopen(db) < 0){ free(db); return 0; } return db; }
/* * get the system name */ static void ipid(void) { uint8_t addr[6]; struct ndbtuple *t, *tt; char *p, *attr; struct ndbs s; int f; char buf[Maxpath]; /* use environment, ether addr, or ipaddr to get system name */ if (mysysname == 0) { /* * environment has priority. * * on the sgi power the default system name * is the ip address. ignore that. * */ p = getenv("sysname"); if (p && *p) { attr = ipattr(p); if (strcmp(attr, "ip") != 0) mysysname = strdup(p); } /* * the /net/ndb contains what the network * figured out from DHCP. use that name if * there is one. */ if (mysysname == 0 && netdb != NULL) { ndbreopen(netdb); for (tt = t = ndbparse(netdb); t != NULL; t = t->entry) { if (strcmp(t->attr, "sys") == 0) { mysysname = strdup(t->val); break; } } ndbfree(tt); } /* next network database, ip address, and ether address to find * a name */ if (mysysname == 0) { t = NULL; if (isvalidip(ipa)) free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t)); if (t == NULL) { for (f = 0; f < 3; f++) { snprintf(buf, sizeof(buf), "%s/ether%d", mntpt, f); if (myetheraddr(addr, buf) < 0) continue; snprintf(eaddr, sizeof(eaddr), "%E", addr); free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t)); if (t != NULL) break; } } for (tt = t; tt != NULL; tt = tt->entry) { if (strcmp(tt->attr, "sys") == 0) { mysysname = strdup(tt->val); break; } } ndbfree(t); } /* nothing else worked, use the ip address */ if (mysysname == 0 && isvalidip(ipa)) mysysname = strdup(ipaddr); /* set /dev/sysname if we now know it */ if (mysysname) { f = open("/dev/sysname", O_RDWR); if (f >= 0) { write(f, mysysname, strlen(mysysname)); close(f); } } } }