libc: introduce <envvars.h>

The new header envvars.h contains the names of commonly used
environment variables, such as $user, $ifs, $path and so on.

These defines are useful for Jehanne core applications that
use them to comunicate some values.

They are not strictly required, but having such defines we
can easily change the naming convention (from lowercase to uppercase).
This commit is contained in:
Giacomo Tesio 2017-10-20 00:55:05 +02:00
parent 65d37f432f
commit fab9c1d80f
47 changed files with 144 additions and 69 deletions

44
sys/include/envvars.h Normal file
View File

@ -0,0 +1,44 @@
/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* Jehanne is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
/* This file defines the names of a few environment variables shared
* among the userspace tools provided by Jehanne.
*
* It must not provide defines for variables used by commonly installed
* softwares, just the one used by the core tools, the one required
* to boot a minimal system.
*/
#define ENV_APID "apid"
#define ENV_USER "user"
#define ENV_HOME "home"
#define ENV_IFS "ifs"
#define ENV_PATH "path"
#define ENV_PID "pid"
#define ENV_PROMPT "prompt"
#define ENV_STATUS "status"
#define ENV_CDPATH "cdpath"
#define ENV_CPUTYPE "cputype"
#define ENV_SERVICE "service"
#define ENV_SYSNAME "sysname"
#define ENV_OBJTYPE "objtype"
#define ENV_WSYS "wsys"
extern char* jehanne_getenv(const char*);
extern int jehanne_putenv(const char*, const char*);

View File

@ -325,7 +325,6 @@ extern int jehanne_encodefmt(Fmt*);
extern void jehanne_exits(const char*) __attribute__ ((noreturn));
extern double jehanne_frexp(double, int*);
extern uintptr_t jehanne_getcallerpc(void);
extern char* jehanne_getenv(const char*);
extern int jehanne_getfields(char*, char**, int, int, const char*);
extern int jehanne_gettokens(char *, char **, int, const char *);
extern char* jehanne_getuser(void);
@ -344,7 +343,6 @@ extern void jehanne_perror(const char*);
extern int jehanne_pipe(int pipes[2]);
extern int jehanne_postnote(int, int, const char *);
extern double jehanne_pow10(int);
extern int jehanne_putenv(const char*, const char*);
extern void jehanne_qsort(void*, long, int,
int (*)(const void*, const void*));
extern void* jehanne_bsearch(const void* key, const void* base, size_t nmemb, size_t size,

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
@ -124,9 +125,9 @@ threadmain(int argc, char *argv[])
fontnames[1] = estrdup(fontnames[1]);
quotefmtinstall();
cputype = getenv("cputype");
objtype = getenv("objtype");
home = getenv("home");
cputype = getenv(ENV_CPUTYPE);
objtype = getenv(ENV_OBJTYPE);
home = getenv(ENV_HOME);
p = getenv("tabstop");
if(p != nil){
maxtab = strtoul(p, nil, 0);

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include <thread.h>
#include <9P2000.h>
@ -61,7 +62,7 @@ threadmain(int argc, char *argv[])
av = emalloc(3*sizeof(char*));
av[0] = "rc";
av[1] = "-i";
name = getenv("sysname");
name = getenv(ENV_SYSNAME);
}else{
av = argv;
name = utfrrune(av[0], '/');

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>

View File

@ -63,7 +63,7 @@ runas(char *user, char *cmd)
{
if(becomeuser(user) < 0)
sysfatal("can't change uid for %s: %r", user);
putenv("service", "rx");
putenv(ENV_SERVICE, "rx");
execl("/bin/rc", "rc", "-lc", cmd, nil);
sysfatal("exec /bin/rc: %r");
}

View File

@ -568,7 +568,7 @@ rexec(User *user, Job *j)
open("/dev/null", OWRITE);
if(strcmp(j->host, "local") == 0){
putenv("service", "rx");
putenv(ENV_SERVICE, "rx");
execl("/bin/rc", "rc", "-lc", buf, nil);
} else {
execl("/bin/rx", "rx", j->host, buf, nil);

View File

@ -1,3 +1,4 @@
#include <envvars.h>
#include "dat.h"
//static char secstore[100]; /* server name */ // NOT USED
@ -712,7 +713,7 @@ promptforhostowner(void)
char owner[64], *p;
/* hack for bitsy; can't prompt during boot */
if(p = getenv("user")){
if(p = getenv(ENV_USER)){
writehostowner(p);
free(p);
return;

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <auth.h>
#include <authsrv.h>
#include <bio.h>
@ -70,10 +71,12 @@ void
setenv(char *var, char *val)
{
int fd;
char buf[128+4];
fd = ocreate(var, OWRITE, 0644);
snprint(buf, sizeof(buf), "#e/%s", var);
fd = ocreate(buf, OWRITE, 0644);
if(fd < 0)
print("init: can't open %s\n", var);
print("init: can't open %s\n", buf);
else{
fprint(fd, val);
close(fd);
@ -126,7 +129,7 @@ getauthdom(void)
if(authdom != nil)
return authdom;
sysname = getenv("sysname");
sysname = getenv(ENV_SYSNAME);
if(sysname == nil)
return strdup("cs.bell-labs.com");
@ -209,7 +212,7 @@ main(int argc, char *argv[])
rfork(RFENVG|RFNAMEG);
service = getenv("service");
service = getenv(ENV_SERVICE);
if(strcmp(service, "cpu") == 0)
fprint(2, "login: warning: running on a cpu server!\n");
if(argc != 1){
@ -239,24 +242,23 @@ main(int argc, char *argv[])
mountfactotum(srvname);
/* set up a new environment */
cputype = getenv("cputype");
sysname = getenv("sysname");
cputype = getenv(ENV_CPUTYPE);
sysname = getenv(ENV_SYSNAME);
tz = getenv("timezone");
rfork(RFCENVG);
setenv("#e/service", "con");
setenv("#e/user", user);
setenv(ENV_SERVICE, "con");
setenv(ENV_USER, user);
snprint(home, sizeof(home), "/usr/%s", user);
setenv("#e/home", home);
setenv("#e/cputype", cputype);
setenv("#e/objtype", cputype);
setenv(ENV_HOME, home);
setenv(ENV_CPUTYPE, cputype);
setenv(ENV_OBJTYPE, cputype);
if(sysname != nil)
setenv("#e/sysname", sysname);
setenv(ENV_SYSNAME, sysname);
if(tz != nil)
setenv("#e/timezone", tz);
setenv("timezone", tz);
/* go to new home directory */
snprint(buf, sizeof(buf), "/usr/%s", user);
if(chdir(buf) < 0)
if(chdir(home) < 0)
chdir("/");
/* read profile and start interactive rc */

View File

@ -26,7 +26,7 @@ main(int argc, char *argv[])
if(argc)
usage();
s = getenv("service");
s = getenv(ENV_SERVICE);
if(s && strcmp(s, "cpu") == 0){
fprint(2, "netkey must not be run on the cpu server\n");
exits("boofhead");

View File

@ -25,7 +25,7 @@ main(int argc, char **argv)
syslog(0, "secstore", "no /lib/ndb/local");
db = ndbcat(db, db2);
print("user=%s\n", getenv("user"));
print("%s\n", secureidcheck(getenv("user"), argv[1]));
print("%s=%s\n", ENV_USER, getenv(ENV_USER));
print("%s\n", secureidcheck(getenv(ENV_USER), argv[1]));
exits(0);
}

View File

@ -24,6 +24,7 @@ THIS SOFTWARE.
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <ctype.h>
#include <bio.h>
#include "awk.h"

View File

@ -16,6 +16,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include <auth.h>
#include <9P2000.h>
@ -301,7 +302,7 @@ remoteside(int old)
int i, n, fd, badchdir, gotcmd;
rfork(RFENVG);
putenv("service", "cpu");
putenv(ENV_SERVICE, "cpu");
fd = 0;
/* negotiate authentication mechanism */
@ -319,9 +320,9 @@ remoteside(int old)
fatal(1, "srvauth");
/* Set environment values for the user */
putenv("user", user);
putenv(ENV_USER, user);
sprint(home, "/usr/%s", user);
putenv("home", home);
putenv(ENV_HOME, home);
/* Now collect invoking cpu's current directory or possibly a command */
gotcmd = 0;

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <disk.h>
#include <auth.h>
#include <bio.h>
@ -115,7 +116,7 @@ main(int argc, char **argv)
zbuf = malloc(buflen);
memset(zbuf, 0, buflen);
cputype = getenv("cputype");
cputype = getenv(ENV_CPUTYPE);
if(cputype == 0)
cputype = "386";

View File

@ -12,6 +12,7 @@
*/
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <auth.h>
#include <9P2000.h>
#include <libsec.h>
@ -213,7 +214,7 @@ main(int argc, char **argv)
fatal("exportfs by none disallowed");
if(auth_chuid(ai, nsfile) < 0)
fatal("auth_chuid: %r");
putenv("service", "exportfs");
putenv(ENV_SERVICE, "exportfs");
}
if(srvfdfile){

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <ctype.h>
#include <bio.h>
#include <ip.h>

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include "pci.h"

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <auth.h>
#include <libsec.h>
@ -363,7 +364,7 @@ passive(void)
sysfatal("auth_proxy: %r");
if(auth_chuid(ai, nil) < 0)
sysfatal("auth_chuid: %r");
putenv("service", "import");
putenv(ENV_SERVICE, "import");
fd = dup(0, -1);
close(0);

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <ip.h>
#include "dhcp.h"
@ -210,6 +211,7 @@ void
dhcpinit(void)
{
int fd;
char *sysname;
dhcp.state = Sinit;
dhcp.timeout = 4;
@ -222,7 +224,9 @@ dhcpinit(void)
dhcp.xid = time(0)*getpid();
srand(dhcp.xid);
sprint(dhcp.cid, "%s.%d", getenv("sysname"), getpid());
sysname = getenv(ENV_SYSNAME);
sprint(dhcp.cid, "%s.%d", sysname, getpid());
free(sysname);
}
void

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <ip.h>
#include <bio.h>
#include <ndb.h>
@ -1360,7 +1361,7 @@ readsysname(void)
return name;
}
}
p = getenv("sysname");
p = getenv(ENV_SYSNAME);
if(p == nil || *p == 0)
return "unknown";
return p;

View File

@ -3,6 +3,7 @@
*/
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <ip.h>
#include <bio.h>
#include <ndb.h>
@ -390,7 +391,7 @@ init(void)
nsec(); /* make sure time file is open before forking */
setnetmtpt(conf.mpoint, sizeof conf.mpoint, nil);
conf.cputype = getenv("cputype");
conf.cputype = getenv(ENV_CPUTYPE);
if(conf.cputype == nil)
conf.cputype = "386";
@ -410,7 +411,7 @@ parseargs(int argc, char **argv)
/* default to any host name we already have */
if(*conf.hostname == 0){
p = getenv("sysname");
p = getenv(ENV_SYSNAME);
if(p == nil || *p == 0)
p = sysname();
if(p != nil)

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <auth.h>
#define NAMELEN 64 /* reasonable upper limit for name elements */
@ -97,7 +98,7 @@ main(int argc, char *argv[])
quiet = 0;
immutable = 0;
argv0 = argv[0];
cpu = getenv("cputype");
cpu = getenv(ENV_CPUTYPE);
if(cpu == 0)
error("can't get cputype");

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <auth.h>
int verbose;

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <auth.h>
#include <ip.h>
#include <mp.h>
@ -249,7 +250,7 @@ main(int argc, char **argv)
fmtinstall('E', eipfmt);
fmtinstall('I', eipfmt);
fmtinstall('V', eipfmt);
sysid = getenv("sysname");
sysid = getenv(ENV_SYSNAME);
/* detach from the current namespace */
if(debug)

View File

@ -16,10 +16,11 @@
* -t suppresses expanding multiple blanks into tabs
*
*/
#include <u.h>
#include <lib9.h>
#include <draw.h>
#include <bio.h>
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <draw.h>
#include <bio.h>
#define WIDTH 80
#define TAB 4

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <auth.h>
#include <9P2000.h>
#include <bio.h>
@ -1032,7 +1033,7 @@ ipid(void)
* is the ip address. ignore that.
*
*/
p = getenv("sysname");
p = getenv(ENV_SYSNAME);
if(p && *p){
attr = ipattr(p);
if(strcmp(attr, "ip") != 0)

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include <ndb.h>
#include <ip.h>

View File

@ -1,6 +1,7 @@
/* RFC2136 DNS inform - necessary for Win2k3 DNS servers */
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include <ndb.h>
#include <ip.h>
@ -123,8 +124,8 @@ main(int argc, char *argv[])
if(argc != 0)
usage();
if((sysname = getenv("sysname")) == nil)
sysfatal("$sysname not set");
if((sysname = getenv(ENV_SYSNAME)) == nil)
sysfatal("$%s not set", ENV_SYSNAME);
if((db = ndbopen(nil)) == nil)
sysfatal("can't open ndb: %r");

View File

@ -8,16 +8,9 @@
*/
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#define ENV_RCARGLIST "*"
#define ENV_APID "apid"
#define ENV_HOME "home"
#define ENV_IFS "ifs"
#define ENV_PATH "path"
#define ENV_PID "pid"
#define ENV_PROMPT "prompt"
#define ENV_STATUS "status"
#define ENV_CDPATH "cdpath"
#define ENV_RCNAME "rcname"
#define ENV_RCCMD "rccmd"

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
@ -167,7 +168,7 @@ filsysinit(Channel *cxfidalloc)
* Post srv pipe
*/
snprint(srvpipe, sizeof(srvpipe), "/srv/rio.%s.%d", fs->user, pid);
post(srvpipe, "wsys", fs->cfd);
post(srvpipe, ENV_WSYS, fs->cfd);
return fs;

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
@ -172,7 +173,7 @@ threadmain(int argc, char *argv[])
maxtab = 4;
free(s);
s = getenv("cputype");
s = getenv(ENV_CPUTYPE);
if(s){
snprint(buf, sizeof(buf), "/arch/%s/aux/rio", s);
bind(buf, "/cmd", MBEFORE);

View File

@ -26,7 +26,6 @@ Rune *right[]= {
char RSAM[] = "sam";
char SAMTERM[] = "/cmd/aux/samterm";
char HOME[] = "home";
char TMPDIR[] = "/tmp";
char SH[] = "rc";
char SHPATH[] = "/cmd/rc";

View File

@ -83,7 +83,7 @@ void main(int argc, char *argv[])
Strinit0(&rhs);
Strinit0(&curwd);
Strinit0(&plan9cmd);
home = getenv(HOME);
home = getenv(ENV_HOME);
disk = diskinit();
if(home == 0)
home = "/";

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <plumb.h>
#include "errors.h"
@ -353,7 +354,6 @@ extern Rune *right[];
extern char RSAM[]; /* system dependent */
extern char SAMTERM[];
extern char HOME[];
extern char TMPDIR[];
extern char SH[];
extern char SHPATH[];

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <libc.h>
#include <envvars.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <ctype.h>
#include <draw.h>
#include <event.h>
@ -1236,7 +1237,7 @@ main(int argc, char *argv[])
quotefmtinstall();
nmach = 1;
mysysname = getenv("sysname");
mysysname = getenv(ENV_SYSNAME);
if(mysysname == nil){
fprint(2, "stats: can't find $sysname: %r\n");
exits("sysname");

View File

@ -22,6 +22,7 @@
#include "fns.h"
#include "ureg.h"
#include "pool.h"
#include "envvars.h"
#include "io.h"
#include "apic.h"
@ -374,11 +375,11 @@ init0(void)
jehanne_snprint(buf, sizeof(buf), "%s %s", "AMD64", conffile);
loadenv(oargc, oargv);
ksetenv("terminal", buf, 0);
ksetenv("cputype", "amd64", 0);
ksetenv(ENV_CPUTYPE, "amd64", 0);
if(cpuserver)
ksetenv("service", "cpu", 0);
ksetenv(ENV_SERVICE, "cpu", 0);
else
ksetenv("service", "terminal", 0);
ksetenv(ENV_SERVICE, "terminal", 0);
poperror();
}
kproc("alarm", alarmkproc, 0);

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <libc.h>
#include <envvars.h>
#include <auth.h>
#include <9P2000.h>
#include "../boot/boot.h"

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <libc.h>
#include <envvars.h>
#include <auth.h>
#include <9P2000.h>
#include "../boot/boot.h"
@ -22,7 +23,7 @@ authentication(int cpuflag)
char *argv[16], **av;
int ac;
if(jehanne_access(factotumPath, AEXEC) < 0 || jehanne_getenv("user") != nil){
if(jehanne_access(factotumPath, AEXEC) < 0 || jehanne_getenv(ENV_USER) != nil){
glenda();
return;
}
@ -69,7 +70,7 @@ glenda(void)
int fd;
char *s;
s = jehanne_getenv("user");
s = jehanne_getenv(ENV_USER);
if(s == nil)
s = "glenda";

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include <auth.h>
#include <authsrv.h>
@ -65,9 +66,9 @@ buildns(int newns, char *user, char *file)
}
if(newns){
rfork(RFENVG|RFCNAMEG);
setenv("user", user);
setenv(ENV_USER, user);
snprint(home, sizeof home, "/usr/%s", user);
setenv("home", home);
setenv(ENV_HOME, home);
}
cdroot = nsfile(newns ? "newns" : "addns", b, rpc);

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <authsrv.h>
static int32_t finddosfile(int, char*);
@ -137,7 +138,7 @@ findnvram(Nvrwhere *locp)
if (nvrfile == nil)
nvrfile = getenv("nvram");
if (cputype == nil)
cputype = getenv("cputype");
cputype = getenv(ENV_CPUTYPE);
if(cputype == nil)
cputype = strdup("mips");
if(strcmp(cputype, "386")==0 || strcmp(cputype, "amd64")==0 || strcmp(cputype, "alpha")==0) {

View File

@ -1,5 +1,6 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include <auth.h>
#include <9P2000.h>

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <draw.h>
/* Connect us to new window, if possible */
@ -19,7 +20,7 @@ newwindow(char *str)
char *wsys;
char buf[256];
wsys = getenv("wsys");
wsys = getenv(ENV_WSYS);
if(wsys == nil)
return -1;
fd = open(wsys, ORDWR);

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <draw.h>
int

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <bio.h>
#include <ctype.h>
#include <ndb.h>

View File

@ -9,6 +9,7 @@
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include "plumb.h"
int

View File

@ -17,6 +17,7 @@
*/
#include <u.h>
#include <lib9.h>
#include <envvars.h>
#include <posix.h>
#include "internal.h"