1 #include <u.h> 2 #include <libc.h> 3 #include <../boot/boot.h> 4 5 int 6 plumb(char *dir, char *dest, int *efd, char *here) 7 { 8 char buf[128]; 9 char name[128]; 10 int n; 11 12 sprint(name, "%s/clone", dir); 13 efd[0] = open(name, ORDWR); 14 if(efd[0] < 0) 15 return -1; 16 n = read(efd[0], buf, sizeof(buf)-1); 17 if(n < 0){ 18 close(efd[0]); 19 return -1; 20 } 21 buf[n] = 0; 22 sprint(name, "%s/%s/data", dir, buf); 23 if(here){ 24 sprint(buf, "announce %s", here); 25 if(sendmsg(efd[0], buf) < 0){ 26 close(efd[0]); 27 return -1; 28 } 29 } 30 sprint(buf, "connect %s", dest); 31 if(sendmsg(efd[0], buf) < 0){ 32 close(efd[0]); 33 return -1; 34 } 35 efd[1] = open(name, ORDWR); 36 if(efd[1] < 0){ 37 close(efd[0]); 38 return -1; 39 } 40 return efd[1]; 41 } 42 43 int 44 sendmsg(int fd, char *msg) 45 { 46 int n; 47 48 n = strlen(msg); 49 if(write(fd, msg, n) != n) 50 return -1; 51 return 0; 52 } 53 54 void 55 warning(char *s) 56 { 57 char buf[ERRLEN]; 58 59 errstr(buf); 60 fprint(2, "boot: %s: %s\n", s, buf); 61 } 62 63 void 64 fatal(char *s) 65 { 66 char buf[ERRLEN]; 67 68 errstr(buf); 69 fprint(2, "boot: %s: %s\n", s, buf); 70 exits(0); 71 } 72 73 int 74 readfile(char *name, char *buf, int len) 75 { 76 int f, n; 77 78 buf[0] = 0; 79 f = open(name, OREAD); 80 if(f < 0) 81 return -1; 82 n = read(f, buf, len-1); 83 if(n >= 0) 84 buf[n] = 0; 85 close(f); 86 return 0; 87 } 88 89 int 90 writefile(char *name, char *buf, int len) 91 { 92 int f, n; 93 94 f = open(name, OWRITE); 95 if(f < 0) 96 return -1; 97 n = write(f, buf, len); 98 close(f); 99 return (n != len) ? -1 : 0; 100 } 101 102 void 103 setenv(char *name, char *val) 104 { 105 int f; 106 char ename[2*NAMELEN]; 107 108 sprint(ename, "#e/%s", name); 109 f = create(ename, 1, 0666); 110 if(f < 0) 111 return; 112 write(f, val, strlen(val)); 113 close(f); 114 } 115 116 void 117 srvcreate(char *name, int fd) 118 { 119 char *srvname; 120 int f; 121 char buf[2*NAMELEN]; 122 123 srvname = strrchr(name, '/'); 124 if(srvname) 125 srvname++; 126 else 127 srvname = name; 128 129 sprint(buf, "#s/%s", srvname); 130 f = create(buf, 1, 0666); 131 if(f < 0) 132 fatal(buf); 133 sprint(buf, "%d", fd); 134 if(write(f, buf, strlen(buf)) != strlen(buf)) 135 fatal("write"); 136 close(f); 137 } 138 139 static int 140 catchint(void *a, char *note) 141 { 142 USED(a); 143 if(strcmp(note, "alarm") == 0) 144 return 1; 145 return 0; 146 } 147 148 int 149 outin(int timeout, char *prompt, char *def, int len) 150 { 151 int n; 152 char buf[256]; 153 154 if(timeout) { 155 atnotify(catchint, 1); 156 alarm(15*1000); 157 do{ 158 print("%s[%s]: ", prompt, *def ? def : "no default"); 159 n = read(0, buf, len); 160 alarm(15*1000); 161 }while(n == 0); 162 alarm(0); 163 atnotify(catchint, 0); 164 } 165 else { 166 do { 167 print("%s[%s]: ", prompt, *def ? def : "no default"); 168 n = read(0, buf, len); 169 }while(n == 0); 170 } 171 if(n < 0) 172 return 1; 173 if(n != 1){ 174 buf[n-1] = 0; 175 strcpy(def, buf); 176 } 177 return n; 178 } 179 180 /* 181 * get second word of the terminal environment variable. If it 182 * ends in "boot", get rid of that part. 183 */ 184 void 185 getconffile(char *conffile, char *terminal) 186 { 187 char *p, *q; 188 char *s; 189 int n; 190 191 s = conffile; 192 *conffile = 0; 193 p = terminal; 194 if((p = strchr(p, ' ')) == 0 || p[1] == ' ' || p[1] == 0) 195 return; 196 p++; 197 for(q = p; *q && *q != ' '; q++) 198 ; 199 while(p < q) 200 *conffile++ = *p++; 201 *conffile = 0; 202 203 /* dump a trailing boot */ 204 n = strlen(s); 205 if(n > 4 && strcmp(s + n - 4, "boot") == 0) 206 *(s+n-4) = 0; 207 } 208