dm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* $NetBSD: dm.c,v 1.21 2004/11/05 21:30:32 dsl Exp $ */
  2. /*
  3. * Copyright (c) 1987, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
  33. The Regents of the University of California. All rights reserved.\n");
  34. #endif /* not lint */
  35. #ifndef lint
  36. #if 0
  37. static char sccsid[] = "@(#)dm.c 8.1 (Berkeley) 5/31/93";
  38. #else
  39. __RCSID("$NetBSD: dm.c,v 1.21 2004/11/05 21:30:32 dsl Exp $");
  40. #endif
  41. #endif /* not lint */
  42. #include <sys/param.h>
  43. #include <sys/file.h>
  44. #include <sys/time.h>
  45. #include <sys/resource.h>
  46. #include <err.h>
  47. #include <ctype.h>
  48. #include <errno.h>
  49. #include <pwd.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <time.h>
  54. #include <unistd.h>
  55. #include "utmpentry.h"
  56. #include "pathnames.h"
  57. static time_t now; /* current time value */
  58. static int priority = 0; /* priority game runs at */
  59. static char *game, /* requested game */
  60. *gametty; /* from tty? */
  61. void c_day(const char *, const char *, const char *);
  62. void c_game(const char *, const char *, const char *, const char *);
  63. void c_tty(const char *);
  64. const char *hour(int);
  65. double load(void);
  66. int main(int, char *[]);
  67. void nogamefile(void);
  68. void play(char **) __attribute__((__noreturn__));
  69. void read_config(void);
  70. int users(void);
  71. int
  72. main(argc, argv)
  73. int argc __attribute__((__unused__));
  74. char *argv[];
  75. {
  76. char *cp;
  77. nogamefile();
  78. game = (cp = strrchr(*argv, '/')) ? ++cp : *argv;
  79. if (!strcmp(game, "dm"))
  80. exit(0);
  81. gametty = ttyname(0);
  82. unsetenv("TZ");
  83. (void)time(&now);
  84. read_config();
  85. #ifdef LOG
  86. logfile();
  87. #endif
  88. play(argv);
  89. /*NOTREACHED*/
  90. return (0);
  91. }
  92. /*
  93. * play --
  94. * play the game
  95. */
  96. void
  97. play(args)
  98. char **args;
  99. {
  100. char pbuf[MAXPATHLEN];
  101. snprintf(pbuf, sizeof(pbuf), "%s%s", _PATH_HIDE, game);
  102. if (priority > 0) /* < 0 requires root */
  103. (void)setpriority(PRIO_PROCESS, 0, priority);
  104. execv(pbuf, args);
  105. err(1, "%s", pbuf);
  106. }
  107. /*
  108. * read_config --
  109. * read through config file, looking for key words.
  110. */
  111. void
  112. read_config()
  113. {
  114. FILE *cfp;
  115. char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
  116. if (!(cfp = fopen(_PATH_CONFIG, "r")))
  117. return;
  118. while (fgets(lbuf, sizeof(lbuf), cfp))
  119. switch (*lbuf) {
  120. case 'b': /* badtty */
  121. if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
  122. strcasecmp(f1, "badtty"))
  123. break;
  124. c_tty(f2);
  125. break;
  126. case 'g': /* game */
  127. if (sscanf(lbuf, "%s%s%s%s%s",
  128. f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
  129. break;
  130. c_game(f2, f3, f4, f5);
  131. break;
  132. case 't': /* time */
  133. if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
  134. strcasecmp(f1, "time"))
  135. break;
  136. c_day(f2, f3, f4);
  137. }
  138. (void)fclose(cfp);
  139. }
  140. /*
  141. * c_day --
  142. * if day is today, see if okay to play
  143. */
  144. void
  145. c_day(s_day, s_start, s_stop)
  146. const char *s_day, *s_start, *s_stop;
  147. {
  148. static const char *const days[] = {
  149. "sunday", "monday", "tuesday", "wednesday",
  150. "thursday", "friday", "saturday",
  151. };
  152. static struct tm *ct;
  153. int start, stop;
  154. if (!ct)
  155. ct = localtime(&now);
  156. if (strcasecmp(s_day, days[ct->tm_wday]))
  157. return;
  158. if (!isdigit((unsigned char)*s_start) || !isdigit((unsigned char)*s_stop))
  159. return;
  160. start = atoi(s_start);
  161. stop = atoi(s_stop);
  162. if (ct->tm_hour >= start && ct->tm_hour < stop) {
  163. if (start == 0 && stop == 24)
  164. errx(0, "Sorry, games are not available today.");
  165. else
  166. errx(0, "Sorry, games are not available from %s to %s today.",
  167. hour(start), hour(stop));
  168. }
  169. }
  170. /*
  171. * c_tty --
  172. * decide if this tty can be used for games.
  173. */
  174. void
  175. c_tty(tty)
  176. const char *tty;
  177. {
  178. static int first = 1;
  179. static char *p_tty;
  180. if (first) {
  181. p_tty = strrchr(gametty, '/');
  182. first = 0;
  183. }
  184. if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty)))
  185. errx(1, "Sorry, you may not play games on %s.", gametty);
  186. }
  187. /*
  188. * c_game --
  189. * see if game can be played now.
  190. */
  191. void
  192. c_game(s_game, s_load, s_users, s_priority)
  193. const char *s_game, *s_load, *s_users, *s_priority;
  194. {
  195. static int found;
  196. if (found)
  197. return;
  198. if (strcmp(game, s_game) && strcasecmp("default", s_game))
  199. return;
  200. ++found;
  201. if (isdigit((unsigned char)*s_load) && atoi(s_load) < load())
  202. errx(0, "Sorry, the load average is too high right now.");
  203. if (isdigit((unsigned char)*s_users) && atoi(s_users) <= users())
  204. errx(0, "Sorry, there are too many users logged on right now.");
  205. if (isdigit((unsigned char)*s_priority))
  206. priority = atoi(s_priority);
  207. }
  208. /*
  209. * load --
  210. * return 15 minute load average
  211. */
  212. double
  213. load()
  214. {
  215. double avenrun[3];
  216. if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0)
  217. err(1, "getloadavg() failed");
  218. return (avenrun[2]);
  219. }
  220. /*
  221. * users --
  222. * return current number of users
  223. * todo: check idle time; if idle more than X minutes, don't
  224. * count them.
  225. */
  226. int
  227. users()
  228. {
  229. static struct utmpentry *ohead = NULL;
  230. struct utmpentry *ep;
  231. int nusers;
  232. nusers = getutentries(NULL, &ep);
  233. if (ep != ohead) {
  234. freeutentries(ep);
  235. ohead = ep;
  236. }
  237. return nusers;
  238. }
  239. void
  240. nogamefile()
  241. {
  242. int fd, n;
  243. char buf[BUFSIZ];
  244. if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
  245. #define MESG "Sorry, no games right now.\n\n"
  246. (void)write(2, MESG, sizeof(MESG) - 1);
  247. while ((n = read(fd, buf, sizeof(buf))) > 0)
  248. (void)write(2, buf, n);
  249. exit(1);
  250. }
  251. }
  252. /*
  253. * hour --
  254. * print out the hour in human form
  255. */
  256. const char *
  257. hour(h)
  258. int h;
  259. {
  260. static const char *const hours[] = {
  261. "midnight", "1am", "2am", "3am", "4am", "5am",
  262. "6am", "7am", "8am", "9am", "10am", "11am",
  263. "noon", "1pm", "2pm", "3pm", "4pm", "5pm",
  264. "6pm", "7pm", "8pm", "9pm", "10pm", "11pm", "midnight" };
  265. if (h < 0 || h > 24)
  266. return ("BAD TIME");
  267. else
  268. return (hours[h]);
  269. }
  270. #ifdef LOG
  271. /*
  272. * logfile --
  273. * log play of game
  274. */
  275. logfile()
  276. {
  277. struct passwd *pw;
  278. FILE *lp;
  279. uid_t uid;
  280. int lock_cnt;
  281. if (lp = fopen(_PATH_LOG, "a")) {
  282. for (lock_cnt = 0;; ++lock_cnt) {
  283. if (!flock(fileno(lp), LOCK_EX))
  284. break;
  285. if (lock_cnt == 4) {
  286. warnx("log lock");
  287. (void)fclose(lp);
  288. return;
  289. }
  290. sleep((u_int)1);
  291. }
  292. if (pw = getpwuid(uid = getuid()))
  293. fputs(pw->pw_name, lp);
  294. else
  295. fprintf(lp, "%u", uid);
  296. fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
  297. (void)fclose(lp);
  298. (void)flock(fileno(lp), LOCK_UN);
  299. }
  300. }
  301. #endif /* LOG */