summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@localhost>2011-08-22 10:03:15 +0200
committercinap_lenrek <cinap_lenrek@localhost>2011-08-22 10:03:15 +0200
commit0e5148b26942b1c60b942f3f2ac550464af9e787 (patch)
treef49f5470ff86df3c912601beba18f97a41a7479d
parente61553c52c6192b5eaea754bd559c657ffdb024f (diff)
downloadplan9front-0e5148b26942b1c60b942f3f2ac550464af9e787.tar.xz
improve swap
-rwxr-xr-xrc/bin/cpurc2
-rwxr-xr-xrc/bin/dontkill2
-rwxr-xr-xrc/bin/termrc2
-rw-r--r--sys/man/8/swap17
-rw-r--r--sys/src/cmd/swap.c84
5 files changed, 52 insertions, 55 deletions
diff --git a/rc/bin/cpurc b/rc/bin/cpurc
index 61cc32e4a..25f6cf431 100755
--- a/rc/bin/cpurc
+++ b/rc/bin/cpurc
@@ -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)$'
diff --git a/rc/bin/dontkill b/rc/bin/dontkill
index 96203e675..a471aadb8 100755
--- a/rc/bin/dontkill
+++ b/rc/bin/dontkill
@@ -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}
diff --git a/rc/bin/termrc b/rc/bin/termrc
index 96430b033..8af723a8f 100755
--- a/rc/bin/termrc
+++ b/rc/bin/termrc
@@ -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)$'
diff --git a/sys/man/8/swap b/sys/man/8/swap
index e905d8f45..73c0dcb45 100644
--- a/sys/man/8/swap
+++ b/sys/man/8/swap
@@ -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)
diff --git a/sys/src/cmd/swap.c b/sys/src/cmd/swap.c
index 6350caff5..8c3211626 100644
--- a/sys/src/cmd/swap.c
+++ b/sys/src/cmd/swap.c
@@ -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;
- 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);
+ if(argc != 1){
+ fprint(2, "Usage: swap file\n");
+ exits("usage");
}
- if(swapfd < 0 || (p[0] == '/' && p[1] == '\0')) {
- print("swap: failed to make a file: %r\n");
- exits("swap: failed");
+ 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;
+
+ 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);
-}