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
# 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
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)
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
.SH DESCRIPTION
.I Swap
establishes a file or device for the system to swap on.
establishes a file for the system to swap on.
If
.I file
is a device, the device is used directly; if a directory,
a unique file is created in that directory on which to swap.
is an existing file, it is used for system swap. If it
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
.B swap
is set to the full name of the resulting file.
@ -27,4 +30,10 @@ the device an error is returned instead.
.SH BUGS
Swapping to a file served by a local user-level process, such as
.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 <libc.h>
void error(char *);
void
main(int argc, char **argv)
{
Dir *d;
int swapfd, cswfd;
char buf[128], *p;
int i, j;
char buf[1024], *p;
Dir *d;
if(argc != 2) {
print("Usage: swap path\n");
exits("swap: failed");
ARGBEGIN {
} ARGEND;
if(argc != 1){
fprint(2, "Usage: swap file\n");
exits("usage");
}
d = dirstat(argv[1]);
if(d == nil){
print("swap: can't stat %s: %r\n", argv[1]);
exits("swap: failed");
}
if(d->type != 'M'){ /* kernel device */
swapfd = open(argv[1], ORDWR);
p = argv[1];
}
else {
p = getenv("sysname");
if(p == 0)
p = "swap";
sprint(buf, "%s/%sXXXXXXX", argv[1], p);
p = mktemp(buf);
swapfd = create(p, ORDWR|ORCLOSE, 0600);
swapfd = -1;
if(d = dirstat(p = *argv)){
if(d->mode & DMDIR){
p = getenv("sysname");
if(p == 0)
p = "swap";
snprint(buf, sizeof buf, "%s/%sXXXXXXX", *argv, p);
p = mktemp(buf);
} else
swapfd = open(p, ORDWR);
}
if(d == nil || (d->mode & DMDIR)){
if((swapfd = create(p, ORDWR|ORCLOSE, 0600)) >= 0){
Dir nd;
if(swapfd < 0 || (p[0] == '/' && p[1] == '\0')) {
print("swap: failed to make a file: %r\n");
exits("swap: failed");
nulldir(&nd);
nd.mode = DMTMP|0600;
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);
cswfd = open("/dev/swap", OWRITE);
if(cswfd < 0)
error("open: /dev/swap");
if((cswfd = open("/dev/swap", OWRITE)) < 0)
sysfatal("open: %r");
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);
}
void
error(char *s)
{
perror(s);
exits(s);
}