summaryrefslogtreecommitdiff
path: root/avsys-audio-sync.c
blob: f0db33857151b9983522f82272359408ae91f428 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * avsystem
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

#include "avsys-common.h"
#include "avsys-audio-sync.h"
#include "avsys-error.h"
#include "avsys-debug.h"
#include "avsys-audio-shm.h"
#include "avsys-audio-path.h"

static avsys_sync_param_t g_presettings[AVSYS_AUDIO_SYNC_IDEN_CNT] = {
	{"audio_handle_lock", AVSYS_KEY_PREFIX_GEN(AVSYS_KEY_PREFIX_AUDIO, AVSYS_AUDIO_SYNC_IDEN_HANDLE)},
	{"audio_path_lock", AVSYS_KEY_PREFIX_GEN(AVSYS_KEY_PREFIX_AUDIO, AVSYS_AUDIO_SYNC_IDEN_PATH)},
};

int avsys_audio_create_sync(const avsys_audio_sync_iden_t iden)
{
	if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden)
		return AVSYS_STATE_ERR_INVALID_PARAMETER;
	return avsys_create_sync(&g_presettings[iden]);
}

int avsys_audio_remove_sync(const avsys_audio_sync_iden_t iden)
{
	if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden)
		return AVSYS_STATE_ERR_INVALID_PARAMETER;
	return avsys_remove_sync(&g_presettings[iden]);
}

int avsys_audio_lock_sync(const avsys_audio_sync_iden_t iden)
{
	pid_t pid = -1;
	int index = 0;
	int err = AVSYS_STATE_SUCCESS;

	if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden) {
		return AVSYS_STATE_ERR_INVALID_PARAMETER;
	}
	//avsys_info(AVAUDIO,"lock[%d]\n",(int)iden);

	err = avsys_lock_sync(&g_presettings[iden]);

	pid = getpid();
	if ((iden == AVSYS_AUDIO_SYNC_IDEN_PATH) && (err == AVSYS_STATE_SUCCESS)) {
		avsys_audio_path_ex_info_t *control = NULL;
		avsys_audio_path_ex_info_t **temp = NULL;

		temp = &control;
		if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_PATH, (void **)temp))) {
			avsys_error_r(AVAUDIO, "avsys_audio_get_shm() for path failed in %s\n", __func__);
			return AVSYS_STATE_ERR_INTERNAL;
		}

		do {
			/* Add mark */
			if (control->pathlock_pid[index] < 0) {	/* find empty slot */
				control->pathlock_pid[index] = pid;
				break;
			}
			index++;
			if (index == AVSYS_AUDIO_LOCK_SLOT_MAX) {
				int i;
				avsys_critical(AVAUDIO, "path lock pid slot is full. print stored pid before clear all slot\n");
				/* print and cleanup all stored pid */
				for (i = 0; i < AVSYS_AUDIO_LOCK_SLOT_MAX; i++) {
					avsys_critical(AVAUDIO, "path lock pid : %d\n", control->pathlock_pid[i]);
					control->pathlock_pid[i] = -1;
				}
				control->pathlock_pid[0] = pid; /* add current pid */
			}
		} while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
	} else if ((iden == AVSYS_AUDIO_SYNC_IDEN_HANDLE) && (err == AVSYS_STATE_SUCCESS)) {
		avsys_audio_handle_info_t *control = NULL;
		avsys_audio_handle_info_t **temp = NULL;

		temp = &control;
		if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_HANDLE, (void **)temp))) {
			avsys_error(AVAUDIO, "avsys_audio_get_shm() for handle failed in %s\n", __func__);
			return AVSYS_STATE_ERR_INTERNAL;
		}

		do {
			/* Add mark */
			if (control->handlelock_pid[index] < 0) {	/* find empty slot */
				control->handlelock_pid[index] = pid;
				break;
			}
			index++;
			if (index == AVSYS_AUDIO_LOCK_SLOT_MAX) {
				int i;
				avsys_critical(AVAUDIO, "handle lock pid slot is full. print stored pid before clear all slot\n");
				/* print and cleanup all stored pid */
				for (i = 0; i < AVSYS_AUDIO_LOCK_SLOT_MAX; i++) {
					avsys_critical(AVAUDIO, "handle lock pid : %d\n", control->handlelock_pid[i]);
					control->handlelock_pid[i] = -1;
				}
				control->handlelock_pid[0] = pid; /*add current pid */
			}
		} while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
	}
	return err;
}

int avsys_audio_unlock_sync(const avsys_audio_sync_iden_t iden)
{
	pid_t pid = -1;
	int index = 0;
	int err = AVSYS_STATE_SUCCESS;

	if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden) {
		return AVSYS_STATE_ERR_INVALID_PARAMETER;
	}
	//avsys_info(AVAUDIO,"unlock[%d]\n",(int)iden);

	err = avsys_unlock_sync(&g_presettings[iden]);

	pid = getpid();
	if ((iden == AVSYS_AUDIO_SYNC_IDEN_PATH) && (err == AVSYS_STATE_SUCCESS)) {
		avsys_audio_path_ex_info_t *control = NULL;
		avsys_audio_path_ex_info_t **temp = NULL;

		temp = &control;
		if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_PATH, (void **)temp))) {
			avsys_error_r(AVAUDIO, "avsys_audio_get_shm() failed in %s\n", __func__);
			return AVSYS_STATE_ERR_INTERNAL;
		}

		do {
			/* Remove mark */
			if (control->pathlock_pid[index] == pid) {	/* find empty slot */
				control->pathlock_pid[index] = -1;
				break;
			}
			index++;
			if (index == AVSYS_AUDIO_LOCK_SLOT_MAX)
				avsys_error(AVAUDIO, "Can not find pid (%d) in path_lock_pid slot\n", pid);
		} while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
	} else if ((iden == AVSYS_AUDIO_SYNC_IDEN_HANDLE) && (err == AVSYS_STATE_SUCCESS)) {
		avsys_audio_handle_info_t *control = NULL;
		avsys_audio_handle_info_t **temp = NULL;

		temp = &control;
		if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_HANDLE, (void **)temp))) {
			avsys_error(AVAUDIO, "avsys_audio_get_shm() for handle failed in %s\n", __func__);
			return AVSYS_STATE_ERR_INTERNAL;
		}

		do {
			/* Add mark */
			if (control->handlelock_pid[index] == pid) {	/* find empty slot */
				control->handlelock_pid[index] = -1;
				break;
			}
			index++;
			if (index == AVSYS_AUDIO_LOCK_SLOT_MAX)
				avsys_error(AVAUDIO, "Can not find pid (%d) in handlelock_pid slot\n", pid);
		} while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
	}

	return err;
}

/* find pid information in proc filesystem */
/* if pid exist, return AVSYS_STATE_SUCCESS */
/* if pid does not exist, return AVSYS_STATE_ERR_ALLOCATION */
int avsys_check_process(int check_pid)
{
	DIR *dir = NULL;
	char check_path[128] = "";
	int exist = AVSYS_STATE_SUCCESS;

	memset(check_path, '\0', sizeof(check_path));
	snprintf(check_path, sizeof(check_path) - 1, "/proc/%d", check_pid);

	dir = opendir(check_path);
	if (dir == NULL) {
		switch (errno) {
		case ENOENT:
			avsys_error(AVAUDIO, "pid %d does not exist anymore\n", check_pid);
			exist = AVSYS_STATE_ERR_ALLOCATION;
			break;
		case EACCES:
			avsys_error(AVAUDIO, "Permission denied\n");
			break;
		case EMFILE:
			avsys_error(AVAUDIO, "Too many file descriptors in use by process\n");
			break;
		case ENFILE:
			avsys_error(AVAUDIO, "Too many files are currently open in the system\n");
			break;
		default:
			avsys_error(AVAUDIO, "Other error : %d\n", errno);
			break;
		}
	} else {
		avsys_warning(AVAUDIO, "pid : %d still alive\n", check_pid);
		if (-1 == closedir(dir)) {
			avsys_error(AVAUDIO, "[%s] closedir failed with errno : %d\n", __func__, errno);
		}
	}
	return exist;
}

int avsys_audio_dump_sync(void)
{
	pid_t pid = -1;
	int index = 0;
	int err = AVSYS_STATE_SUCCESS;
	int i = 0;
	int sem_value = 0;

	fprintf(stdout, "Dump sync : Start\n");
	for (i = 0; i < AVSYS_AUDIO_SYNC_IDEN_CNT; i++) {
		err = avsys_dump_sync(&g_presettings[i]);
	}
	fprintf(stdout, "Dump sync : End\n");
	return err;
}