2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "ha_sync_ctl.h"
20 #include <sys/types.h>
22 #include <sys/select.h>
28 #include <processing/jobs/callback_job.h>
30 #define HA_SYNC_FIFO IPSEC_PIDDIR "/charon.ha"
32 typedef struct private_ha_sync_ctl_t private_ha_sync_ctl_t
;
35 * Private data of an ha_sync_ctl_t object.
37 struct private_ha_sync_ctl_t
{
40 * Public ha_sync_ctl_t interface.
47 ha_sync_cache_t
*cache
;
56 * FIFO dispatching function
58 static job_requeue_t
dispatch_fifo(private_ha_sync_ctl_t
*this)
64 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &old
);
65 fifo
= open(HA_SYNC_FIFO
, O_RDONLY
);
66 pthread_setcancelstate(old
, NULL
);
69 DBG1(DBG_CFG
, "opening HA sync fifo failed: %s", strerror(errno
));
71 return JOB_REQUEUE_FAIR
;
74 memset(buf
, 0, sizeof(buf
));
75 if (read(fifo
, buf
, sizeof(buf
)-1) > 1)
77 segment
= atoi(&buf
[1]);
83 this->cache
->activate(this->cache
, segment
);
86 this->cache
->deactivate(this->cache
, segment
);
95 return JOB_REQUEUE_DIRECT
;
99 * Implementation of ha_sync_ctl_t.destroy.
101 static void destroy(private_ha_sync_ctl_t
*this)
103 this->job
->cancel(this->job
);
110 ha_sync_ctl_t
*ha_sync_ctl_create(ha_sync_cache_t
*cache
)
112 private_ha_sync_ctl_t
*this = malloc_thing(private_ha_sync_ctl_t
);
114 this->public.destroy
= (void(*)(ha_sync_ctl_t
*))destroy
;
116 if (access(HA_SYNC_FIFO
, R_OK
|W_OK
) != 0)
118 if (mkfifo(HA_SYNC_FIFO
, 600) != 0)
120 DBG1(DBG_CFG
, "creating HA sync FIFO %s failed: %s",
121 HA_SYNC_FIFO
, strerror(errno
));
126 this->job
= callback_job_create((callback_job_cb_t
)dispatch_fifo
,
128 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
129 return &this->public;