swap.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. void error(char *);
  12. void
  13. main(int argc, char **argv)
  14. {
  15. Dir *d;
  16. int swapfd, cswfd;
  17. char buf[128], *p;
  18. int i, j;
  19. if(argc != 2) {
  20. print("Usage: swap path\n");
  21. exits("swap: failed");
  22. }
  23. d = dirstat(argv[1]);
  24. if(d == nil){
  25. print("swap: can't stat %s: %r\n", argv[1]);
  26. exits("swap: failed");
  27. }
  28. if(d->type != 'M'){ /* kernel device */
  29. swapfd = open(argv[1], ORDWR);
  30. p = argv[1];
  31. }
  32. else {
  33. p = getenv("sysname");
  34. if(p == 0)
  35. p = "swap";
  36. sprint(buf, "%s/%sXXXXXXX", argv[1], p);
  37. p = mktemp(buf);
  38. swapfd = create(p, ORDWR|ORCLOSE, 0600);
  39. }
  40. if(swapfd < 0 || (p[0] == '/' && p[1] == '\0')) {
  41. print("swap: failed to make a file: %r\n");
  42. exits("swap: failed");
  43. }
  44. i = create("/env/swap", OWRITE, 0666);
  45. if(i < 0)
  46. error("open /env/swap");
  47. if(write(i, p, strlen(p)) != strlen(p))
  48. error("sysname");
  49. close(i);
  50. print("swap: %s\n", p);
  51. cswfd = open("/dev/swap", OWRITE);
  52. if(cswfd < 0)
  53. error("open: /dev/swap");
  54. j = sprint(buf, "%d", swapfd);
  55. if(write(cswfd, buf, j) != j)
  56. error("write: /dev/swap");
  57. exits(0);
  58. }
  59. void
  60. error(char *s)
  61. {
  62. perror(s);
  63. exits(s);
  64. }