Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <ctype.h>
5 /*
6 * Absent other hints, it works reasonably well to use
7 * the X11 display name as the name space identifier.
8 * This is how sam's B has worked since the early days.
9 * Since most programs using name spaces are also using X,
10 * this still seems reasonable. Terminal-only sessions
11 * can set $NAMESPACE.
12 */
13 static char*
14 nsfromdisplay(void)
15 {
16 int fd;
17 Dir *d;
18 char *disp, *p;
20 if((disp = getenv("DISPLAY")) == nil){
21 werrstr("$DISPLAY not set");
22 return nil;
23 }
25 /* canonicalize: xxx:0.0 => xxx:0 */
26 p = strrchr(disp, ':');
27 if(p){
28 p++;
29 while(isdigit((uchar)*p))
30 p++;
31 if(strcmp(p, ".0") == 0)
32 *p = 0;
33 }
35 p = smprint("/tmp/ns.%s.%s", getuser(), disp);
36 free(disp);
37 if(p == nil){
38 werrstr("out of memory");
39 return p;
40 }
41 if((fd=create(p, OREAD, DMDIR|0700)) >= 0){
42 close(fd);
43 return p;
44 }
45 if((d = dirstat(p)) == nil){
46 free(d);
47 werrstr("stat %s: %r", p);
48 free(p);
49 return nil;
50 }
51 if((d->mode&0777) != 0700 || strcmp(d->uid, getuser()) != 0){
52 werrstr("bad name space dir %s", p);
53 free(p);
54 free(d);
55 return nil;
56 }
57 free(d);
58 return p;
59 }
61 char*
62 getns(void)
63 {
64 char *ns;
66 ns = getenv("NAMESPACE");
67 if(ns == nil)
68 ns = nsfromdisplay();
69 if(ns == nil){
70 werrstr("$NAMESPACE not set, %r");
71 return nil;
72 }
73 return ns;
74 }