1 /*-
2  * Copyright (c) 1999 Thomas Runge (coto@core.de)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <X11/Intrinsic.h>
36 
37 #include "version_check.h"
38 #include "radio.h"
39 
40 extern int debug;
41 extern XtAppContext app_con;
42 
43 static XtIntervalId fetchtimerId = -1;
44 static int fetch_cmd_pid;
45 
46 static void checkReturnedValue();
47 
child_handler()48 static void child_handler()
49 {
50 	pid_t child_pid;
51 	int status;
52 
53 	if(debug)
54 		printf("Waiting for dead child\n");
55 
56 	child_pid = wait(&status);
57 
58 	if(debug)
59 		printf("child was pid: %d (returned %d).\n", child_pid, status);
60 	checkReturnedValue();
61 }
62 
timeout_handler(XtPointer clientData,XtIntervalId * id)63 static void timeout_handler(XtPointer clientData, XtIntervalId *id)
64 {
65 	if(fetch_cmd_pid != -1)
66 	{
67 		if(debug)
68 			printf("timer went by, killing unsuccessful version check\n");
69 		kill(fetch_cmd_pid, SIGINT);
70 		fetch_cmd_pid = -1;
71 		checkReturnedValue();
72 	}
73 	else
74 		if(debug)
75 			printf("timer went by\n");
76 }
77 
CheckNewVersion()78 void CheckNewVersion()
79 {
80 	fetchtimerId = XtAppAddTimeOut(app_con, CHECKTIMEOUT,
81 									timeout_handler, (XtPointer)NULL);
82 
83 	version_enable_button(False);
84 	signal(SIGALRM, (void*) checkReturnedValue);
85 	signal(SIGCHLD, (void*) child_handler);
86 	if((fetch_cmd_pid = fork()) == -1)
87 	{
88 		perror("fork");
89 		return;
90 	}
91 	else if(fetch_cmd_pid == 0)
92 	{
93 		/* child process */
94 		chdir("/tmp");
95 		if(execl(FTP_COMMAND, "xmradio_fetch_version",
96 #ifdef __NetBSD__
97 					"-f",
98 #endif
99 					"-V", HOMEURL CHECKVERSION) == -1)
100 		{
101 			perror("exec");
102 		}
103 		kill(getppid(), SIGALRM);
104 		if(debug)
105 			printf("Child exiting.\n");
106 		exit(EXIT_SUCCESS);
107 	}
108 	else
109 	{
110 		/* parent */
111 		if(debug)
112 			printf("Parent returning.\n");
113 		return;
114 	}
115 }
116 
117 
checkReturnedValue()118 static void checkReturnedValue()
119 {
120 	char *ver;
121 	int ret = 0;
122 	char *ret_string;
123 	FILE *fp;
124 	char buf[512];
125 
126     if(debug)
127         printf("got signal from child!\n");
128 
129 	XtRemoveTimeOut(fetchtimerId);
130 	version_enable_button(True);
131 
132 	ver = (char*)malloc(strlen(CHECKVERSION) + 6);
133 	sprintf(ver, "/tmp/%s", CHECKVERSION);
134 
135 	fp = fopen(ver, "r");
136 	if(fp)
137 	{
138 		if(fgets(buf, 512, fp) != NULL)
139 			ret_string = strdup(buf);
140 
141 		fclose(fp);
142 		unlink(ver);
143 	}
144 	else
145 	{
146 		ret_string = strdup("Probably a network error.");
147 		ret = -1;
148 	}
149 
150 	free(ver);
151 	version_check(ret, ret_string);
152 }
153 
154