EOF [geoff] --rw-rw-r-- M 59 glenda sys 25852 Apr 12 01:38 sys/src/cmd/aux/timesync.c /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:17,28 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:17,28 Utc, Gps, - HZAvgSecs= 3*60, /* target averaging period for the frequency in seconds */ - MinSampleSecs= 60, /* minimum sampling time in seconds */ + HZAvgSecs= 3*60, /* target averaging period for frequency in seconds */ + MinSampleSecs= 60, /* minimum sampling time in seconds */ }; - char *dir = "/tmp"; // directory sample files live in + char *dir = "/tmp"; /* directory sample files live in */ char *logfile = "timesync"; char *timeserver; char *Rootid; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:32,41 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:32,41 int impotent; int logging; int type; - int gmtdelta; // rtc+gmtdelta = gmt + int gmtdelta; /* rtc+gmtdelta = gmt */ uvlong avgerr; - // ntp server info + /* ntp server info */ int stratum = 14; vlong mydisp, rootdisp; vlong mydelay, rootdelay; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:45,51 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:45,51 char *sysid; int myprec; - // list of time samples + /* list of time samples */ typedef struct Sample Sample; struct Sample { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:55,61 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:55,61 vlong stime; }; - // ntp packet + /* ntp packet */ typedef struct NTPpkt NTPpkt; struct NTPpkt { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:67,80 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:67,80 uchar rootdisp[4]; uchar rootid[4]; uchar refts[8]; - uchar origts[8]; // departed client - uchar recvts[8]; // arrived at server - uchar xmitts[8]; // departed server + uchar origts[8]; /* departed client */ + uchar recvts[8]; /* arrived at server */ + uchar xmitts[8]; /* departed server */ uchar keyid[4]; uchar digest[16]; }; - // ntp server + /* ntp server */ typedef struct NTPserver NTPserver; struct NTPserver { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:92,102 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:92,102 enum { - NTPSIZE= 48, // basic ntp packet - NTPDIGESTSIZE= 20, // key and digest + NTPSIZE= 48, /* basic ntp packet */ + NTPDIGESTSIZE= 20, /* key and digest */ }; - // error bound of last sample + /* error bound of last sample */ ulong ε; static void addntpserver(char *name); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:104,110 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:104,110 static void background(void); static int caperror(vlong dhz, int tsecs, vlong taccuracy); static long fstime(void); - static int gettime(vlong *nsec, uvlong *ticks, uvlong *hz); // returns time, ticks, hz + static int gettime(vlong *nsec, uvlong *ticks, uvlong *hz); /* returns time, ticks, hz */ static int getclockprecision(vlong); static vlong gpssample(void); static void hnputts(void *p, vlong nsec); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:121,153 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:121,159 static vlong sample(long (*get)(void)); static void setpriority(void); static void setrootid(char *d); - static void settime(vlong now, uvlong hz, vlong delta, int n); // set time, hz, delta, period + static void settime(vlong now, uvlong hz, vlong delta, int n); /* set time, hz, delta, period */ static vlong utcsample(void); static uvlong vabs(vlong); - static uvlong whatisthefrequencykenneth(uvlong hz, uvlong minhz, uvlong maxhz, vlong dt, vlong ticks, vlong period); + static uvlong whatisthefrequencykenneth(uvlong hz, uvlong minhz, uvlong maxhz, + vlong dt, vlong ticks, vlong period); static void writefreqfile(int fd, vlong hz, int secs, vlong diff); - // ((1970-1900)*365 + 17/*leap days*/)*24*60*60 + // ((1970-1900)*365 + 17 /*leap days*/)*24*60*60 #define EPOCHDIFF 2208988800UL + static void + usage(void) + { + fprint(2, "usage: %s [-a accuracy][-d dir][-I rootid][-s net]" + "[-S stratum][-DfGilLnrU] timesource ...\n", argv0); + exits("usage"); + } + void main(int argc, char **argv) { - int i; - int secs; // sampling period - int tsecs; // temporary sampling period - int t, fd; - Sample *s, *x, *first, **l; - vlong diff, accuracy, taccuracy; + int i, t, fd, nservenet; + int secs; /* sampling period */ + int tsecs; /* temporary sampling period */ uvlong hz, minhz, maxhz, period, nhz; + vlong diff, accuracy, taccuracy; char *servenet[4]; - int nservenet; - char *a; + Sample *s, *x, *first, **l; Tm tl, tg; - type = Fs; // by default, sync with the file system + type = Fs; /* by default, sync with the file system */ debug = 0; - accuracy = 1000000LL; // default accuracy is 1 millisecond + accuracy = 1000000LL; /* default accuracy is 1 millisecond */ nservenet = 0; tsecs = secs = MinSampleSecs; timeserver = ""; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:154,203 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:160,207 ARGBEGIN{ case 'a': - a = ARGF(); - if(a == nil) + accuracy = strtoll(EARGF(usage()), 0, 0); /* specified in ns */ + if(accuracy <= 1) sysfatal("bad accuracy specified"); - accuracy = strtoll(a, 0, 0); // accuracy specified in ns - if(accuracy <= 1LL) - sysfatal("bad accuracy specified"); break; + case 'd': + dir = EARGF(usage()); + break; + case 'D': + debug = 1; + break; case 'f': type = Fs; stratum = 2; break; - case 'r': - type = Rtc; - stratum = 0; - break; - case 'U': - type = Utc; - stratum = 1; - break; case 'G': type = Gps; stratum = 1; break; - case 'n': - type = Ntp; + case 'i': + impotent = 1; break; - case 'D': - debug = 1; + case 'I': + Rootid = EARGF(usage()); break; - case 'd': - dir = ARGF(); + case 'l': + logging = 1; break; case 'L': - // - // Assume time source in local time rather than GMT. - // Calculate difference so that rtctime can return GMT. - // This is useful with the rtc on PC's that run Windows - // since Windows keeps the local time in the rtc. - // + /* + * Assume time source in local time rather than GMT. + * Calculate difference so that rtctime can return GMT. + * This is useful with the rtc on PC's that run Windows + * since Windows keeps the local time in the rtc. + */ t = time(0); tl = *localtime(t); tg = *gmtime(t); - // if the years are different, we're at most a day off, so just rewrite + /* + * if the years are different, we're at most a day off, + * so just rewrite + */ if(tl.year < tg.year){ tg.year--; tg.yday = tl.yday + 1; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:215,243 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:219,245 assert(abs(gmtdelta) <= 24*60*60); break; - case 'i': - impotent = 1; + case 'n': + type = Ntp; break; - case 'I': - Rootid = ARGF(); + case 'r': + type = Rtc; + stratum = 0; break; + case 'U': + type = Utc; + stratum = 1; + break; case 's': if(nservenet >= nelem(servenet)) sysfatal("too many networks to serve on"); - a = ARGF(); - if(a == nil) - sysfatal("must specify network to serve on"); - servenet[nservenet++] = a; + servenet[nservenet++] = EARGF(usage()); break; - case 'l': - logging = 1; - break; case 'S': - a = ARGF(); - if(a == nil) - sysfatal("bad stratum specified"); - stratum = strtoll(a, 0, 0); + stratum = strtoll(EARGF(usage()), 0, 0); break; + default: + usage(); }ARGEND; fmtinstall('E', eipfmt); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:245,251 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:247,253 fmtinstall('V', eipfmt); sysid = getenv("sysname"); - // detach from the current namespace + /* detach from the current namespace */ if(debug) rfork(RFNAMEG); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:269,286 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:271,287 timeserver = "/srv/boot"; break; case Ntp: - if(argc > 0){ - for(i = 0; i 0) + for(i = 0; i < argc; i++) addntpserver(argv[i]); - } else { + else addntpserver("$ntp"); - } break; } setpriority(); - // figure out our time interface and initial frequency + /* figure out our time interface and initial frequency */ inittime(); gettime(0, 0, &hz); minhz = hz/10; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:287,298 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:288,299 maxhz = hz*10; myprec = getclockprecision(hz); - // convert the accuracy from nanoseconds to ticks + /* convert the accuracy from nanoseconds to ticks */ taccuracy = hz*accuracy/SEC; - // - // bind in clocks - // + /* + * bind in clocks + */ switch(type){ case Fs: fd = open(timeserver, ORDWR); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:321,359 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:322,357 break; } - // - // start a local ntp server(s) - // - for(i = 0; i < nservenet; i++){ + /* + * start a local ntp server(s) + */ + for(i = 0; i < nservenet; i++) switch(rfork(RFPROC|RFFDG|RFMEM|RFNOWAIT)){ case -1: sysfatal("forking: %r"); - break; case 0: ntpserver(servenet[i]); _exits(0); - break; - default: - break; } - } - // get the last known frequency from the file + /* get the last known frequency from the file */ fd = openfreqfile(); hz = readfreqfile(fd, hz, minhz, maxhz); - // this is the main loop. it gets a sample, adjusts the - // clock and computes a sleep period until the next loop. - // we balance frequency drift against the length of the - // period to avoid blowing the accuracy limit. + /* + * this is the main loop. it gets a sample, adjusts the + * clock and computes a sleep period until the next loop. + * we balance frequency drift against the length of the + * period to avoid blowing the accuracy limit. + */ first = nil; l = &first; - avgerr = accuracy>>1; - for(;; background(),sleep(tsecs*(1000))){ - s = mallocz(sizeof(*s), 1); + avgerr = accuracy >> 1; + for(;; background(), sleep(tsecs*1000)){ + s = mallocz(sizeof *s, 1); diff = 0; - // get times for this sample + /* get times for this sample */ ε = ~0; switch(type){ case Fs: /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:396,440 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:394,442 } } - // use fastest method to read local clock and ticks + /* use fastest method to read local clock and ticks */ gettime(&s->ltime, &s->ticks, 0); if(type == Ntp || type == Gps) s->stime = s->ltime + diff; - // if the sample was bad, ignore it + /* if the sample was bad, ignore it */ if(s->stime < 0){ free(s); continue; } - // reset local time + /* reset local time */ diff = s->stime - s->ltime; if(diff > 10*SEC || diff < -10*SEC){ - // we're way off, just set the time + /* we're way off, just set the time */ secs = MinSampleSecs; settime(s->stime, 0, 0, 0); } else { - // keep a running average of the error. + /* keep a running average of the error. */ avgerr = (avgerr>>1) + (vabs(diff)>>1); - // the time to next sample depends on how good or - // bad we're doing. + /* + * the time to next sample depends on how good or + * bad we're doing. + */ tsecs = secs = adjustperiod(diff, accuracy, secs); - // work off the fixed difference. This is done - // by adding a ramp to the clock. Each 100th of a - // second (or so) the kernel will add diff/(4*secs*100) - // to the clock. we only do 1/4 of the difference per - // period to dampen any measurement noise. - // - // any difference greater than ε we work off during the - // sampling period. - if(abs(diff) > ε){ + /* + * work off the fixed difference. This is done + * by adding a ramp to the clock. Each 100th of a + * second (or so) the kernel will add diff/(4*secs*100) + * to the clock. we only do 1/4 of the difference per + * period to dampen any measurement noise. + * + * any difference greater than ε we work off during the + * sampling period. + */ + if(abs(diff) > ε) if(diff > 0) settime(-1, 0, diff-((3*ε)/4), secs); else settime(-1, 0, diff+((3*ε)/4), secs); - } else + else settime(-1, 0, diff, 4*secs); } /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:441,447 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:443,449 if(debug) fprint(2, "δ %lld avgδ %lld f %lld\n", diff, avgerr, hz); - // dump old samples (keep at least one) + /* dump old samples (keep at least one) */ while(first != nil){ if(first->next == nil) break; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:452,472 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:454,474 free(x); } - // The sampling error is limited by the total error. If - // we make sure the sampling period is at least 16 million - // times the average error, we should calculate a frequency - // with on average a 1e-7 error. - // - // So that big hz changes don't blow our accuracy requirement, - // we shorten the period to make sure that δhz*secs will be - // greater than the accuracy limit. - period = avgerr<<24; - for(x = first; x != nil; x = x->next){ - if(s->stime - x->stime < period) + /* + * The sampling error is limited by the total error. If + * we make sure the sampling period is at least 16 million + * times the average error, we should calculate a frequency + * with on average a 1e-7 error. + * + * So that big hz changes don't blow our accuracy requirement, + * we shorten the period to make sure that δhz*secs will be + * greater than the accuracy limit. + */ + period = avgerr << 24; + for(x = first; x != nil; x = x->next) + if(s->stime - x->stime < period || + x->next == nil || s->stime - x->next->stime < period) break; - if(x->next == nil || s->stime - x->next->stime < period) - break; - } if(x != nil){ nhz = whatisthefrequencykenneth( hz, minhz, maxhz, /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:478,484 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:480,486 writefreqfile(fd, hz, (s->stime - x->stime)/SEC, diff); } - // add current sample to list. + /* add current sample to list. */ *l = s; l = &s->next; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:488,496 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:490,498 } } - // - // adjust the sampling period with some histeresis - // + /* + * adjust the sampling period with some histeresis + */ static int adjustperiod(vlong diff, vlong accuracy, int secs) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:509,527 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:511,527 return secs; } - // - // adjust the frequency - // + /* + * adjust the frequency + */ static uvlong - whatisthefrequencykenneth(uvlong hz, uvlong minhz, uvlong maxhz, vlong dt, vlong ticks, vlong period) + whatisthefrequencykenneth(uvlong hz, uvlong minhz, uvlong maxhz, vlong dt, + vlong ticks, vlong period) { - static mpint *mpdt; - static mpint *mpticks; - static mpint *mphz; - static mpint *mpbillion; uvlong ohz = hz; + static mpint *mpdt, *mpticks, *mphz, *mpbillion; - // sanity check + /* sanity check */ if(dt <= 0 || ticks <= 0) return hz; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:530,536 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:530,536 mpbillion = uvtomp(SEC, nil); } - // hz = (ticks*SEC)/dt + /* hz = (ticks*SEC)/dt */ mpdt = vtomp(dt, mpdt); mpticks = vtomp(ticks, mpticks); mpmul(mpticks, mpbillion, mpticks); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:537,547 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:537,547 mpdiv(mpticks, mpdt, mphz, nil); hz = mptoui(mphz); - // sanity + /* sanity */ if(hz < minhz || hz > maxhz) return ohz; - // damp the change if we're shorter than the target period + /* damp the change if we're shorter than the target period */ if(period > dt) hz = (12ULL*ohz + 4ULL*hz)/16ULL; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:549,560 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:549,562 return hz; } - // We may be changing the frequency to match a bad measurement - // or to match a condition no longer in effet. To make sure - // that this doesn't blow our error budget over the next measurement - // period, shorten the period to make sure that δhz*secs will be - // less than the accuracy limit. Here taccuracy is accuracy converted - // from nanoseconds to ticks. + /* + * We may be changing the frequency to match a bad measurement + * or to match a condition no longer in effect. To make sure + * that this doesn't blow our error budget over the next measurement + * period, shorten the period to make sure that δhz*secs will be + * less than the accuracy limit. Here taccuracy is accuracy converted + * from nanoseconds to ticks. + */ static int caperror(vlong dhz, int tsecs, vlong taccuracy) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:570,578 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:572,580 return tsecs; } - // - // kernel interface - // + /* + * kernel interface + */ enum { Ibintime, /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:595,607 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:597,609 else mode = ORDWR; - // bind in clocks + /* bind in clocks */ if(access("/dev/time", 0) < 0) bind("#c", "/dev", MAFTER); if(access("/dev/rtc", 0) < 0) bind("#r", "/dev", MAFTER); - // figure out what interface we have + /* figure out what interface we have */ ifc = Ibintime; bintimefd = open("/dev/bintime", mode); if(bintimefd >= 0) /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:619,627 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:621,629 ifc = Itiming; } - // - // convert binary numbers from/to kernel - // + /* + * convert binary numbers from/to kernel + */ static uvlong uvorder = 0x0001020304050607ULL; static uchar* /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:678,686 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:680,688 return t+sizeof(long); } - // - // read ticks and local time in nanoseconds - // + /* + * read ticks and local time in nanoseconds + */ static int gettime(vlong *nsec, uvlong *ticks, uvlong *hz) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:766,772 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:768,775 uchar b[1+sizeof(vlong)+sizeof(long)], *p; if(debug) - fprint(2, "settime(now=%lld, hz=%llud, delta=%lld, period=%d)\n", now, hz, delta, n); + fprint(2, "settime(now=%lld, hz=%llud, delta=%lld, period=%d)\n", + now, hz, delta, n); if(impotent) return; switch(ifc){ /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:809,817 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:812,820 } } - // - // set priority high and wire process to a processor - // + /* + * set priority high and wire process to a processor + */ static void setpriority(void) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:831,851 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:834,853 close(fd); } - // convert to ntp timestamps + /* convert to ntp timestamps */ static void hnputts(void *p, vlong nsec) { uchar *a; - ulong tsh; - ulong tsl; + ulong tsh, tsl; a = p; - // zero is a special case + /* zero is a special case */ if(nsec == 0) return; - tsh = (nsec/SEC); + tsh = nsec/SEC; nsec -= tsh*SEC; tsl = (nsec<<32)/SEC; hnputl(a, tsh+EPOCHDIFF); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:852,858 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:854,860 hnputl(a+4, tsl); } - // convert from ntp timestamps + /* convert from ntp timestamps */ static vlong nhgetts(void *p) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:869,875 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:871,877 return nsec; } - // convert to ntp 32 bit fixed point + /* convert to ntp 32 bit fixed point */ static void hnputfp(void *p, vlong nsec) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:882,888 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:884,890 hnputl(a, fp); } - // convert from ntp fixed point to nanosecs + /* convert from ntp fixed point to nanosecs */ static vlong nhgetfp(void *p) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:896,902 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:898,904 return nsec; } - // get network address of the server + /* get network address of the server */ static void setrootid(char *d) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:904,910 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:906,912 int fd, n; char *p; - snprint(buf, sizeof(buf), "%s/remote", d); + snprint(buf, sizeof buf, "%s/remote", d); fd = open(buf, OREAD); if(fd < 0) return; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:941,950 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:943,952 *l = ns; } - // - // sntp client, we keep calling if the delay seems - // unusually high, i.e., 30% longer than avg. - // + /* + * sntp client, we keep calling if the delay seems + * unusually high, i.e., 30% longer than avg. + */ static int ntptimediff(NTPserver *ns) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:953,958 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:955,962 vlong dt, recvts, origts, xmitts, destts, x; char dir[64]; + notify(ding); + alarm(30*1000); /* don't wait forever if ns->name is unreachable */ fd = dial(netmkaddr(ns->name, "udp", "ntp"), 0, dir, 0); if(fd < 0){ syslog(0, logfile, "can't reach %s: %r", ns->name); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:959,965 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:963,968 return -1; } setrootid(dir); - notify(ding); memset(&ntpout, 0, sizeof(ntpout)); ntpout.mode = 3 | (3 << 3); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:974,992 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:977,995 continue; } - n = read(fd, &ntpin, sizeof(ntpin)); + n = read(fd, &ntpin, sizeof ntpin); alarm(0); gettime(&destts, 0, 0); if(n >= NTPSIZE){ close(fd); - // we got one, use it + /* we got one, use it */ recvts = nhgetts(ntpin.recvts); origts = nhgetts(ntpin.origts); xmitts = nhgetts(ntpin.xmitts); dt = ((recvts - origts) + (xmitts - destts))/2; - // save results + /* save results */ ns->rtt = ((destts - origts) - (xmitts - recvts))/2; ns->dt = dt; ns->stratum = ntpin.stratum; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:995,1006 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:998,1009 ns->rootdisp = nhgetfp(ntpin.rootdisp); if(debug) - fprint(2, "ntp %s stratum %d ntpdelay(%lld)\n", + fprint(2, "ntp %s stratum %d ntpdelay(%lld)\n", ns->name, ntpin.stratum, ns->rtt); return 0; } - // try again + /* try again */ sleep(250); } close(fd); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1020,1038 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1023,1039 seek(gpsfil, 0, 0); n = read(gpsfil, buf, sizeof buf - 1); if (n <= 0) - return(0LL); + return 0; buf[n] = 0; n = tokenize(buf, v, nelem(v)); - if(n != 4) - return(0LL); - if(strcmp(v[3], "A") != 0) - return(0LL); + if(n != 4 || strcmp(v[3], "A") != 0) + return 0; g = atoll(v[1]); l = atoll(v[2]); if(g-l > d) d = g-l; } - return(d); + return d; } static vlong /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1057,1065 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1058,1066 } if(ns == nil) - return 0LL; + return 0; - // save data for our server + /* save data for our server */ rootdisp = ns->rootdisp; rootdelay = ns->rootdelay; mydelay = ns->rtt; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1073,1081 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1074,1082 return ns->dt; } - // - // sample the utc file - // + /* + * sample the utc file + */ static vlong utcsample(void) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1087,1114 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1088,1114 seek(utcfil, 0, 0); n = read(utcfil, buf, sizeof buf - 1); if (n <= 0) - return(0LL); + return 0; buf[n] = 0; n = tokenize(buf, v, nelem(v)); if (strcmp(v[0], "0") == 0) - return(0LL); + return 0; if (n == 2) { gettime(&s, nil, nil); s -= atoll(v[1]); } lastutc = atoll(v[0]) + s; - return(lastutc); + return lastutc; } - // - // sntp server - // + /* + * sntp server + */ static int openlisten(char *net) { int fd, cfd; - char data[128]; - char devdir[40]; + char data[128], devdir[40]; sprint(data, "%s/udp!*!ntp", net); cfd = announce(data, devdir); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1128,1138 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1128,1137 static void ntpserver(char *servenet) { - int fd, n; - NTPpkt *ntp; - char buf[512]; - int vers, mode; + int fd, n, vers, mode; vlong recvts, x; + char buf[512]; + NTPpkt *ntp; fd = openlisten(servenet); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1152,1161 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1151,1160 break; } if (Rootid != nil) - memmove(rootid, Rootid, strlen(Rootid) > 4 ? 4 : strlen(Rootid)); + memmove(rootid, Rootid, strlen(Rootid) > 4? 4: strlen(Rootid)); for(;;){ - n = read(fd, buf, sizeof(buf)); + n = read(fd, buf, sizeof buf); gettime(&recvts, 0, 0); if(n < 0) return; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1183,1191 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1182,1190 } } - // - // get the current time from the file system - // + /* + * get the current time from the file system + */ static long fstime(void) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1201,1209 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1200,1208 return t; } - // - // get the current time from the real time clock - // + /* + * get the current time from the real time clock + */ static long rtctime(void) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1217,1238 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1216,1236 f = open("/dev/rtc", OREAD|OCEXEC); if(f < 0) break; - if(seek(f, 0, 0) < 0 || (i = read(f, b, sizeof(b))) < 0){ + if(seek(f, 0, 0) < 0 || (i = read(f, b, sizeof b)) < 0){ close(f); f = -1; - } else { + } else if(i != 0) break; - } } return strtoul(b, 0, 10)+gmtdelta; } - // - // Sample a clock. We wait for the clock to always - // be at the leading edge of a clock period. - // + /* + * Sample a clock. We wait for the clock to always + * be at the leading edge of a clock period. + */ static vlong sample(long (*get)(void)) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1254,1263 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1252,1261 return SEC*this - (end-start)/2; } - // - // the name of the frequency file has the method and possibly the - // server name encoded in it. - // + /* + * the name of the frequency file has the method and possibly the + * server name encoded in it. + */ static int openfreqfile(void) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1284,1293 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1282,1291 return fd; } - // - // the file contains the last known frequency and the - // number of seconds it was sampled over - // + /* + * the file contains the last known frequency and the + * number of seconds it was sampled over + */ static vlong readfreqfile(int fd, vlong ohz, vlong minhz, vlong maxhz) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1295,1301 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1293,1299 char buf[128]; vlong hz; - n = read(fd, buf, sizeof(buf)-1); + n = read(fd, buf, sizeof buf-1); if(n <= 0) return ohz; buf[n] = 0; /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1308,1316 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1306,1314 return hz; } - // - // remember hz and averaging period - // + /* + * remember hz and averaging period + */ static void writefreqfile(int fd, vlong hz, int secs, vlong diff) { /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1331,1340 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1329,1338 static uvlong vabs(vlong x) { - if(x < 0LL) - return (uvlong)-x; + if(x < 0) + return -x; else - return (uvlong)x; + return x; } static void /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1345,1351 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1343,1349 if(inbackground) return; - if(!debug) { + if(!debug) switch(rfork(RFPROC|RFFDG|RFNAMEG|RFNOTEG|RFNOWAIT)){ case -1: sysfatal("forking: %r"); /n/sourcesdump/2007/0412/plan9/sys/src/cmd/aux/timesync.c:1355,1361 - /n/sources/plan9/sys/src/cmd/aux/timesync.c:1353,1358 default: exits(0); } - } inbackground = 1; }