improve swap

This commit is contained in:
cinap_lenrek 2011-08-22 10:03:15 +02:00
parent e61553c52c
commit 0e5148b269
5 changed files with 51 additions and 54 deletions

View file

@ -76,4 +76,4 @@ if(test -e /cfg/$sysname/cpustart)
# mode of /proc/*/ctl is inherited across rfork, and sets modes on # mode of /proc/*/ctl is inherited across rfork, and sets modes on
# other /proc files, such as note, so let listen be killed. # other /proc files, such as note, so let listen be killed.
dontkill '^(ipconfig|factotum|mntgen|venti|kfs|cwfs.*|cs|dns|reboot)$' dontkill '^(ipconfig|factotum|mntgen|venti|kfs|cwfs.*|cs|dns|reboot|usbd|disk)$'

View file

@ -7,4 +7,4 @@ if (! ~ $#* 1) {
} }
# see /sys/src/9/port/proc.c:/^killbig # see /sys/src/9/port/proc.c:/^killbig
pids=`{psu | awk '$NF ~ /'$1'/ {print $2}'} pids=`{psu | awk '$NF ~ /'$1'/ {print $2}'}
~ $#pids 0 || chmod -w /proc/^$pids^/ctl ~ $#pids 0 || for(p in /proc/^$pids^/ctl){chmod +w $p; echo noswap >$p; chmod -w $p}

View file

@ -103,4 +103,4 @@ rm -f '/env/fn#ask'
if(test -f /dev/apm) if(test -f /dev/apm)
aux/apm aux/apm
dontkill '^(ipconfig|factotum|mntgen|kfs|cwfs.*|cs|dns|listen|reboot)$' dontkill '^(ipconfig|factotum|mntgen|kfs|cwfs.*|cs|dns|listen|reboot|usbd|disk)$'

View file

@ -6,11 +6,14 @@ swap \- establish a swap file
.I file .I file
.SH DESCRIPTION .SH DESCRIPTION
.I Swap .I Swap
establishes a file or device for the system to swap on. establishes a file for the system to swap on.
If If
.I file .I file
is a device, the device is used directly; if a directory, is an existing file, it is used for system swap. If it
a unique file is created in that directory on which to swap. does not exist, a new file is created. If
.I file
is a directory, a unique file is created in that directory
on which to swap.
The environment variable The environment variable
.B swap .B swap
is set to the full name of the resulting file. is set to the full name of the resulting file.
@ -27,4 +30,10 @@ the device an error is returned instead.
.SH BUGS .SH BUGS
Swapping to a file served by a local user-level process, such as Swapping to a file served by a local user-level process, such as
.IR kfs (4), .IR kfs (4),
can lead to deadlock; use raw devices or remote files instead. will lead to deadlock if the process isn't made non-swappable
(see the
.B noswap
ctl-message in
.IR proc (3)).
.SH "SEE ALSO"
.IR proc (3)

View file

@ -1,66 +1,54 @@
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
void error(char *);
void void
main(int argc, char **argv) main(int argc, char **argv)
{ {
Dir *d;
int swapfd, cswfd; int swapfd, cswfd;
char buf[128], *p; char buf[1024], *p;
int i, j; Dir *d;
if(argc != 2) { ARGBEGIN {
print("Usage: swap path\n"); } ARGEND;
exits("swap: failed");
if(argc != 1){
fprint(2, "Usage: swap file\n");
exits("usage");
} }
d = dirstat(argv[1]); swapfd = -1;
if(d == nil){ if(d = dirstat(p = *argv)){
print("swap: can't stat %s: %r\n", argv[1]); if(d->mode & DMDIR){
exits("swap: failed"); p = getenv("sysname");
} if(p == 0)
if(d->type != 'M'){ /* kernel device */ p = "swap";
swapfd = open(argv[1], ORDWR); snprint(buf, sizeof buf, "%s/%sXXXXXXX", *argv, p);
p = argv[1]; p = mktemp(buf);
} } else
else { swapfd = open(p, ORDWR);
p = getenv("sysname");
if(p == 0)
p = "swap";
sprint(buf, "%s/%sXXXXXXX", argv[1], p);
p = mktemp(buf);
swapfd = create(p, ORDWR|ORCLOSE, 0600);
} }
if(d == nil || (d->mode & DMDIR)){
if((swapfd = create(p, ORDWR|ORCLOSE, 0600)) >= 0){
Dir nd;
if(swapfd < 0 || (p[0] == '/' && p[1] == '\0')) { nulldir(&nd);
print("swap: failed to make a file: %r\n"); nd.mode = DMTMP|0600;
exits("swap: failed"); dirfwstat(swapfd, &nd);
}
} }
if(swapfd < 0)
sysfatal("%r");
if(fd2path(swapfd, p = buf, sizeof buf))
sysfatal("fd2path: %r");
if(putenv("swap", p) < 0)
sysfatal("putenv: %r");
i = create("/env/swap", OWRITE, 0666);
if(i < 0)
error("open /env/swap");
if(write(i, p, strlen(p)) != strlen(p))
error("sysname");
close(i);
print("swap: %s\n", p); print("swap: %s\n", p);
cswfd = open("/dev/swap", OWRITE); if((cswfd = open("/dev/swap", OWRITE)) < 0)
if(cswfd < 0) sysfatal("open: %r");
error("open: /dev/swap"); if(fprint(cswfd, "%d", swapfd) < 0)
sysfatal("write: %r");
j = sprint(buf, "%d", swapfd);
if(write(cswfd, buf, j) != j)
error("write: /dev/swap");
exits(0); exits(0);
} }
void
error(char *s)
{
perror(s);
exits(s);
}