summaryrefslogtreecommitdiff
path: root/components/services/trilobite/libtrilobite/trilobite-core-utils.c
blob: d49befa9bf484e69ea8de2ae084158474ba4b58a (plain)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* 
 * Copyright (C) 2000 Eazel, Inc
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Authors: J Shane Culpepper <pepper@eazel.com>
 *	    Robey Pointer <robey@eazel.com>
 *
 */

/*
 * libtrilobite - Useful functions shared between all services.  This
 * includes things like xml parsing, logging, error control, and others.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gnome.h>

#ifndef TRILOBITE_SLIM 
#include <libgnomevfs/gnome-vfs.h>
#include <liboaf/liboaf.h>
#include <bonobo.h>
#else /* TRILOBITE_SLIM */
#include <ghttp.h>
#endif /* TRILOBITE_SLIM */

#include "trilobite-core-utils.h"

#define TRILOBITE_SERVICE_CONFIG_DIR "/etc/trilobite"
#define TRILOBITE_SERVICE_CONFIG_DIR_ENV "TRILOBITE_CONFIG"

#ifdef OPEN_MAX
#define LAST_FD OPEN_MAX
#else
#define LAST_FD 1024
#endif

/* better/safer replacement for popen (which doesn't exist on all platforms, anyway).
 * forks and executes a specific program, with pipes carrying stdin/stdout/stderr back
 * to you.  this way, you can fork off a subprocess and control its environment. and
 * it doesn't use system() like most evil popen implementations.
 * for the future, it'd be nice to be able to clear out the subprocess's env vars.
 */
int
trilobite_pexec (const char *path, char * const argv[], int *stdin_fd, int *stdout_fd, int *stderr_fd)
{
	pid_t child;
	int pipe_in[2], pipe_out[2], pipe_err[2];
	int i;

	pipe_in[0] = pipe_in[1] = pipe_out[0] = pipe_out[1] = pipe_err[0] = pipe_err[1] = -1;
	if ((pipe (pipe_in) != 0) || (pipe (pipe_out) != 0) || (pipe (pipe_err) != 0)) {
		goto close_and_give_up;
	}
	child = fork ();
	if (child < 0) {
		goto close_and_give_up;
	}

	if (child == 0) {
#if 0
		child = fork ();
		if (child != 0) {
			exit (0);
		}

		/* keep child processes from trying to write to the tty */
		setsid ();
		setpgid (0, 0);
#endif

		/* make stdin/stdout/stderr use the pipes */
		if (stdin_fd) {
			dup2 (pipe_in[0], 0);
		}
		if (stdout_fd) {
			dup2 (pipe_out[1], 1);
		}
		if (stderr_fd) {
			dup2 (pipe_err[1], 2);
		}
		/* close all open fd's */
		for (i = 3; i < LAST_FD; i++) {
			close(i);
		}

		/* FIXME bugzilla.eazel.com 2589: might we want to specify our own environment here? */
		execv (path, argv);

		/* if we get here, then somehow, exec failed */
		exit (-1);
	}

	/* copy out all the in/out/err fd's */
	close (pipe_in[0]);
	close (pipe_out[1]);
	close (pipe_err[1]);
	if (stdin_fd) {
		*stdin_fd = pipe_in[1];
	} else {
		close (pipe_in[1]);
	}
	if (stdout_fd) {
		*stdout_fd = pipe_out[0];
	} else {
		close (pipe_out[0]);
	}
	if (stderr_fd) {
		*stderr_fd = pipe_err[0];
	} else {
		close (pipe_err[0]);
	}

	return (int)child;

close_and_give_up:
	close (pipe_in[0]);
	close (pipe_in[1]);
	close (pipe_out[0]);
	close (pipe_out[1]);
	close (pipe_err[0]);
	close (pipe_err[1]);

	return 0;
}


static FILE *saved_logf = NULL;
static int do_debug_log = 0;

/* handler for trapping g_log/g_warning/g_error/g_message stuff, and sending it to
 * a standard logfile.
 */
static void
trilobite_add_log (const char *domain, GLogLevelFlags flags, const char *message, FILE *logf)
{
	char *prefix;
	g_assert (logf != NULL);

	if (flags & G_LOG_LEVEL_DEBUG) {
		if (do_debug_log) {
			prefix = "/// debug:";
		} else {
			return;
		}
	} else if (flags & G_LOG_LEVEL_MESSAGE) {
		prefix = "---";
	} else if (flags & G_LOG_LEVEL_WARNING) {
		prefix = "*** warning:";
	} else if (flags & G_LOG_LEVEL_ERROR) {
		prefix = "!!! ERROR:";
	} else {
		prefix = "???";
	}

	fprintf (logf, "%s %s\n", prefix, message);
	fflush (logf);
}

static void
trilobite_close_log (void)
{
	if (saved_logf != NULL) {
		fclose (saved_logf);
	}
}


#ifndef TRILOBITE_SLIM
static GnomeVFSHandle *
trilobite_open_uri (const char *uri_text)
{
	GnomeVFSResult err;
	GnomeVFSURI *uri;
	GnomeVFSHandle *handle = NULL;

	if (! gnome_vfs_initialized ()) {
		setenv ("GNOME_VFS_HTTP_USER_AGENT", trilobite_get_useragent_string (FALSE, NULL), 1);

		if (! gnome_vfs_init ()) {
			g_warning ("cannot initialize gnome-vfs!");
			return NULL;
		}
	}

	uri = gnome_vfs_uri_new (uri_text);
	if (uri == NULL) {
		trilobite_debug ("fetch-uri: invalid uri");
		return NULL;
	}

	err = gnome_vfs_open_uri (&handle, uri, GNOME_VFS_OPEN_READ);
	if (err != GNOME_VFS_OK) {
		trilobite_debug ("fetch-uri: open failed: %s", gnome_vfs_result_to_string (err));
		handle = NULL;
	}

	gnome_vfs_uri_unref (uri);
	return handle;
}
#endif /* TRILOBITE_SLIM */

#ifndef TRILOBITE_SLIM
/* fetch a file from an url, using gnome-vfs
 * (using gnome-vfs allows urls of the type "eazel-auth:/etc" to work)
 * generally this will be used to fetch XML files.
 * on success, the body will be null-terminated (this helps work around bugs in libxml,
 * and also makes it easy to manipulate a small body using string operations).
 */
gboolean
trilobite_fetch_uri (const char *uri_text, char **body, int *length)
{
	GnomeVFSResult err;
	GnomeVFSHandle *handle;
	int buffer_size;
	GnomeVFSFileSize bytes;

	handle = trilobite_open_uri (uri_text);
	if (handle == NULL) {
		return FALSE;
	}

	/* start the buffer at a reasonable size */
	buffer_size = 4096;
	*body = g_malloc (buffer_size);
	*length = 0;

	while (1) {
		g_main_iteration (FALSE);
		err = gnome_vfs_read (handle, (*body) + (*length), buffer_size - (*length), &bytes);
		if ((bytes == 0) || (err != GNOME_VFS_OK)) {
			break;
		}
		*length += bytes;
		if (*length >= buffer_size - 64) {
			/* expando time! */
			buffer_size *= 2;
			*body = g_realloc (*body, buffer_size);
		}
	}

	if (err != GNOME_VFS_OK) {
		g_free (*body);
		*body = NULL;
		goto fail;
	}

	(*body)[*length] = 0;
	gnome_vfs_close (handle);
	return TRUE;

fail:
	trilobite_debug ("fetch-uri: %s", gnome_vfs_result_to_string (err));
	gnome_vfs_close (handle);
	return FALSE;
}
#endif /* TRILOBITE_SLIM */

#ifndef TRILOBITE_SLIM
gboolean
trilobite_fetch_uri_to_file (const char *uri_text, const char *filename)
{
	GnomeVFSResult err;
	GnomeVFSHandle *handle;
	FILE *file;
	char buffer[1024];
	GnomeVFSFileSize bytes;

	file = fopen (filename, "w");
	if (file == NULL) {
		return FALSE;
	}

	handle = trilobite_open_uri (uri_text);
	if (handle == NULL) {
		fclose (file);
		return FALSE;
	}

	while (1) {
		g_main_iteration (FALSE);
		err = gnome_vfs_read (handle, buffer, sizeof(buffer), &bytes);
		if ((bytes == 0) || (err != GNOME_VFS_OK)) {
			break;
		}
		fwrite (buffer, bytes, 1, file);
	}

	gnome_vfs_close (handle);
	fclose (file);

	return (err == GNOME_VFS_OK);
}
#endif /* TRILOBITE_SLIM */

#ifdef TRILOBITE_SLIM
gboolean trilobite_fetch_uri (const char *uri_text, 
			      char **body, 
			      int *length)
{
	char *uri = NULL;
        ghttp_request* request;
        ghttp_status status;
	gboolean result = TRUE;

	g_assert (body!=NULL);
	g_assert (uri_text != NULL);
	g_assert (length != NULL);

	uri = g_strdup (uri_text);
        request = NULL;
        (*length) = -1;
        (*body) = NULL;

        if ((request = ghttp_request_new())==NULL) {
                g_warning (_("Could not create an http request !"));
                result = FALSE;
        } 

        if (result && (ghttp_set_uri (request, uri) != 0)) {
                g_warning (_("Invalid uri !"));
                result = FALSE;
        }

	if (result) {
		ghttp_set_header (request, http_hdr_Connection, "close");
		ghttp_set_header (request, http_hdr_User_Agent, trilobite_get_useragent_string (FALSE, NULL));
	}

        if (result && (ghttp_prepare (request) != 0)) {
                g_warning (_("Could not prepare http request !"));
                result = FALSE;
        }

        if (result && ghttp_set_sync (request, ghttp_async)) {
                g_warning (_("Couldn't get async mode "));
                result = FALSE;
        }

        while (result && (status = ghttp_process (request)) == ghttp_not_done) {
                ghttp_current_status curStat = ghttp_get_status (request);
		g_main_iteration (FALSE);
        }

        if (result && (ghttp_status_code (request) != 200)) {
                g_warning (_("HTTP error: %d %s"), ghttp_status_code (request),
			   ghttp_reason_phrase (request));
                result = FALSE;
        }
	if (result && (ghttp_status_code (request) != 404)) {
		(*length) = ghttp_get_body_len (request);
		(*body) = g_new0 (char, *length + 1);
		memcpy (*body, ghttp_get_body (request), *length);
		(*body)[*length] = 0;
	} else {
		result = FALSE;
	}

        if (request) {
                ghttp_request_destroy (request);
        }
	
	g_free (uri);

	return result;
}
#endif /* TRILOBITE_SLIM */

#ifdef TRILOBITE_SLIM
gboolean trilobite_fetch_uri_to_file (const char *uri_text, 
				      const char *filename)
{
	char *body = NULL;
	int length;
	gboolean result = FALSE;

	result =  trilobite_fetch_uri (uri_text, &body, &length);
	if (result) {
		FILE* file;
		file = fopen (filename, "wb");
		if (file == NULL) {
			g_warning (_("Could not open target file %s"),filename);
			result = FALSE;
		} else {
			fwrite (body, length, 1, file);
		}
	} 

	return result;	
}
#endif /* TRILOBITE_SLIM */

#ifndef TRILOBITE_SLIM
/* trilobite_init -- does all the init stuff 
 * FIXME bugzilla.eazel.com 1656:
 * for now, this requires init_gnome, and thus a running X server.
 * initializes OAF and bonobo, too.
 *
 * service_name should be your G_LOG_DOMAIN, if you set one, because of the way logging works
 * 
 * options is a way to add extra parameters in the future (can be NULL for now).
 *
 * returns FALSE if init fails.
 */
gboolean
trilobite_init (const char *service_name, const char *version_name, const char *log_filename,
		int argc, char **argv, GData *options)
{
	CORBA_ORB orb;
	FILE *logf;

	/* for future reference:
	 * possible to avoid gtk_init (which requires X) by using gtk_type_init(), gtk_signal_init()
	 * according to george.
	 * gtk_type_init ();
	 * gnomelib_init ("trilobite-eazel-install-service-factory", "0.1");
	 * gnomelib_register_popt_table (oaf_popt_options, "Trilobite-Eazel-Install-Server");
	 * orb = oaf_init (argc, argv);
	 * gnomelib_parse_args (argc, argv, 0);
	 */
	gnome_init_with_popt_table (service_name, version_name, argc, argv, oaf_popt_options, 0, NULL);
	orb = oaf_init (argc, argv);

	if (!bonobo_init (orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL)) {
		g_error (_("Could not initialize Bonobo"));
		goto fail;
	}

	if (log_filename != NULL) {
		logf = fopen (log_filename, "wt");
		if (logf != NULL) {
			g_log_set_handler (service_name, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING |
					   G_LOG_LEVEL_ERROR | G_LOG_LEVEL_DEBUG,
					   (GLogFunc)trilobite_add_log, logf);
			/* send libtrilobite messages there, too */
			g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING |
					   G_LOG_LEVEL_ERROR | G_LOG_LEVEL_DEBUG,
					   (GLogFunc)trilobite_add_log, logf);
		} else {
			g_warning (_("Can't write logfile %s -- using default log handler"), log_filename);
		}
	}

	g_atexit (trilobite_close_log);

	if (g_datalist_get_data (&options, "debug") != NULL) {
		/* debug mode on */
		do_debug_log = 1;
	}

	return TRUE;

fail:
	return FALSE;
}
#endif /* TRILOBITE_SLIM */

const char *
trilobite_get_useragent_string (gboolean version, char *suffix)
{
	static char *result = NULL;

	g_free (result);
	result = g_strdup_printf ("Trilobite/%s%s%s", 
				  version ? "/" TRILOBITE_VERSION : suffix ? "/" : "" , 
				  suffix ? "/" : "", 
				  suffix ? suffix : "");
	return result;
}

const char *
trilobite_get_config_dir_string ()
{
	static const char *res = NULL;

	if (res!=NULL) {
		return res;
	}

	if (getenv (TRILOBITE_SERVICE_CONFIG_DIR_ENV)) {
		res = g_strdup (getenv (TRILOBITE_SERVICE_CONFIG_DIR_ENV));
	} else {
		res = g_strdup (TRILOBITE_SERVICE_CONFIG_DIR);
	}
	
	return res;
}

/* copied from libnautilus-extensions */
gboolean
trilobite_setenv (const char *name, const char *value, gboolean overwrite)
{
#if defined (HAVE_SETENV)
	return (setenv (name, value, overwrite) == 0);
#else
	char *string;

	if (! overwrite && g_getenv (name) != NULL) {
		return FALSE;
	}

	/* This results in a leak when you overwrite existing
	 * settings. It would be fairly easy to fix this by keeping
	 * our own parallel array or hash table.
	 */
	string = g_strconcat (name, "=", value, NULL);
	return putenv (string);
#endif
}