changing UID/GID after startup of pluto/charon
[strongswan.git] / src / charon / processing / scheduler.c
1 /**
2 * @file scheduler.c
3 *
4 * @brief Implementation of scheduler_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stdlib.h>
25 #include <pthread.h>
26
27 #include "scheduler.h"
28
29 #include <daemon.h>
30 #include <processing/job_queue.h>
31
32
33 typedef struct private_scheduler_t private_scheduler_t;
34
35 /**
36 * Private data of a scheduler_t object.
37 */
38 struct private_scheduler_t {
39 /**
40 * Public part of a scheduler_t object.
41 */
42 scheduler_t public;
43
44 /**
45 * Assigned thread.
46 */
47 pthread_t assigned_thread;
48 };
49
50 /**
51 * Implementation of private_scheduler_t.get_events.
52 */
53 static void get_events(private_scheduler_t * this)
54 {
55 job_t *current_job;
56
57 /* cancellation disabled by default */
58 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
59
60 DBG1(DBG_JOB, "scheduler thread running, thread_ID: %06u",
61 (int)pthread_self());
62
63 /* drop threads capabilities */
64 charon->drop_capabilities(charon, TRUE, FALSE, FALSE);
65
66 while (TRUE)
67 {
68 DBG2(DBG_JOB, "waiting for next event...");
69 /* get a job, this block until one is available */
70 current_job = charon->event_queue->get(charon->event_queue);
71 /* queue the job in the job queue, workers will eat them */
72 DBG2(DBG_JOB, "got event, adding job %N to job-queue",
73 job_type_names, current_job->get_type(current_job));
74 charon->job_queue->add(charon->job_queue, current_job);
75 }
76 }
77
78 /**
79 * Implementation of scheduler_t.destroy.
80 */
81 static void destroy(private_scheduler_t *this)
82 {
83 pthread_cancel(this->assigned_thread);
84 pthread_join(this->assigned_thread, NULL);
85 free(this);
86 }
87
88 /*
89 * Described in header.
90 */
91 scheduler_t * scheduler_create()
92 {
93 private_scheduler_t *this = malloc_thing(private_scheduler_t);
94
95 this->public.destroy = (void(*)(scheduler_t*)) destroy;
96
97 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))get_events, this) != 0)
98 {
99 /* thread could not be created */
100 free(this);
101 charon->kill(charon, "unable to create scheduler thread");
102 }
103
104 return &(this->public);
105 }