File: ledd.c

package info (click to toggle)
ledcontrol 0.5.2-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 792 kB
  • ctags: 259
  • sloc: ansic: 3,264; sh: 848; makefile: 176; perl: 49
file content (116 lines) | stat: -rw-r--r-- 2,720 bytes parent folder | download | duplicates (5)
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
/*
 * The main program.
 * Includes some odd-ball functions that don't fit anywhere else.
 */

#include "ledd.h"

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

static options *ledd_opts_new(void);

/*
 * The main function (as if you didn't know). Sets up the stuff and
 * includes the main loop.
 */
int main(int argc, char *argv[]) {
	options *opts;
	gint p;
	GMainLoop *mainloop;

	/* Set logging to STDERR. */
	log_set_facility(NULL);
	signal_ignore_abort();

	/* Parse the command line and config file options */
	opts=ledd_opts_new();
	config_cmdline(opts,argc,argv);

	/* Check for existing ledd */
	p=startup_pidfile_check(opts);
	if (p) {
		fprintf(stderr,"Another ledd process (PID ");
		if (p==1)
			fprintf(stderr,"unknown");
		else
			fprintf(stderr,"%d",p);
		fprintf(stderr,") seems to be running.\n");
		fprintf(stderr,"If it's not, delete the file %s "
			"and try again\n",opts->pidfile->name);
		abort();
	}

	/* Go into daemon mode if requested (before signal handler) */
	if (opts->daemon>0) {
		startup_fork();
	}

	/* We may now be in daemon mode, so we change logging to the
	 * requested type. If we are in daemon mode, no messages can be
	 * sent outside nicely anyway. */
	log_set_facility(opts->logging);

	/* Set the pidfile */
	startup_pidfile_write(opts);

	/* Set the signal handler */
	signal_set_handler(opts);

	/* Make the pipes */
	pipe_open_all(opts);

	/* Exec the startup script(s) */
	startup_exec_all(opts);

	g_message("up and chugging (PID %d)",getpid());

	/* Then we'll just wait... */
	/* TODO: Not entirely nice casting stuff, if someone can suggest
	 * a clean way (not having the functions cast a (gpointer) to
	 * an (options *)) to do this, please do!
	 */
	mainloop=g_main_new(FALSE);
	g_timeout_add(SLEEPTIME,(GHookCheckFunc)led_do_your_thing,opts);
	g_timeout_add(SLEEPTIME,(GHookCheckFunc)pipe_check_all,opts);
	g_timeout_add(250,(GHookCheckFunc)startup_check_all,opts);
	g_main_run(mainloop);

	g_warning("MAINLOOP exited! Internal bug! Mail the author!");
	return 0;
}




/*
 * Allocates a new options struct, sets everything to sane values
 * (all pointers to NULL, all FDs to -1) and returns it.
 */
static options *ledd_opts_new(void) {
	options *opts;
	int i,j;

	opts=g_new0(options,1);

	opts->ttys=g_slist_alloc();
	opts->pipes=g_slist_alloc();
	opts->startup=g_slist_alloc();
	opts->fixttys=g_slist_alloc();  /* tty-change */
	opts->pidfile=g_new0(File,1);
	opts->pidfile->fd=-1;
	opts->logging=g_new0(File,1);
	opts->logging->fd=-1;

	for (i=0; i<LED_MAX; i++)
		for (j=0; j<(PRIORITY_MAX+1); j++)
			opts->leds[i][j]=g_new0(LedControl,1);

	/* We'll keep it running... */
	opts->anim_timer=g_timer_new();
	g_timer_start(opts->anim_timer);

	return opts;
}