File: lib_config.c

package info (click to toggle)
kronosnet 1.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: ansic: 25,419; sh: 5,295; makefile: 664
file content (86 lines) | stat: -rw-r--r-- 1,708 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (C) 2021-2025 Red Hat, Inc.  All rights reserved.
 *
 * Authors: Fabio M. Di Nitto <fabbione@kronosnet.org>
 *
 * This software licensed under LGPL-2.0+
 */

#include "config.h"

#include <pthread.h>
#include <string.h>
#include <errno.h>

#include "internals.h"
#include "logging.h"

pthread_mutex_t handle_config_mutex = PTHREAD_MUTEX_INITIALIZER;

struct handle_tracker handle_list;
uint8_t handle_list_init = 0;

int _is_valid_handle(knet_handle_t knet_h)
{
	int found = 0;
	int savederrno = 0;
	knet_handle_t temp = NULL;

	/*
	 * we are validating the handle, hence we cannot use
	 * the handle for logging purposes
	 */
	savederrno = pthread_mutex_lock(&handle_config_mutex);
	if (savederrno) {
		errno = savederrno;
		return 0;
	}

	errno = EINVAL;
	/*
	 * this is to protect against knet_handle_free being called
	 * before knet_handle_new that initialize the list struct
	 */
	if (handle_list_init) {
		qb_list_for_each_entry(temp, &handle_list.head, list) {
			if (temp == knet_h) {
				found = 1;
				errno = 0;
			}
		}
	}

	pthread_mutex_unlock(&handle_config_mutex);

	return found;
}

pthread_rwlock_t shlib_rwlock;
static uint8_t shlib_wrlock_init = 0;

int _init_shlib_tracker(knet_handle_t knet_h)
{
	int savederrno = 0;

	if (!shlib_wrlock_init) {
		savederrno = pthread_rwlock_init(&shlib_rwlock, NULL);
		if (savederrno) {
			log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize shared lib rwlock: %s",
				strerror(savederrno));
			errno = savederrno;
			return -1;
		}
		shlib_wrlock_init = 1;
	}

	return 0;
}

void _fini_shlib_tracker(void)
{
	if (qb_list_empty(&handle_list.head)) {
		pthread_rwlock_destroy(&shlib_rwlock);
		shlib_wrlock_init = 0;
	}
	return;
}