Procházet zdrojové kódy

Improved the strategy to set time.

If we can't obtain the time from the timeserver, we try it with the rtc.

Change-Id: Ic72b5264e1783c02e88f4766b65280330fa0c4c4
rafael před 8 roky
rodič
revize
48382ed53a
1 změnil soubory, kde provedl 33 přidání a 19 odebrání
  1. 33 19
      sys/src/9/boot/settime.c

+ 33 - 19
sys/src/9/boot/settime.c

@@ -17,10 +17,29 @@ static int32_t lusertime(char*);
 
 char *timeserver = "#s/boot";
 
+static int
+setlocaltime(char* timebuf, int s){
+	int n, f, t;
+	t=0;
+	f = open("#r/rtc", ORDWR);
+	if(f >= 0){
+		if((n = read(f, timebuf, s-1)) > 0){
+			timebuf[n] = '\0';
+			t = 1;
+		}
+		close(f);
+	}else do{
+		strcpy(timebuf, "yymmddhhmm[ss]");
+		outin("\ndate/time ", timebuf, s);
+	}while((t=lusertime(timebuf)) <= 0);
+	return t;
+}
+
+
 void
 settime(int islocal, int afd, char *rp)
 {
-	int n, f;
+	int f;
 	int timeset;
 	Dir dir[2];
 	char timebuf[64];
@@ -31,17 +50,7 @@ settime(int islocal, int afd, char *rp)
 		/*
 		 *  set the time from the real time clock
 		 */
-		f = open("#r/rtc", ORDWR);
-		if(f >= 0){
-			if((n = read(f, timebuf, sizeof(timebuf)-1)) > 0){
-				timebuf[n] = '\0';
-				timeset = 1;
-			}
-			close(f);
-		}else do{
-			strcpy(timebuf, "yymmddhhmm[ss]");
-			outin("\ndate/time ", timebuf, sizeof(timebuf));
-		}while((timeset=lusertime(timebuf)) <= 0);
+		timeset=setlocaltime(timebuf, sizeof(timebuf));
 	}
 	if(timeset == 0){
 		/*
@@ -53,16 +62,21 @@ settime(int islocal, int afd, char *rp)
 		if(mount(f, afd, "/tmp", MREPL, rp, 'M') < 0){
 			warning("settime mount");
 			close(f);
-			return;
+			if((!islocal) && (setlocaltime(timebuf, sizeof(timebuf)) == 0))
+				return;
+		} else {
+			close(f);
+			if(stat("/tmp", statbuf, sizeof statbuf) < 0)
+				fatal("stat");
+			convM2D(statbuf, sizeof statbuf, &dir[0], (char*)&dir[1]);
+			sprint(timebuf, "%ld", dir[0].atime);
+			unmount(0, "/tmp");
 		}
-		close(f);
-		if(stat("/tmp", statbuf, sizeof statbuf) < 0)
-			fatal("stat");
-		convM2D(statbuf, sizeof statbuf, &dir[0], (char*)&dir[1]);
-		sprint(timebuf, "%ld", dir[0].atime);
-		unmount(0, "/tmp");
 	}
 
+	if((!islocal) && (strcmp(timebuf,"0")==0))
+		setlocaltime(timebuf, sizeof(timebuf));
+
 	f = open("#c/time", OWRITE);
 	if(write(f, timebuf, strlen(timebuf)) < 0)
 		warning("can't set #c/time");