ref: 9883fb9af749887da25d9ed95699064b7a10cc00
parent: 51489f979a4b1a0df534f9b8d461314c8ad86483
author: B. Wilson <x@wilsonb.com>
date: Sat Jun 21 08:13:15 EDT 2025
Miscellaneous cleanup
--- a/ridefs.c
+++ b/ridefs.c
@@ -15,20 +15,15 @@
QLock;
char *addr;
- ulong umask;
/* internal use */
- int ridone; /* ride initialized */
Req *req; /* i/o req */
int pid; /* pid */
char *r; /* response buffer */
int rn; /* length */
int roff; /* offset */
- char *user;
- char *rinfo;
+ char *qinfo;
char *qctl;
- long time0;
- int id;
int fd; /* data */
int cfd; /* ctl */
};
@@ -40,7 +35,7 @@
Qclient,
Qctl,
Qio,
- Qrinfo,
+ Qinfo,
QCOUNT
};
@@ -51,7 +46,7 @@
nil,
"ctl",
"io",
- "rinfo",
+ "info",
};
struct Rfid {
@@ -67,7 +62,6 @@
static char *defport;
static char *user;
static char *qrctl;
-static ulong umask;
static long bufsz;
static long time0;
static uint nclients;
@@ -110,15 +104,13 @@
int
genqrctl(void){
- sprintf(qrctl,
+ snprintf(qrctl, bufsz,
"version %i\n"
"bufsz %u\n"
"nclients %u\n"
"debug %i\n"
- "umask 0%hlo\n"
"defport %s\n",
- VERSION, bufsz, nclients,
- debug, umask, defport);
+ VERSION, bufsz, nclients, debug,defport);
return strlen(qrctl);
}
@@ -128,11 +120,10 @@
Client *c;
c = clientref(client);
- sprintf(c->qctl,
+ snprintf(c->qctl, bufsz,
"%i\n"
- "connect %s\n"
- "umask 0%hlo\n",
- client, c->addr, c->umask);
+ "connect %s\n",
+ client, c->addr);
return strlen(c->qctl);
}
@@ -150,13 +141,10 @@
c = &cpool[i];
incref(c);
- c->id = i;
- c->umask = umask;
- c->user = estrdup(getuser());
- c->time0 = time(0);
- c->qctl = ecalloc(bufsz);
c->r = ecalloc(bufsz);
c->rn = -1;
+ c->qctl = ecalloc(bufsz);
+ c->qinfo = ecalloc(bufsz);
genqctl(i);
return i;
@@ -172,9 +160,8 @@
if(c->fd) close(c->fd);
if(c->cfd) close(c->cfd);
- if(c->user) free(c->user);
if(c->qctl) free(c->qctl);
- if(c->rinfo) free(c->rinfo);
+ if(c->qinfo) free(c->qinfo);
if(c->r) free(c->r);
memset(c, 0, sizeof(*c));
@@ -200,7 +187,7 @@
int len;
len = n+8;
- r = ecalloc(len+8);
+ r = ecalloc(len);
r[0] = 24>>len & 0xff;
r[1] = 16>>len & 0xff;
r[2] = 8>>len & 0xff;
@@ -216,82 +203,75 @@
}
long
-readmsg(int fd, void *pld, long n){
+readmsg(int fd, void **pld){
int len, e;
char buf[9];
if(0 > (e = readn(fd, buf, 8)))
return e;
+
buf[9] = '\0';
if(0 != (e = strcmp(&buf[4], "RIDE")))
return e;
+
len = -8 + (buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);
- if(len > n)
- return n-len;
- if(0 > (e = readn(fd, pld, len)))
+ *pld = ecalloc(len+1); /* ensure trailing null byte */
+ if(0 > (e = readn(fd, *pld, len))){
+ free(*pld);
return e;
+ }
return len;
}
char *
-rideinit(int client){
- int fd;
- char *addr, *buf, *s, *e;
- Client *c;
+rideinit(char *addr, int *fd, int *cfd, char **info){
+ int i;
+ char *b;
JSON *j;
JSONEl *d;
- c = clientref(client);
- addr = netmkaddr(c->addr, "tcp", defport);
+ b = netmkaddr(addr, "tcp", defport);
if(debug)
fprintf(stderr, "dialing %s\n", addr);
- if((fd = dial(addr, nil, nil, &c->cfd)) < 0)
+ if((*fd = dial(b, nil, nil, cfd)) < 0)
return "failed to dial";
- c = clientref(client);
- c->fd = fd;
- buf = ecalloc(bufsz);
- if(0 > readmsg(fd, buf, bufsz))
+ if(0 > readmsg(*fd, &b))
return "failed to read handshake";
- if(0 != strcmp(buf, "SupportedProtocols=2"))
+ if(0 != strcmp(b, "SupportedProtocols=2"))
return "unrecognized protocol";
- free(buf);
+ free(b);
- buf = "UsingProtocol=2";
- if(0 > writemsg(fd, buf, strlen(buf)))
+ b = "UsingProtocol=2";
+ if(0 > writemsg(*fd, b, strlen(b)))
return "failed to write handshake";
- buf = "[\"Identify\",{\"apiVersion\":1,\"identity\":1}]";
- if(0 > writemsg(fd, buf, strlen(buf)))
+ b = "[\"Identify\",{\"apiVersion\":1,\"identity\":1}]";
+ if(0 > writemsg(*fd, b, strlen(b)))
return "failed to send identification message";
- buf = ecalloc(bufsz);
- if(0 > readmsg(fd, buf, bufsz))
+ if(0 > readmsg(*fd, &b))
return "failed to receive identification message";
- j = jsonparse(buf);
- free(buf);
+ if(nil == (j = jsonparse(b)))
+ return "failed to parse identification message";
+ free(b);
if(j == nil || j->t != JSONArray || nil == j->first)
return "unrecognized reply";
- if(nil == (s = jsonstr(j->first->val)) || 0 != strcmp(s, "ReplyIdentify"))
+ if(nil == (b = jsonstr(j->first->val)) || 0 != strcmp(b, "ReplyIdentify"))
return "unexpected identification reply";
if(nil == (d = j->first->next) || d->val->t != JSONObject)
return "malformed identification reply";
- s = c->rinfo = ecalloc(bufsz);
- buf = ecalloc(bufsz);
- e = s + bufsz-1;
- for(d = d->val->first; d != nil; d = d->next){
+ for(i = 0, d = d->val->first; d != nil; d = d->next){
switch(d->val->t){
case JSONBool:
case JSONNumber:
- sprintf(buf, "%s %i\n", d->name, d->val->n); break;
+ i += snprintf(*info+i, bufsz-i, "%s %i\n", d->name, d->val->n); break;
case JSONString:
- sprintf(buf, "%s %s\n", d->name, d->val->s); break;
+ i += snprintf(*info+i, bufsz-i, "%s %s\n", d->name, d->val->s); break;
}
- s = strecpy(s, e, buf);
}
- free(buf);
jsonfree(j);
return nil;
@@ -306,8 +286,6 @@
for(s = strtok(b, sep); s != nil; s = strtok(nil, sep)){
if(strcmp(s, "bufsz") == 0)
bufsz = strtoul(strtok(nil, sep), nil, 0);
- if(strcmp(s, "umask") == 0)
- umask = strtoul(strtok(nil, sep), nil, 0);
if(strcmp(s, "nclients") == 0)
nclients = strtoul(strtok(nil, sep), nil, 0);
if(strcmp(s, "debug") == 0)
@@ -332,8 +310,6 @@
for(s = strtok(b, sep); s != nil; s = strtok(nil, sep)){
if(strcmp(s, "connect") == 0)
c->addr = estrdup(strtok(nil, sep));
- if(strcmp(s, "umask") == 0)
- c->umask = strtoul(strtok(nil, sep), nil, 0);
}
genqctl(client);
@@ -348,11 +324,15 @@
memset(d, 0, sizeof(*d));
mkqid(&d->qid, kind, client);
- d->mode = 0444 & umask;
+ d->mode = 0444;
+ d->atime = d->mtime = time0;
+ d->uid = estrdup(user);
+ d->gid = estrdup(user);
+ d->muid = estrdup(user);
if(nil != (nm = nametab[kind]))
d->name = estrdup(nm);
if(kind == Qclient){
- nm = ecalloc(bufsz);
+ nm = ecalloc(client+2); /* client+1 >= log(client) */
sprintf(nm, "%i", client);
d->name = estrdup(nm);
}
@@ -361,33 +341,16 @@
c = clientref(client);
switch(kind){
- case Qrctl: d->mode = 0666 & umask; break;
+ case Qrctl: d->mode = 0666; break;
case Qctl:
- case Qio: d->mode = 0666 & c->umask; break;
+ case Qio: d->mode = 0666; break;
}
switch(kind){
- case Qroot:
- case Qrctl:
- case Qclone:
- d->atime = d->mtime = time0;
- d->uid = estrdup(user);
- d->gid = estrdup(user);
- d->muid = estrdup(user);
- break;
- default:
- d->atime = d->mtime = c->time0;
- d->uid = estrdup(c->user);
- d->gid = estrdup(c->user);
- d->muid = estrdup(c->user);
- break;
- }
-
- switch(kind){
case Qrctl: d->length = strlen(qrctl); break;
case Qctl: d->length = strlen(c->qctl); break;
case Qio: d->length = 0; break;
- case Qrinfo: d->length = c->rinfo == nil ? 0 : strlen(c->rinfo); break;
+ case Qinfo: d->length = strlen(c->qinfo); break;
}
}
@@ -497,13 +460,12 @@
switch(pid = rfork(RFPROC|RFNOWAIT|RFMEM)){
case 0:
qlock(c);
- if(c->ridone)
+ if(c->fd)
goto end;
- err = rideinit(f->client);
+ err = rideinit(c->addr, &c->fd, &c->cfd, &c->qinfo);
c->pid = 0;
c->req = nil;
- c->ridone = 1;
end:
respond(r, err);
@@ -539,7 +501,7 @@
case Qclone: err = "read prohibited"; break;
case Qclient: dirread9p(r, genqclient, (void*)f->client); break;
case Qctl: buf = estrdup(c->qctl); break;
- case Qrinfo: buf = estrdup(c->rinfo); break;
+ case Qinfo: buf = estrdup(c->qinfo); break;
case Qio:
switch(pid = rfork(RFPROC|RFNOWAIT|RFMEM)){
case 0:
@@ -547,7 +509,8 @@
off = r->ifcall.offset - c->roff;
if(c->rn < 0){
- c->rn = readmsg(c->fd, c->r, bufsz);
+ free(c->r);
+ c->rn = readmsg(c->fd, &c->r);
c->roff = r->ifcall.offset;
off = 0;
}
@@ -755,7 +718,6 @@
user = getuser();
mtpt = "/mnt/ride";
defport = "4502";
- umask = 0755;
bufsz = 4096;
time0 = time(0);
nclients = 256;
--- a/test
+++ b/test
@@ -33,12 +33,12 @@
cat ctl >/dev/null ||
fail 'Could not read client ctl'
- x=`{grep umask ctl}
- echo 'umask 0777' >ctl ||
+ x=`{grep connect ctl}
+ echo 'connect 127.0.0.1' >ctl ||
fail 'Could not write client ctl'
- y=`{grep umask ctl}
- ~ $y(2) 0777 ||
+ y=`{grep connect ctl}
+ ~ $y(2) 127.0.0.1 ||
fail 'Written and read client ctl values differ'
echo umask $x(2) >ctl
@@ -58,7 +58,7 @@
fail 'Could not connect to server'
<>io >[1=0] {
- ~ `{wc -c rinfo} 0 &&
+ ~ `{wc -c info} 0 &&
fail 'Did not receive RIDE info'
cat >/dev/null ||
--
⑨