xref: /openbsd/games/sail/pl_1.c (revision df69c215)
1 /*	$OpenBSD: pl_1.c,v 1.13 2019/06/28 13:32:52 deraadt Exp $	*/
2 /*	$NetBSD: pl_1.c,v 1.3 1995/04/22 10:37:07 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/wait.h>
34 
35 #include <errno.h>
36 #include <signal.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 #include "extern.h"
41 #include "player.h"
42 
43 #ifndef __GNUC__
44 #define __attribute__(x)
45 #endif
46 
47 /*
48  * If we get here before a ship is chosen, then ms == 0 and
49  * we don't want to update the score file, or do any Write's either.
50  * We can assume the sync file is already created and may need
51  * to be removed.
52  * Of course, we don't do any more Sync()'s if we got here
53  * because of a Sync() failure.
54  */
55 void
leave(int conditions)56 leave(int conditions)
57 {
58 	(void) signal(SIGHUP, SIG_IGN);
59 	(void) signal(SIGINT, SIG_IGN);
60 	(void) signal(SIGQUIT, SIG_IGN);
61 	(void) signal(SIGALRM, SIG_IGN);
62 	(void) signal(SIGCHLD, SIG_DFL);
63 
64 	if (done_curses) {
65 		Msg("It looks like you've had it!");
66 		switch (conditions) {
67 		case LEAVE_QUIT:
68 			break;
69 		case LEAVE_CAPTURED:
70 			Msg("Your ship was captured.");
71 			break;
72 		case LEAVE_HURRICAN:
73 			Msg("Hurricane!  All ships destroyed.");
74 			break;
75 		case LEAVE_DRIVER:
76 			Msg("The driver died.");
77 			break;
78 		case LEAVE_SYNC:
79 			Msg("Synchronization error.");
80 			break;
81 		default:
82 			Msg("A funny thing happened (%d).", conditions);
83 		}
84 	} else {
85 		switch (conditions) {
86 		case LEAVE_QUIT:
87 			break;
88 		case LEAVE_DRIVER:
89 			printf("The driver died.\n");
90 			break;
91 		case LEAVE_FORK:
92 			perror("fork");
93 			break;
94 		case LEAVE_SYNC:
95 			printf("Synchronization error\n.");
96 			break;
97 		default:
98 			printf("A funny thing happened (%d).\n",
99 				conditions);
100 		}
101 	}
102 
103 	if (ms != 0) {
104 		logger(ms);
105 		if (conditions != LEAVE_SYNC) {
106 			makemsg(ms, "Captain %s relinquishing.",
107 				mf->captain);
108 			Write(W_END, ms, 0, 0, 0, 0);
109 			(void) Sync();
110 		}
111 	}
112 	sync_close(!hasdriver);
113 	sleep(5);
114 	cleanupscreen();
115 	exit(0);
116 }
117 
118 void
choke(int n)119 choke(int n __attribute__((unused)))
120 {
121 	leave(LEAVE_QUIT);
122 }
123 
124 void
child(int n)125 child(int n __attribute__((unused)))
126 {
127 	int status;
128 	int pid;
129 	int save_errno = errno;
130 
131 	(void) signal(SIGCHLD, SIG_DFL);
132 	do {
133 		pid = waitpid((pid_t)-1, &status, WNOHANG);
134 		if (pid == -1 || (pid > 0 && !WIFSTOPPED(status)))
135 			hasdriver = 0;
136 	} while (pid > 0);
137 	(void) signal(SIGCHLD, child);
138 	errno = save_errno;
139 }
140