chat.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "all.h"
  10. #define SIZE 1024
  11. int chatty;
  12. int conftime;
  13. #define NSIZE 128
  14. static char nbuf[NSIZE];
  15. static int chatpid;
  16. static void
  17. killchat(void)
  18. {
  19. char buf[NSIZE];
  20. int fd;
  21. remove(nbuf);
  22. snprint(buf, sizeof buf, "/proc/%d/note", chatpid);
  23. fd = open(buf, OWRITE);
  24. write(fd, "kill\n", 5);
  25. close(fd);
  26. }
  27. void
  28. chatsrv(char *name)
  29. {
  30. int n, sfd, pfd[2];
  31. char *p, buf[256];
  32. if(name && *name)
  33. snprint(nbuf, sizeof nbuf, "/srv/%s", name);
  34. else{
  35. if(p = strrchr(argv0, '/')) /* assign = */
  36. name = p+1;
  37. else
  38. name = argv0;
  39. snprint(nbuf, sizeof nbuf, "/srv/%s.chat", name);
  40. }
  41. remove(nbuf);
  42. if(pipe(pfd) < 0)
  43. panic("chatsrv pipe");
  44. sfd = create(nbuf, OWRITE, 0600);
  45. if(sfd < 0)
  46. panic("chatsrv create %s", nbuf);
  47. chatpid = rfork(RFPROC|RFMEM);
  48. switch(chatpid){
  49. case -1:
  50. panic("chatsrv fork");
  51. case 0:
  52. break;
  53. default:
  54. atexit(killchat);
  55. return;
  56. }
  57. fprint(sfd, "%d", pfd[1]);
  58. close(sfd);
  59. close(pfd[1]);
  60. for(;;){
  61. n = read(pfd[0], buf, sizeof(buf)-1);
  62. if(n < 0)
  63. break;
  64. if(n == 0)
  65. continue;
  66. buf[n] = 0;
  67. if(buf[0] == 'c')
  68. conftime = 999;
  69. chatty = strtol(buf, 0, 0);
  70. if(abs(chatty) < 2)
  71. rpcdebug = 0;
  72. else
  73. rpcdebug = abs(chatty) - 1;
  74. fprint(2, "%s: chatty=%d, rpcdebug=%d, conftime=%d\n",
  75. nbuf, chatty, rpcdebug, conftime);
  76. }
  77. _exits(0);
  78. }
  79. void
  80. chat(char *fmt, ...)
  81. {
  82. char buf[SIZE];
  83. va_list arg;
  84. Fmt f;
  85. if(!chatty)
  86. return;
  87. fmtfdinit(&f, 2, buf, sizeof buf);
  88. va_start(arg, fmt);
  89. fmtvprint(&f, fmt, arg);
  90. va_end(arg);
  91. fmtfdflush(&f);
  92. }
  93. void
  94. clog(char *fmt, ...)
  95. {
  96. char buf[SIZE];
  97. va_list arg;
  98. int n;
  99. va_start(arg, fmt);
  100. vseprint(buf, buf+SIZE, fmt, arg);
  101. va_end(arg);
  102. n = strlen(buf);
  103. if(chatty || rpcdebug)
  104. write(2, buf, n);
  105. if(chatty <= 0){
  106. if(n>0 && buf[n-1] == '\n')
  107. buf[n-1] = 0;
  108. syslog(0, "nfs", buf);
  109. }
  110. }
  111. void
  112. panic(char *fmt, ...)
  113. {
  114. char buf[SIZE];
  115. va_list arg;
  116. va_start(arg, fmt);
  117. vseprint(buf, buf+SIZE, fmt, arg);
  118. va_end(arg);
  119. if(chatty || rpcdebug)
  120. fprint(2, "%s %d: %s: %r\n", argv0, getpid(), buf);
  121. if(chatty <= 0)
  122. syslog(0, "nfs", buf);
  123. exits("panic");
  124. }