1 /*
2 * Copyright 1996, 1997, 1998, 1999 by Daniel B. Suthers,
3 * Pleasanton Ca. 94588 USA
4 * E-MAIL dbs@tanj.com
5 *
6 */
7
8 /*
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24 #include <stdio.h>
25 #include <ctype.h>
26 #if 0
27 #include "x10.h"
28 #include "process.h"
29 #endif
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 #include "x10.h"
36 #include "process.h"
37
38 #ifdef pid_t
39 #define PID_T pid_t
40 #else
41 #define PID_T long
42 #endif
43
44 extern int verbose;
45 extern CONFIG config;
46 extern CONFIG *configp;
47
48
49 /* This function looks up the currently running relay and aux
50 * processes and kills them.
51 */
c_stop(argc,argv)52 int c_stop(argc, argv)
53 int argc;
54 char *argv[];
55 {
56 PID_T pid;
57 extern PID_T lockpid( char * );
58 extern int unlock_state_engine();
59 extern void quit();
60 #ifdef __linux__
61 FILE * pidfile;
62 char buf[80];
63 char procname[80];
64 char *ignoretp;
65 #endif
66 char relayfilename[PATH_LEN + 1];
67 char auxfilename[PATH_LEN + 1];
68
69 if ( configp->ttyaux[0] != '\0' ) {
70 sprintf(auxfilename, "%s%s", AUXFILE, configp->suffixaux);
71 pid = lockpid(auxfilename);
72 if ( pid > 0 ) {
73 if ( verbose ) {
74 printf("killing %ld\n", (long)pid);
75 }
76 if ( kill(pid, SIGTERM) != 0 ) {
77 fprintf(stderr, "I could not kill %s (pid = %ld)\n",
78 auxfilename, (long)pid );
79 }
80 }
81 if ( pid == 0 ) {
82 if( verbose )
83 fprintf(stderr, "stop.c: heyu_aux pid not available\n");
84 }
85 }
86
87
88 sprintf(relayfilename, "%s%s", RELAYFILE, configp->suffix);
89 pid = lockpid(relayfilename);
90 if ( pid == 0 ) {
91 if ( verbose )
92 printf("stop.c: heyu_relay pid not available\n");
93 return(1);
94 }
95 if ( pid < 0 )
96 return(-1);
97
98 /* Now this is really linux dependent. It relies on having the
99 * /proc filesystem in order to verify that the process is really
100 * the right one.
101 */
102 #ifdef __linux__
103 sprintf(procname, "/proc/%ld/cmdline", (long)pid);
104 if( (pidfile = fopen( procname,"r")) == NULL )
105 {
106 if( verbose )
107 printf("proc file not openable for %ld\n", (long)pid);
108 quit(0);
109 }
110 buf[0] = '\0';
111 ignoretp = fgets(buf,79, pidfile);
112 {
113 buf[79] = '\0';
114 if( strstr(buf, "heyu_relay") == NULL )
115 {
116 if( verbose )
117 printf("proc file for %ld did not contain \"heyu_relay\"\n", (long)pid);
118 quit(0);
119 }
120 }
121 #endif
122
123 if( verbose )
124 printf("killing %ld\n", (long)pid);
125
126 unlock_state_engine();
127
128 if ( kill(pid, SIGTERM) != 0 ) {
129 fprintf(stderr, "I could not kill %s (pid = %ld)\n",
130 relayfilename, (long)pid);
131 quit();
132 }
133
134 #if 0
135 quit();
136 #endif
137 return(0);
138 }
139