~ubuntu-branches/ubuntu/edgy/fnfx/edgy

« back to all changes in this revision

Viewing changes to src/fnfxd_init.c

  • Committer: Bazaar Package Importer
  • Author(s): Thom May
  • Date: 2005-04-01 11:39:55 UTC
  • Revision ID: james.westby@ubuntu.com-20050401113955-nnsv7bovk07obxa9
Tags: upstream-0.3
ImportĀ upstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Author: Timo Hoenig <thoenig@nouse.net>
 
3
 * Copyright (c) 2003, 2004 Timo Hoenig <thoenig@nouse.net>
 
4
 *                          All rights reserved
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2, or (at your option)
 
9
 * any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 */
 
21
 
 
22
/*
 
23
 * Defines
 
24
 *
 
25
 */
 
26
 
 
27
/*
 
28
 * Includes
 
29
 *
 
30
 */
 
31
 
 
32
#include <sys/ioctl.h>
 
33
#include <fcntl.h>
 
34
 
 
35
#include "fnfx.h"
 
36
 
 
37
/*
 
38
 * Declarations
 
39
 *
 
40
 */
 
41
 
 
42
void sigterm_handler(int signal);
 
43
int check_pid(void);
 
44
int init_sig_handler(void);
 
45
int init_fnfxd(int *, int *);
 
46
int init_shmem(t_daemon_cfg *);
 
47
int cp_cfg(t_daemon_cfg *);
 
48
int set_flags(int *, int *);
 
49
int init_pid(void);
 
50
int init_acpi(void);
 
51
int init_sem(void);
 
52
 
 
53
/*
 
54
 * External functions 
 
55
 *
 
56
 */
 
57
 
 
58
extern void debug(const char *, ...);
 
59
extern void fatal(const char *, ...);
 
60
extern int init_config(void);
 
61
 
 
62
extern t_daemon_cfg *cfg;
 
63
 
 
64
/*
 
65
 * Globals
 
66
 *
 
67
 */
 
68
 
 
69
/*
 
70
 * Functions
 
71
 *
 
72
 */
 
73
 
 
74
/*
 
75
 * sigterm_handler()
 
76
 * 
 
77
 * Signal handler for terminating signals.
 
78
 *
 
79
 * - Remove pid file
 
80
 * - Free shared memory
 
81
 */
 
82
 
 
83
void sigterm_handler(int signal)
 
84
{
 
85
    debug("Received signal %d. Terminating.", signal);
 
86
    /* destory shared memory */
 
87
    shmctl(cfg->shmem.id, IPC_RMID, cfg->shmem.addr);
 
88
    /* remove semaphore */
 
89
    semctl(cfg->sem.id, 0, IPC_RMID);
 
90
    /* remove pid file */
 
91
    unlink(PIDFILE);
 
92
    exit(255);
 
93
}
 
94
 
 
95
/*
 
96
 * init_sig_handler()
 
97
 *
 
98
 * Register the signal handler for terminating signals.a
 
99
 *
 
100
 */
 
101
 
 
102
int init_sig_handler()
 
103
{
 
104
    signal(SIGTERM, sigterm_handler);
 
105
    signal(SIGKILL, sigterm_handler);
 
106
    signal(SIGQUIT, sigterm_handler);
 
107
    signal(SIGINT, sigterm_handler);
 
108
 
 
109
    return 0;
 
110
}
 
111
 
 
112
/*
 
113
 * check_pid()
 
114
 * 
 
115
 * Check if another instance of fnfxd is running.
 
116
 * 
 
117
 */
 
118
 
 
119
int check_pid()
 
120
{
 
121
    FILE *f;
 
122
    pid_t pid, read_pid;
 
123
 
 
124
    pid = (unsigned int) getpid();
 
125
    read_pid = 0;
 
126
 
 
127
    f = fopen(PIDFILE, "r+");
 
128
 
 
129
    if (f) {
 
130
        fscanf(f, "%i", &read_pid);
 
131
        fatal
 
132
            ("According to %s, another instance of `fnfxd` is already running (PID = %i).\n"
 
133
             "If this is not the case, please remove %s.", PIDFILE, read_pid,
 
134
             PIDFILE);
 
135
        fclose(f);
 
136
        return 1;
 
137
    }
 
138
 
 
139
    return 0;
 
140
}
 
141
 
 
142
/*
 
143
 * init_shmem()
 
144
 *
 
145
 * - Initialize shared memory segment
 
146
 * - Attach shared memory segment
 
147
 *
 
148
 */
 
149
 
 
150
int init_shmem(t_daemon_cfg * config)
 
151
{
 
152
    key_t shmkey = (int) SHMKEY;
 
153
 
 
154
    config->shmem.key = shmkey;
 
155
    config->shmem.id =
 
156
        shmget(config->shmem.key, (int) SHMSIZE, IPC_CREAT | 0666);
 
157
 
 
158
    if (config->shmem.id < 0) {
 
159
        fatal("Failed to create shared memory segment.");
 
160
        return 1;
 
161
    }
 
162
 
 
163
    if ((config->shmem.addr = shmat(config->shmem.id, NULL, 0)) == (char *) -1) {
 
164
        fatal("Error while attaching shared memory segment.");
 
165
        return 1;
 
166
    }
 
167
 
 
168
    return 0;
 
169
}
 
170
 
 
171
/*
 
172
 * cp_cfg()
 
173
 *
 
174
 * - Copy configuration to shared memory segment
 
175
 * - Let the global t_daemon_cfg *cfg point to it
 
176
 *   
 
177
 */
 
178
 
 
179
int cp_cfg(t_daemon_cfg * config)
 
180
{
 
181
    memcpy((config->shmem.addr + DCFG_OFFSET), (void *) config,
 
182
           sizeof(*config));
 
183
 
 
184
    cfg = config->shmem.addr + DCFG_OFFSET;
 
185
 
 
186
    return 0;
 
187
}
 
188
 
 
189
/*
 
190
 * set_flags()
 
191
 *
 
192
 * Set the command line flags in the configuration (in memory).
 
193
 * 
 
194
 */
 
195
 
 
196
int set_flags(int *debug_flag, int *nodetach_flag)
 
197
{
 
198
    cfg->debug_flag = *debug_flag;
 
199
    cfg->nodetach_flag = *nodetach_flag;
 
200
 
 
201
    return 0;
 
202
}
 
203
 
 
204
/*
 
205
 * init_pid()
 
206
 *
 
207
 * Check for other instances of fnfxd and register PID file.
 
208
 *
 
209
 */
 
210
 
 
211
int init_pid()
 
212
{
 
213
    FILE *f;
 
214
 
 
215
    cfg->pid = getpid();
 
216
    f = fopen(PIDFILE, "w");
 
217
 
 
218
    if (f) {
 
219
        fprintf(f, "%u\n", cfg->pid);
 
220
        fclose(f);
 
221
    }
 
222
    else {
 
223
        fatal("Could not access pid file %s.", PIDFILE);
 
224
        return 1;
 
225
    }
 
226
 
 
227
    return 0;
 
228
}
 
229
 
 
230
/*
 
231
 * init_acpi()
 
232
 *
 
233
 * - Check if /proc/acpi/toshiba and subkeys are available
 
234
 *
 
235
 */
 
236
 
 
237
int init_acpi()
 
238
{
 
239
    FILE *f;
 
240
    int i;
 
241
 
 
242
    const char *acpi_proc_keys[9] = {
 
243
        ACPI_TOSHIBA,
 
244
        ACPI_FAN,
 
245
        ACPI_KEYS,
 
246
        ACPI_LCD,
 
247
        ACPI_VERSION,
 
248
        ACPI_VIDEO,
 
249
        ACPI_SLEEP,
 
250
        ACPI_CPU,
 
251
        ACPI_BLUETOOTH
 
252
    };
 
253
 
 
254
    for (i = 1; i < 7; i++) {
 
255
        if ((f = fopen(acpi_proc_keys[i], "r+"))) {
 
256
            cfg->acpi.config |= 1 << i;
 
257
            fclose(f);
 
258
        }
 
259
        else if ((i == 2) || (i == 3)) {
 
260
 
 
261
            fatal("Could open %s.", acpi_proc_keys[i]);
 
262
            fprintf(stderr,
 
263
                    "Please make sure that your kernel has enabled the Toshiba option in the ACPI section.\n"
 
264
                    "For more information read the documentation and/or http://fnfx.sf.net/index.php?section=doc#kernel.\n\n");
 
265
            return 1;
 
266
        }
 
267
    }
 
268
 
 
269
    cfg->acpi.config |= 1 << 0;
 
270
 
 
271
    return 0;
 
272
}
 
273
 
 
274
/*
 
275
 * init_sem()
 
276
 *
 
277
 * Initialize semaphores.
 
278
 *
 
279
 */
 
280
 
 
281
int init_sem()
 
282
{
 
283
    cfg->sem.key = SEMKEY;
 
284
 
 
285
    cfg->sem.id = semget(cfg->sem.key, 3, IPC_CREAT | 0666);
 
286
    if (cfg->sem.id < 0) {
 
287
        fatal("Could not create semaphore with key 0x%i.", cfg->sem.key);
 
288
        return 1;
 
289
    }
 
290
 
 
291
    if (semctl(cfg->sem.id, 0, SETVAL, 0)) {
 
292
        fatal("Could not initialize semaphore %i (id = %i) with 0.", 0,
 
293
              cfg->sem.id);
 
294
        return 1;
 
295
    }
 
296
    if (semctl(cfg->sem.id, 1, SETVAL, 0)) {
 
297
        fatal("Could not initialize semaphore %i (id = %i) with 0.", 1,
 
298
              cfg->sem.id);
 
299
        return 1;
 
300
    }
 
301
    if (semctl(cfg->sem.id, 2, SETVAL, 0)) {
 
302
        fatal("Could not initialize semaphore %i (id = %i) with 0.", 2,
 
303
              cfg->sem.id);
 
304
        return 1;
 
305
    }
 
306
 
 
307
    return 0;
 
308
}
 
309
 
 
310
/*
 
311
 * init_fnfxd()
 
312
 *
 
313
 * Call initialization routines.
 
314
 *
 
315
 */
 
316
 
 
317
int init_fnfxd(int *debug_flag, int *nodetach_flag)
 
318
{
 
319
    t_daemon_cfg config;
 
320
 
 
321
    if (check_pid())
 
322
        return 1;
 
323
 
 
324
    if (init_shmem(&config))
 
325
        return 1;
 
326
 
 
327
    if (cp_cfg(&config))
 
328
        return 1;
 
329
 
 
330
    if (set_flags(debug_flag, nodetach_flag))
 
331
        return 1;
 
332
 
 
333
    if (init_acpi())
 
334
        return 1;
 
335
 
 
336
    if (init_config())
 
337
        return 1;
 
338
 
 
339
    if (init_sem())
 
340
        return 1;
 
341
 
 
342
    return 0;
 
343
}