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 <sys/types.h>
20 #include <sys/select.h>
25 #include <threading/thread.h>
26 #include <processing/jobs/callback_job.h>
28 #define HA_FIFO IPSEC_PIDDIR "/charon.ha"
30 typedef struct private_ha_ctl_t private_ha_ctl_t
;
33 * Private data of an ha_ctl_t object.
35 struct private_ha_ctl_t
{
38 * Public ha_ctl_t interface.
45 ha_segments_t
*segments
;
48 * Resynchronization message cache
59 * FIFO dispatching function
61 static job_requeue_t
dispatch_fifo(private_ha_ctl_t
*this)
68 oldstate
= thread_cancelability(TRUE
);
69 fifo
= open(HA_FIFO
, O_RDONLY
);
70 thread_cancelability(oldstate
);
73 DBG1(DBG_CFG
, "opening HA fifo failed: %s", strerror(errno
));
75 return JOB_REQUEUE_FAIR
;
78 memset(buf
, 0, sizeof(buf
));
79 if (read(fifo
, buf
, sizeof(buf
)-1) > 1)
81 segment
= atoi(&buf
[1]);
87 this->segments
->activate(this->segments
, segment
, TRUE
);
90 this->segments
->deactivate(this->segments
, segment
, TRUE
);
93 this->cache
->resync(this->cache
, segment
);
102 return JOB_REQUEUE_DIRECT
;
105 METHOD(ha_ctl_t
, destroy
, void,
106 private_ha_ctl_t
*this)
108 this->job
->cancel(this->job
);
115 ha_ctl_t
*ha_ctl_create(ha_segments_t
*segments
, ha_cache_t
*cache
)
117 private_ha_ctl_t
*this;
124 .segments
= segments
,
128 if (access(HA_FIFO
, R_OK
|W_OK
) != 0)
130 old
= umask(~(S_IRWXU
| S_IRWXG
));
131 if (mkfifo(HA_FIFO
, S_IRUSR
| S_IWUSR
) != 0)
133 DBG1(DBG_CFG
, "creating HA FIFO %s failed: %s",
134 HA_FIFO
, strerror(errno
));
138 if (chown(HA_FIFO
, charon
->uid
, charon
->gid
) != 0)
140 DBG1(DBG_CFG
, "changing HA FIFO permissions failed: %s",
144 this->job
= callback_job_create_with_prio((callback_job_cb_t
)dispatch_fifo
,
145 this, NULL
, NULL
, JOB_PRIO_CRITICAL
);
146 lib
->processor
->queue_job(lib
->processor
, (job_t
*)this->job
);
147 return &this->public;