-
Notifications
You must be signed in to change notification settings - Fork 27
/
stop.c
148 lines (132 loc) · 3.38 KB
/
stop.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
/*
* Copyright 1996, 1997, 1998, 1999 by Daniel B. Suthers,
* Pleasanton Ca. 94588 USA
* E-MAIL [email protected]
*
*/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#if 0
#include "x10.h"
#include "process.h"
#endif
#include <signal.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "x10.h"
#include "process.h"
#ifdef pid_t
#define PID_T pid_t
#else
#define PID_T long
#endif
extern int verbose;
extern CONFIG config;
extern CONFIG *configp;
/* This function looks up the currently running relay and aux
* processes and kills them.
*/
int c_stop(argc, argv)
int argc;
char *argv[];
{
PID_T pid;
extern PID_T lockpid( char * );
extern int unlock_state_engine();
extern void quit();
#ifdef __linux__
FILE * pidfile;
char buf[80];
char procname[80];
char *ignoretp;
#endif
char relayfilename[PATH_LEN + 1];
char auxfilename[PATH_LEN + 1];
if ( configp->ttyaux[0] != '\0' ) {
sprintf(auxfilename, "%s%s", AUXFILE, configp->suffixaux);
pid = lockpid(auxfilename);
if ( pid > 0 ) {
if ( verbose ) {
printf("killing %ld\n", (long)pid);
}
if ( kill(pid, SIGTERM) != 0 ) {
fprintf(stderr, "I could not kill %s (pid = %ld)\n",
auxfilename, (long)pid );
}
}
if ( pid == 0 ) {
if( verbose )
fprintf(stderr, "stop.c: heyu_aux pid not available\n");
}
}
sprintf(relayfilename, "%s%s", RELAYFILE, configp->suffix);
pid = lockpid(relayfilename);
if ( pid == 0 ) {
if ( verbose )
printf("stop.c: heyu_relay pid not available\n");
return(1);
}
if ( pid < 0 )
return(-1);
/* Now this is really linux dependent. It relies on having the
* /proc filesystem in order to verify that the process is really
* the right one.
*/
#ifdef __linux__
sprintf(procname, "/proc/%ld/cmdline", (long)pid);
if( (pidfile = fopen( procname,"r")) == NULL )
{
if( verbose )
printf("proc file not openable for %ld\n", (long)pid);
quit(0);
}
buf[0] = '\0';
ignoretp = fgets(buf,79, pidfile);
{
buf[79] = '\0';
if( strstr(buf, "heyu_relay") == NULL )
{
if( verbose )
printf("proc file for %ld did not contain \"heyu_relay\"\n", (long)pid);
quit(0);
}
}
#endif
if( verbose )
printf("killing %ld\n", (long)pid);
unlock_state_engine();
if ( kill(pid, SIGTERM) != 0 ) {
fprintf(stderr, "I could not kill %s (pid = %ld)\n",
relayfilename, (long)pid);
quit();
}
#if 0
quit();
#endif
return(0);
}