-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
203 lines (173 loc) · 3.93 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*-
* SPDX-License-Identifier: MIT-open-group
*
* Copyright (c) 1991-1994 Sony Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* from Sony Corporation.
*/
#include "config.h"
#include "sj_sysvdef.h"
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "const.h"
#include "server.h"
extern int fork_flag;
extern char *lock_file;
char *version_number = "2.08C";
char *time_stamp = "Mon Mar 23 16:42:59 JST 1998";
static void
opening(void)
{
printf("Kana-Kanji Conversion Server Version %s\r\n", version_number);
printf("Copyright (c) 1990-1995 Sony Corporation\r\n");
printf("Created at %s\r\n", time_stamp);
}
#ifdef LOCK_FILE
int
make_lockfile(void)
{
int fd;
fd = open(lock_file, O_CREAT | O_EXCL, 0600);
if (fd < 0)
return ERROR;
close(fd);
return 0;
}
int
erase_lockfile(void)
{
return unlink(lock_file);
}
#endif
static int
signal_handler(int sig)
{
warning_out("signal %d catched.", sig);
}
static int
terminate_handler(int sig)
{
close_socket();
sj_closeall();
#ifdef LOCK_FILE
erase_lockfile();
#endif
exit(0);
}
void
server_terminate(void)
{
close_socket();
sj_closeall();
#ifdef LOCK_FILE
erase_lockfile();
#endif
exit(0);
}
static void
exec_fork(void)
{
int tmp;
if (fork_flag && (tmp = fork())) {
if (tmp < 0) {
fprintf(stderr, "Can't fork child process\r\n");
fflush(stderr);
} else {
signal(SIGCHLD, _exit);
signal(SIGHUP, SIG_IGN);
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGTERM, _exit);
wait(0);
_exit(0);
}
}
signal(SIGHUP, (void (*)())signal_handler);
signal(SIGINT, (void (*)())terminate_handler);
signal(SIGQUIT, (void (*)())signal_handler);
signal(SIGTERM, (void (*)())terminate_handler);
if (fork_flag)
signal(SIGTSTP, SIG_IGN);
}
static void
leave_tty(void)
{
int tmp;
tmp = open_error();
tmp |= open_log();
tmp |= open_debug();
kill(getppid(), SIGTERM);
fclose(stdin);
fclose(stdout);
if (!tmp)
fclose(stderr);
if (fork_flag) {
if ((tmp = open("/dev/tty", O_RDWR)) >= 0) {
ioctl(tmp, TIOCNOTTY, 0);
close(tmp);
}
}
}
int
main(int argc, char **argv)
{
opening();
#ifndef __NetBSD__
if (setuid(geteuid())) {
fprintf(stderr, "error at setuid.\r\n");
exit(1);
}
#endif
parse_arg(argc, argv);
read_runcmd();
set_default();
#ifdef LOCK_FILE
if (make_lockfile() == ERROR) {
fprintf(stderr, "Already running...\r\n");
exit(1);
}
#endif
exec_fork();
socket_init();
open_socket();
leave_tty();
preload_dict();
preopen_dict();
communicate();
close_socket();
sj_closeall();
#ifdef LOCK_FILE
erase_lockfile();
#endif
exit(0);
}