File: external.c

package info (click to toggle)
streamripper 1.64.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,904 kB
  • sloc: ansic: 11,699; sh: 8,548; makefile: 421; perl: 34
file content (365 lines) | stat: -rw-r--r-- 9,694 bytes parent folder | download | duplicates (3)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* external.c
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#if defined (WIN32)
#include <process.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/wait.h>
#endif
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include "debug.h"
#include "mchar.h"
#include "external.h"
#include "compat.h"

/* Unix:
http://www.cs.uleth.ca/~holzmann/C/system/pipeforkexec.html
http://www.ecst.csuchico.edu/~beej/guide/ipc/fork.html
*/
/* Win32:
Non-blocking pipe using PeekNamedPipe():
http://list-archive.xemacs.org/xemacs-beta/199910/msg00263.html
Using GenerateConsoleCtrlEvent():
http://www.byte.com/art/9410/sec14/art3.htm
*/

/* ----------------------------- SHARED FUNCTIONS ------------------------ */
External_Process*
alloc_ep (void)
{
    External_Process* ep;
    ep = (External_Process*) malloc (sizeof (External_Process));
    if (!ep) return 0;
    ep->line_buf[0] = 0;
    ep->line_buf_idx = 0;
    ep->album_buf[0] = 0;
    ep->artist_buf[0] = 0;
    ep->title_buf[0] = 0;
    return ep;
}

static int
parse_external_byte (RIP_MANAGER_INFO* rmi, External_Process* ep, 
		     TRACK_INFO* ti, char c)
{
    int got_metadata = 0;

    if (c != '\r' && c != '\n') {
	if (ep->line_buf_idx < MAX_EXT_LINE_LEN-1) {
	    ep->line_buf[ep->line_buf_idx++] = c;
	    ep->line_buf[ep->line_buf_idx] = 0;
	}
    } else {
	if (!strcmp (".",ep->line_buf)) {
	    /* Found end of record! */
	    mchar tmp_raw_metadata[MAX_TRACK_LEN];
	    gstring_from_string (rmi, ti->artist, MAX_TRACK_LEN, ep->artist_buf,
				 CODESET_METADATA);
	    gstring_from_string (rmi, ti->album, MAX_TRACK_LEN, ep->album_buf,
				 CODESET_METADATA);
	    gstring_from_string (rmi, ti->title, MAX_TRACK_LEN, ep->title_buf,
				 CODESET_METADATA);

	    g_snprintf (tmp_raw_metadata, MAX_TRACK_LEN, "%s - %s", 
			ti->artist, ti->title);
	    string_from_gstring (rmi, ti->raw_metadata, MAX_TRACK_LEN, 
				 tmp_raw_metadata, CODESET_METADATA);
	    ti->have_track_info = 1;
	    ti->save_track = TRUE;

	    ep->artist_buf[0] = 0;
	    ep->album_buf[0] = 0;
	    ep->title_buf[0] = 0;
	    got_metadata = 1;
	} else if (!strncmp ("ARTIST=", ep->line_buf, strlen("ARTIST="))) {
	    strcpy (ep->artist_buf, &ep->line_buf[strlen("ARTIST=")]);
	} else if (!strncmp ("ALBUM=", ep->line_buf, strlen("ALBUM="))) {
	    strcpy (ep->album_buf, &ep->line_buf[strlen("ALBUM=")]);
	} else if (!strncmp ("TITLE=", ep->line_buf, strlen("TITLE="))) {
	    strcpy (ep->title_buf, &ep->line_buf[strlen("TITLE=")]);
	}
	ep->line_buf[0] = 0;
	ep->line_buf_idx = 0;
    }

    return got_metadata;
}


/* ----------------------------- WIN32 FUNCTIONS ------------------------- */
#if defined (WIN32)

 
External_Process*
spawn_external (char* cmd)
{
    External_Process* ep;
    HANDLE hChildStdinRd;
    HANDLE hChildStdinWr;
    HANDLE hChildStdoutWr;

    SECURITY_ATTRIBUTES saAttr; 
    PROCESS_INFORMATION piProcInfo; 
    STARTUPINFO startup_info;
    BOOL rc;
    DWORD creation_flags;

    ep = alloc_ep ();
    if (!ep) return 0;

    /* Set the bInheritHandle flag so pipe handles are inherited. */
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    saAttr.bInheritHandle = TRUE; 
    saAttr.lpSecurityDescriptor = NULL; 

    /* Create a pipe for the child process's STDOUT. */
    if (!CreatePipe (&ep->mypipe, &hChildStdoutWr, &saAttr, 0)) {
        debug_printf ("Stdout pipe creation failed\n");
	free (ep);
	return 0;
    }

    /* Ensure the read handle to the pipe for STDOUT is not inherited.*/
    SetHandleInformation (ep->mypipe, HANDLE_FLAG_INHERIT, 0);

    /* Create a pipe for the child process's STDIN. */
    if (!CreatePipe (&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
        debug_printf ("Stdin pipe creation failed\n");
	free (ep);
	return 0;
    }

    /* Ensure the write handle to the pipe for STDIN is not inherited. */
    SetHandleInformation (hChildStdinWr, HANDLE_FLAG_INHERIT, 0);

    /* create the child process */
    ZeroMemory (&piProcInfo, sizeof(PROCESS_INFORMATION));
    ZeroMemory (&startup_info, sizeof(STARTUPINFO));
    startup_info.cb = sizeof(STARTUPINFO); 
    startup_info.hStdError = hChildStdoutWr;
    startup_info.hStdOutput = hChildStdoutWr;
    startup_info.hStdInput = hChildStdinRd;
    startup_info.dwFlags |= STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    //startup_info.wShowWindow = SW_SHOW;
    startup_info.wShowWindow = SW_HIDE;

    creation_flags = 0;
    creation_flags |= CREATE_NEW_PROCESS_GROUP;
    //creation_flags |= CREATE_NEW_CONSOLE;

    rc = CreateProcess (
		NULL,           // executable name
		cmd,	        // command line 
		NULL,           // process security attributes 
		NULL,           // primary thread security attributes 
		TRUE,           // handles are inherited 
		creation_flags, // creation flags 
		NULL,           // use parent's environment 
		NULL,           // use parent's current directory 
		&startup_info,  // STARTUPINFO pointer
		&piProcInfo);   // receives PROCESS_INFORMATION 
    if (rc == 0) {
        debug_printf ("CreateProcess() failed\n");
	free (ep);
	return 0;
    }
    ep->hproc = piProcInfo.hProcess;
    ep->pid = piProcInfo.dwProcessId;
    //CloseHandle (piProcInfo.hProcess);
    CloseHandle (piProcInfo.hThread);

    Sleep (0);
    return ep;
}
 
int
read_external (RIP_MANAGER_INFO* rmi, External_Process* ep, TRACK_INFO* ti)
{
    char c;
    int rc;
    int got_metadata = 0;
    DWORD num_read;

    ti->have_track_info = 0;

    Sleep (0);
    while (1) {
	DWORD bytes_avail = 0;
	rc = PeekNamedPipe (ep->mypipe, NULL, 0, NULL, &bytes_avail, NULL);
	if (!rc) {
	    DWORD error_code;
	    /* Pipe closed? */
	    /* GCS FIX: Restart external program if pipe closed */
	    error_code = GetLastError ();
	    debug_printf ("PeekNamedPipe failed, error_code = %d\n",
			    error_code);
	    return 0;
	}
	if (bytes_avail <= 0) {
	    /* Pipe blocked */
	    return got_metadata;
	}
	/* Pipe has data available, so read it */
	rc = ReadFile (ep->mypipe, &c, 1, &num_read, NULL);
	if (rc > 0 && num_read > 0) {
	    int got_meta_byte;
	    got_meta_byte = parse_external_byte (rmi, ep, ti, c);
	    if (got_meta_byte) {
		got_metadata = 1;
	    }
	}
    }
}

void
close_external (External_Process** epp)
{
    External_Process* ep = *epp;
    BOOL rc;

    rc = GenerateConsoleCtrlEvent (CTRL_C_EVENT, ep->pid);
    if (!rc) {
	/* The console control event will fail for the winamp 
	   plugin.  Therefore, no choice but to kill 
	   the process using TerminateProcess()... */
	debug_print_error ();
	debug_printf ("rc = %d, gle = %d\n", rc, GetLastError());
	rc = TerminateProcess (ep->hproc, 0);
	debug_printf ("Terminated process: %d\n", rc);
    }
    CloseHandle (ep->hproc);

    free (ep);
    *epp = 0;
}

/* ----------------------------- UNIX FUNCTIONS -------------------------- */
#else

/* These functions are in either libiberty, or included in argv.c */
char** buildargv (char *sp);

External_Process*
spawn_external (char* cmd)
{
    External_Process* ep;
    int rc;

    ep = alloc_ep ();
    if (!ep) return 0;

    /* Create the pipes */
    rc = pipe (ep->mypipe);
    if (rc) {
	fprintf (stderr, "Can't open pipes\n");
	free (ep);
	return 0;
    }
    /* Create the child process. */
    ep->pid = fork ();
    if (ep->pid == (pid_t) 0) {
	/* This is the child process. */
	int i = 0;
	char** argv;

	close (ep->mypipe[0]);
	dup2 (ep->mypipe[1],1);
	close (ep->mypipe[1]);

	argv = buildargv (cmd);
	while (argv[i]) {
	    debug_printf ("argv[%d] = %s\n", i, argv[i]);
	    i++;
	}

	execvp (argv[0],&argv[0]);
	/* Doesn't return */
	fprintf (stderr, "Error, returned from execlp\n");
	exit (-1);
    } else if (ep->pid < (pid_t) 0) {
	/* The fork failed. */
	close (ep->mypipe[0]);
	close (ep->mypipe[1]);
	fprintf (stderr, "Fork failed.\n");
	free (ep);
	return 0;
    } else {
	/* This is the parent process. */
	close (ep->mypipe[1]);
	rc = fcntl (ep->mypipe[0], F_SETFL, O_NONBLOCK);
	return ep;
    }
}

int
read_external (RIP_MANAGER_INFO* rmi, External_Process* ep, TRACK_INFO* ti)
{
    char c;
    int rc;
    int got_metadata = 0;

    ti->have_track_info = 0;

    while (1) {
	rc = read (ep->mypipe[0],&c,1);
	if (rc > 0) {
	    int got_meta_byte;
	    got_meta_byte = parse_external_byte (rmi, ep, ti, c);
	    if (got_meta_byte) {
		got_metadata = 1;
	    }
	} else if (rc == 0) {
	    /* Pipe closed */
	    /* GCS FIX: Restart external program if pipe closed */
	    return 0;
	} else {
	    if (errno == EAGAIN) {
		/* Would block */
		return got_metadata;
	    }
	    /* GCS FIX: Figure out the error here. */
	    return 0;
	}
    }
}

void
close_external (External_Process** epp)
{
    int rv;
    External_Process* ep = *epp;

    printf ("I should be exiting soon...\n");
    kill (ep->pid,SIGTERM);
    usleep (0);
    if (waitpid (ep->pid,&rv,WNOHANG) == 0) {
	printf ("Waiting for cleanup\n");
	usleep (2000);
	kill (ep->pid,SIGKILL);
    }
    wait(&rv);
    free (ep);
    *epp = 0;
}
#endif