Annotation of embedaddon/freevrrpd/vrrp_thread.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (c) 2001,2002 Sebastien Petit <spe@bsdfr.org>
3: *
4: * Redistribution and use in source forms, with and without modification,
5: * are permitted provided that the following conditions are met:
6: * 1. Redistributions of source code must retain the above copyright notice,
7: * this list of conditions and the following disclaimer.
8: * 2. Redistributions in binary form must reproduce the above copyright notice,
9: * this list of conditions and the following disclaimer in the documentation
10: * and/or other materials provided with the distribution. Obviously, it
11: * would be nice if you gave credit where credit is due but requiring it
12: * would be too onerous.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by Sebastien Petit.
16: * 4. Neither the name of its contributors may be used to endorse or promote
17: * products derived from this software without specific prior written
18: * permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: *
32: * $Id: vrrp_thread.c,v 1.4 2004/03/05 22:06:45 spe Exp $
33: */
34:
35: #include <stdlib.h>
36: #include <errno.h>
37: #include <string.h>
38: #include "vrrp_thread.h"
39: #include "vrrp_moncircuit.h"
40:
41: pthread_mutex_t pth_mutex, pth_mutex_monitor;
42:
43: void
44: vrrp_thread_mutex_lock(void)
45: {
46: pthread_mutex_lock(&pth_mutex);
47:
48: return;
49: }
50:
51: void
52: vrrp_thread_mutex_unlock(void)
53: {
54: pthread_mutex_unlock(&pth_mutex);
55:
56: return;
57: }
58:
59: void vrrp_thread_mutex_lock_monitor(void)
60: {
61: pthread_mutex_lock(&pth_mutex_monitor);
62:
63: return;
64: }
65:
66: void vrrp_thread_mutex_unlock_monitor(void)
67: {
68: pthread_mutex_unlock(&pth_mutex_monitor);
69:
70: return;
71: }
72:
73: void *
74: vrrp_thread_launch_vrrprouter(void *args)
75: {
76: int **args2 = (int **)args;
77: struct vrrp_vr *vr = (struct vrrp_vr *)args2[0];
78: sem_t *sem = (sem_t *)args2[1];
79: int returnCode = 0;
80:
81: vr_ptr[vr_ptr_pos] = vr;
82: vr_ptr_pos++;
83: if (vr_ptr_pos == 255) {
84: syslog(LOG_ERR, "cannot configure more than 255 VRID... exiting\n");
85: exit(-1);
86: }
87: sem_post(sem);
88: for (;;) {
89: switch (vr->state) {
90: case VRRP_STATE_INITIALIZE:
91: returnCode = vrrp_state_initialize(vr);
92: break;
93: case VRRP_STATE_MASTER:
94: returnCode = vrrp_state_master(vr);
95: break;
96: case VRRP_STATE_BACKUP:
97: returnCode = vrrp_state_backup(vr);
98: break;
99: }
100: if (returnCode < 0) {
101: syslog(LOG_ERR, "vrid [%d] Cannot reach the correct state, disabled: %s\n", vr->vr_id, strerror(errno));
102: pthread_exit(NULL);
103: }
104: }
105:
106: /* Normally never executed */
107: return NULL;
108: }
109:
110: char
111: vrrp_thread_initialize(void)
112: {
113: if (pthread_mutex_init(&pth_mutex, NULL) != 0) {
114: syslog(LOG_ERR, "can't initialize thread for socket reading [ PTH_MUTEX, NULL ]");
115: return -1;
116: }
117: if (pthread_mutex_init(&pth_mutex_monitor, NULL) != 0) {
118: syslog(LOG_ERR, "can't initialize thread for socket reading [ PTH_MUTEX, NULL ]");
119: return -1;
120: }
121: return 0;
122: }
123:
124: char
125: vrrp_thread_create_vrid(struct vrrp_vr * vr)
126: {
127: pthread_t pth;
128: pthread_attr_t pth_attr;
129: sem_t sem;
130: void *args[2];
131:
132: if (sem_init(&sem, 0, 0) == -1) {
133: syslog(LOG_ERR, "can't initialize an unnamed semaphore [ SEM, 0, 0 ]");
134: return -1;
135: }
136: if (pthread_attr_init(&pth_attr) != 0) {
137: syslog(LOG_ERR, "can't initialize thread attributes [ PTH_ATTR ]");
138: return -1;
139: }
140: if (pthread_attr_setdetachstate(&pth_attr, PTHREAD_CREATE_DETACHED) != 0) {
141: syslog(LOG_ERR, "can't set thread attributes [ PTH_ATTR, PTHREAD_CREATE_DETACHED ]");
142: return -1;
143: }
144: args[0] = vr;
145: args[1] = &sem;
146: if (pthread_create(&pth, &pth_attr, vrrp_thread_launch_vrrprouter, args) != 0) {
147: syslog(LOG_ERR, "can't create new thread [ PTH, PTH_ATTR, VRRP_THREAD_READ_SOCKET ]");
148: return -1;
149: }
150: sem_wait(&sem);
151: sem_destroy(&sem);
152:
153: return 0;
154: }
155:
156: int vrrp_thread_create_moncircuit(void)
157: {
158: pthread_t pth;
159: pthread_attr_t pth_attr;
160: sem_t sem;
161: int delay = VRRP_MONCIRCUIT_MONDELAY;
162: void *args[2];
163:
164: if (sem_init(&sem, 0, 0) == -1) {
165: syslog(LOG_ERR, "can't initialize an unnamed semaphore [ SEM, 0, 0 ]");
166: return -1;
167: }
168: if (pthread_attr_init(&pth_attr) != 0) {
169: syslog(LOG_ERR, "can't initialize thread attributes [ PTH_ATTR ]");
170: return -1;
171: }
172: if (pthread_attr_setdetachstate(&pth_attr, PTHREAD_CREATE_DETACHED) != 0) {
173: syslog(LOG_ERR, "can't set thread attributes [ PTH_ATTR, PTHREAD_CREATE_DETACHED ]");
174: return -1;
175: }
176: args[0] = &delay;
177: args[1] = &sem;
178: if (pthread_create(&pth, &pth_attr, vrrp_moncircuit_monitor_thread, args) != 0) {
179: syslog(LOG_ERR, "can't create new thread [ PTH, PTH_ATTR, VRRP_THREAD_READ_SOCKET ]");
180: return -1;
181: }
182: sem_wait(&sem);
183: sem_destroy(&sem);
184:
185: return 0;
186: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>