ref: f5dd1e67c44f44209ace4300b7513873296155d9
parent: 50ced482911d9e9f32c94377bfde5a7fb95ab4c8
author: cinap_lenrek <cinap_lenrek@localhost>
date: Mon Aug 22 06:03:15 EDT 2011
improve swap
--- a/rc/bin/cpurc
+++ b/rc/bin/cpurc
@@ -76,4 +76,4 @@
# 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)$'
--- a/rc/bin/dontkill
+++ b/rc/bin/dontkill
@@ -7,4 +7,4 @@
}
# 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}--- a/rc/bin/termrc
+++ b/rc/bin/termrc
@@ -103,4 +103,4 @@
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)$'
--- a/sys/man/8/swap
+++ b/sys/man/8/swap
@@ -6,11 +6,14 @@
.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 @@
.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)
--- 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;
+
+ 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");+ 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->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(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);
}
--
⑨