hard code list of plan 9 services in case they are not in /etc/services

This commit is contained in:
rsc 2005-02-11 16:58:23 +00:00
parent 26a5fd5725
commit b589fce2fb
8 changed files with 300 additions and 8 deletions

View file

@ -10,6 +10,24 @@
static char *nets[] = { "tcp", "udp", nil };
#define CLASS(p) ((*(uchar*)(p))>>6)
static struct {
char *net;
char *service;
int port;
} porttbl[] = {
"tcp", "9fs", 564,
"tcp", "whoami", 565,
"tcp", "guard", 566,
"tcp", "ticket", 567,
"tcp", "exportfs", 17007,
"tcp", "rexexec", 17009,
"tcp", "ncpu", 17010,
"tcp", "cpu", 17013,
"tcp", "venti", 17034,
"tcp", "wiki", 17035,
"tcp", "secstore", 5356,
};
static int
parseip(char *host, u32int *pip)
{
@ -103,7 +121,7 @@ p9dialparse(char *addr, char **pnet, char **punix, u32int *phost, int *pport)
if(strcmp(net, "unix") == 0)
goto Unix;
if(strcmp(net, "tcp")!=0 && strcmp(net, "udp")!=0){
if(strcmp(net, "tcp")!=0 && strcmp(net, "udp")!=0 && strcmp(net, "net") != 0){
werrstr("bad network %s!%s!%s", net, host, port);
return -1;
}
@ -129,7 +147,19 @@ p9dialparse(char *addr, char **pnet, char **punix, u32int *phost, int *pport)
return 0;
}
}
werrstr("unknown service %s", port);
}
for(i=0; i<nelem(porttbl); i++){
if(strcmp(net, "net") == 0 || strcmp(porttbl[i].net, net) == 0)
if(strcmp(porttbl[i].service, port) == 0){
*pnet = porttbl[i].net;
*pport = porttbl[i].port;
return 0;
}
}
if(strcmp(net, "net") == 0){
werrstr("unknown service net!*!%s", port);
return -1;
}
@ -149,6 +179,6 @@ p9dialparse(char *addr, char **pnet, char **punix, u32int *phost, int *pport)
*pport = ntohs(se->s_port);
return 0;
}
werrstr("unknown service %s", port);
werrstr("unknown service %s!*!%s", net, port);
return -1;
}

View file

@ -43,6 +43,10 @@ p9create(char *path, int mode, ulong perm)
umode |= O_EXCL;
mode &= ~OEXCL;
}
if(mode&OAPPEND){
umode |= O_APPEND;
mode &= ~OAPPEND;
}
if(mode){
werrstr("unsupported mode in create");
goto out;

View file

@ -216,15 +216,26 @@ dumpsome(char *ans, char *e, char *buf, long count)
if(count > DUMPL)
count = DUMPL;
for(i=0; i<count && printable; i++)
if((buf[i]<32 && buf[i] !='\n' && buf[i] !='\t') || (uchar)buf[i]>127)
if((buf[i]!=0 && buf[i]<32) || (uchar)buf[i]>127)
printable = 0;
p = ans;
*p++ = '\'';
if(printable){
if(count > e-p-2)
count = e-p-2;
memmove(p, buf, count);
p += count;
if(2*count > e-p-2)
count = (e-p-2)/2;
for(i=0; i<count; i++){
if(buf[i] == 0){
*p++ = '\\';
*p++ = '0';
}else if(buf[i] == '\t'){
*p++ = '\\';
*p++ = 't';
}else if(buf[i] == '\n'){
*p++ = '\\';
*p++ = 'n';
}else
*p++ = buf[i];
}
}else{
if(2*count > e-p-2)
count = (e-p-2)/2;
@ -237,5 +248,6 @@ dumpsome(char *ans, char *e, char *buf, long count)
}
*p++ = '\'';
*p = 0;
assert(p < e);
return p - ans;
}

View file

@ -117,6 +117,7 @@ LIB9OFILES=\
nan.$O\
needsrcquote.$O\
needstack.$O\
netcrypt.$O\
netmkaddr.$O\
notify.$O\
nrand.$O\
@ -130,6 +131,7 @@ LIB9OFILES=\
quote.$O\
rand.$O\
read9pmsg.$O\
readcons.$O\
readn.$O\
rfork.$O\
searchpath.$O\
@ -139,6 +141,7 @@ LIB9OFILES=\
strdup.$O\
strecpy.$O\
sysfatal.$O\
syslog.$O\
sysname.$O\
time.$O\
tokenize.$O\

18
src/lib9/netcrypt.c Normal file
View file

@ -0,0 +1,18 @@
#include <u.h>
#include <libc.h>
#include <auth.h>
int
netcrypt(void *key, void *chal)
{
uchar buf[8], *p;
strncpy((char*)buf, chal, 7);
buf[7] = '\0';
for(p = buf; *p && *p != '\n'; p++)
;
*p = '\0';
encrypt(key, buf, 8);
sprint(chal, "%.2ux%.2ux%.2ux%.2ux", buf[0], buf[1], buf[2], buf[3]);
return 1;
}

View file

@ -32,6 +32,10 @@ p9open(char *name, int mode)
umode |= O_NONBLOCK;
mode ^= ONONBLOCK;
}
if(mode&OAPPEND){
umode |= O_APPEND;
mode ^= OAPPEND;
}
if(mode){
werrstr("mode 0x%x not supported", mode);
return -1;

102
src/lib9/readcons.c Normal file
View file

@ -0,0 +1,102 @@
#include <u.h>
#define NOPLAN9DEFINES
#include <libc.h>
#include <termios.h>
#include <sys/termios.h>
static int
rawx(int fd, int echoing)
{
int was;
static struct termios ttmode;
if(echoing == -1)
return -1;
if(tcgetattr(fd, &ttmode) < 0)
return -1;
was = (ttmode.c_lflag&(ECHO|ICANON));
ttmode.c_lflag &= ~(ECHO|ICANON);
ttmode.c_lflag |= echoing;
if(tcsetattr(fd, TCSANOW, &ttmode) < 0)
return -1;
return was;
}
char*
readcons(char *prompt, char *def, int secret)
{
int fd, n, raw;
char line[10];
char *s, *t;
int l;
if((fd = open("/dev/tty", ORDWR)) < 0)
return nil;
raw = -1;
if(secret){
raw = rawx(fd, 0);
if(raw == -1)
return nil;
}
if(def)
fprint(fd, "%s[%s]: ", prompt, def);
else
fprint(fd, "%s: ", prompt);
s = strdup("");
if(s == nil)
return nil;
for(;;){
n = read(fd, line, 1);
if(n < 0){
Error:
if(secret){
rawx(fd, raw);
write(fd, "\n", 1);
}
close(fd);
free(s);
return nil;
}
if(n > 0 && line[0] == 0x7F)
goto Error;
if(n == 0 || line[0] == 0x04 || line[0] == '\n' || line[0] == '\r'){
if(secret){
rawx(fd, raw);
write(fd, "\n", 1);
}
close(fd);
if(*s == 0 && def){
free(s);
s = strdup(def);
}
return s;
}
if(line[0] == '\b'){
if(strlen(s) > 0)
s[strlen(s)-1] = 0;
}else if(line[0] == 0x15){ /* ^U: line kill */
if(def != nil)
fprint(fd, "\n%s[%s]: ", prompt, def);
else
fprint(fd, "\n%s: ", prompt);
s[0] = 0;
}else{
l = strlen(s);
t = malloc(l+2);
if(t)
memmove(t, s, l);
memset(s, 'X', l);
free(s);
if(t == nil)
return nil;
t[l] = line[0];
t[l+1] = 0;
s = t;
}
}
}

119
src/lib9/syslog.c Normal file
View file

@ -0,0 +1,119 @@
#include <u.h>
#include <libc.h>
static struct
{
int fd;
int consfd;
char *name;
Dir *d;
Dir *consd;
Lock lk;
} sl =
{
-1, -1,
};
static void
_syslogopen(void)
{
char buf[1024], *p;
if(sl.fd >= 0)
close(sl.fd);
snprint(buf, sizeof(buf), "#9/log/%s", sl.name);
p = unsharp(buf);
sl.fd = open(p, OWRITE|OCEXEC|OAPPEND);
free(p);
}
/*
* Print
* sysname: time: mesg
* on /sys/log/logname.
* If cons or log file can't be opened, print on the system console, too.
*/
void
syslog(int cons, char *logname, char *fmt, ...)
{
char buf[1024];
char *ctim, *p;
va_list arg;
int n;
Dir *d;
char err[ERRMAX];
err[0] = '\0';
errstr(err, sizeof err);
lock(&sl.lk);
/*
* paranoia makes us stat to make sure a fork+close
* hasn't broken our fd's
*/
d = dirfstat(sl.fd);
if(sl.fd < 0
|| sl.name == nil
|| strcmp(sl.name, logname)!=0
|| sl.d == nil
|| d == nil
|| d->dev != sl.d->dev
|| d->type != sl.d->type
|| d->qid.path != sl.d->qid.path){
free(sl.name);
sl.name = strdup(logname);
if(sl.name == nil)
cons = 1;
else{
_syslogopen();
if(sl.fd < 0)
cons = 1;
free(sl.d);
sl.d = d;
d = nil; /* don't free it */
}
}
free(d);
if(cons){
d = dirfstat(sl.consfd);
if(sl.consfd < 0
|| d == nil
|| sl.consd == nil
|| d->dev != sl.consd->dev
|| d->type != sl.consd->type
|| d->qid.path != sl.consd->qid.path){
sl.consfd = open("/dev/tty", OWRITE|OCEXEC);
free(sl.consd);
sl.consd = d;
d = nil; /* don't free it */
}
free(d);
}
if(fmt == nil){
unlock(&sl.lk);
return;
}
ctim = ctime(time(0));
werrstr(err);
p = buf + snprint(buf, sizeof(buf)-1, "%s ", sysname());
strncpy(p, ctim+4, 15);
p += 15;
*p++ = ' ';
va_start(arg, fmt);
p = vseprint(p, buf+sizeof(buf)-1, fmt, arg);
va_end(arg);
*p++ = '\n';
n = p - buf;
if(sl.fd >= 0){
seek(sl.fd, 0, 2);
write(sl.fd, buf, n);
}
if(cons && sl.consfd >=0)
write(sl.consfd, buf, n);
unlock(&sl.lk);
}