xref: /plan9/sys/src/9/boot/aux.c (revision 38ebe9496608d2f0e2b0fa51d807d7e59b654781)
1 #include <u.h>
2 #include <libc.h>
3 #include <../boot/boot.h>
4 
5 /*
6 int
7 plumb(char *dir, char *dest, int *efd, char *here)
8 {
9 	char buf[128];
10 	char name[128];
11 	int n;
12 
13 	sprint(name, "%s/clone", dir);
14 	efd[0] = open(name, ORDWR);
15 	if(efd[0] < 0)
16 		return -1;
17 	n = read(efd[0], buf, sizeof(buf)-1);
18 	if(n < 0){
19 		close(efd[0]);
20 		return -1;
21 	}
22 	buf[n] = 0;
23 	sprint(name, "%s/%s/data", dir, buf);
24 	if(here){
25 		sprint(buf, "announce %s", here);
26 		if(sendmsg(efd[0], buf) < 0){
27 			close(efd[0]);
28 			return -1;
29 		}
30 	}
31 	sprint(buf, "connect %s", dest);
32 	if(sendmsg(efd[0], buf) < 0){
33 		close(efd[0]);
34 		return -1;
35 	}
36 	efd[1] = open(name, ORDWR);
37 	if(efd[1] < 0){
38 		close(efd[0]);
39 		return -1;
40 	}
41 	return efd[1];
42 }
43 
44 int
45 sendmsg(int fd, char *msg)
46 {
47 	int n;
48 
49 	n = strlen(msg);
50 	if(write(fd, msg, n) != n)
51 		return -1;
52 	return 0;
53 }
54  */
55 
56 void
warning(char * s)57 warning(char *s)
58 {
59 	char buf[ERRMAX];
60 
61 	buf[0] = '\0';
62 	errstr(buf, sizeof buf);
63 	fprint(2, "boot: %s: %s\n", s, buf);
64 }
65 
66 void
fatal(char * s)67 fatal(char *s)
68 {
69 	char *msg;
70 	char buf[ERRMAX];
71 
72 	buf[0] = '\0';
73 	errstr(buf, sizeof buf);
74 	msg = smprint("%s: %s", s, buf);
75 	fprint(2, "boot: %s\n", msg);
76 	exits(msg);			/* this will trigger a panic */
77 }
78 
79 int
readfile(char * name,char * buf,int len)80 readfile(char *name, char *buf, int len)
81 {
82 	int f, n;
83 
84 	buf[0] = 0;
85 	f = open(name, OREAD);
86 	if(f < 0)
87 		return -1;
88 	n = read(f, buf, len-1);
89 	if(n >= 0)
90 		buf[n] = 0;
91 	close(f);
92 	return 0;
93 }
94 
95 int
writefile(char * name,char * buf,int len)96 writefile(char *name, char *buf, int len)
97 {
98 	int f, n;
99 
100 	f = open(name, OWRITE);
101 	if(f < 0)
102 		return -1;
103 	n = write(f, buf, len);
104 	close(f);
105 	return (n != len) ? -1 : 0;
106 }
107 
108 void
setenv(char * name,char * val)109 setenv(char *name, char *val)
110 {
111 	int f;
112 	char ename[64];
113 
114 	snprint(ename, sizeof ename, "#e/%s", name);
115 	f = create(ename, 1, 0666);
116 	if(f < 0){
117 		fprint(2, "create %s: %r\n", ename);
118 		return;
119 	}
120 	write(f, val, strlen(val));
121 	close(f);
122 }
123 
124 void
srvcreate(char * name,int fd)125 srvcreate(char *name, int fd)
126 {
127 	char *srvname;
128 	int f;
129 	char buf[64];
130 
131 	srvname = strrchr(name, '/');
132 	if(srvname)
133 		srvname++;
134 	else
135 		srvname = name;
136 
137 	snprint(buf, sizeof buf, "#s/%s", srvname);
138 	f = create(buf, 1, 0666);
139 	if(f < 0)
140 		fatal(buf);
141 	sprint(buf, "%d", fd);
142 	if(write(f, buf, strlen(buf)) != strlen(buf))
143 		fatal("write");
144 	close(f);
145 }
146 
147 void
catchint(void * a,char * note)148 catchint(void *a, char *note)
149 {
150 	USED(a);
151 	if(strcmp(note, "alarm") == 0)
152 		noted(NCONT);
153 	noted(NDFLT);
154 }
155 
156 int
outin(char * prompt,char * def,int len)157 outin(char *prompt, char *def, int len)
158 {
159 	int n;
160 	char buf[256];
161 
162 	if(len >= sizeof buf)
163 		len = sizeof(buf)-1;
164 
165 	if(cpuflag){
166 		notify(catchint);
167 		alarm(15*1000);
168 	}
169 	print("%s[%s]: ", prompt, *def ? def : "no default");
170 	memset(buf, 0, sizeof buf);
171 	n = read(0, buf, len);
172 	if(cpuflag){
173 		alarm(0);
174 		notify(0);
175 	}
176 
177 	if(n < 0)
178 		return 1;
179 	if(n > 1){
180 		buf[n-1] = 0;
181 		strcpy(def, buf);
182 	}
183 	return n;
184 }
185