4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "avsys-common.h"
23 #include "avsys-debug.h"
24 #include "avsys-error.h"
30 #include <sys/types.h>
38 #include <semaphore.h>
40 void * avsys_malloc(size_t size)
43 allocated = (char*) malloc(size + 12);
48 memset(allocated, 0, size+8);
49 memcpy(allocated, AVSYS_MAGIC_START, 4);
50 (*(int*)(allocated+4)) = size;
51 memcpy(allocated+size+8, AVSYS_MAGIC_END, 4);
53 return (void*)(allocated + 8);
56 void avsys_free(void *ptr)
58 char *allocated = (char*)ptr;
60 if(avsys_mem_check(allocated)) {
66 int avsys_create_shm(const avsys_shm_param_t* param)
73 return AVSYS_STATE_ERR_NULL_POINTER;
75 if (param->size < 1 || param->key_path == NULL || access(param->key_path, R_OK) != 0) {
76 return AVSYS_STATE_ERR_INVALID_VALUE;
79 key = ftok(param->key_path, param->key_prefix);
81 if ((shmid = shmget(key, param->size, IPC_CREAT|IPC_EXCL|0666)) == -1) {
82 if (errno == EEXIST) {
83 avsys_error(AVAUDIO,"Already initialized.\n");
84 if ((shmid = shmget(key, param->size, 0)) == -1) {
85 avsys_error(AVAUDIO, "Initialization fail.\n");
87 segptr = shmat(shmid, 0, 0);
88 avsys_assert_r(segptr != NULL);
92 avsys_error_r(AVAUDIO, "Require ROOT permission.\n");
93 else if(errno == ENOMEM)
94 avsys_critical_r(AVAUDIO, "System memory is empty.\n");
95 else if(errno == ENOSPC)
96 avsys_critical_r(AVAUDIO, "Resource is empty.\n");
99 shmctl(shmid, SHM_LOCK, 0);
100 segptr = shmat(shmid, 0, 0);
101 avsys_assert_r(segptr != NULL);
102 memset((void*)segptr, 0, param->size);
106 return AVSYS_STATE_SUCCESS;
108 return AVSYS_STATE_ERR_INTERNAL;
112 int avsys_remove_shm(const avsys_shm_param_t* param)
118 return AVSYS_STATE_ERR_NULL_POINTER;
120 if (param->size < 1 || param->key_path == NULL || access(param->key_path, R_OK) != 0) {
121 return AVSYS_STATE_ERR_INVALID_VALUE;
124 key = ftok(param->key_path, param->key_prefix);
126 if ((shmid = shmget(key, param->size, 0)) == -1) {
128 avsys_error_r(AVAUDIO, "Not initialized.\n");
129 else if(errno == EACCES)
130 avsys_error_r(AVAUDIO, "Require ROOT permission.\n");
131 else if(errno == ENOSPC)
132 avsys_critical_r(AVAUDIO, "Resource is empty.\n");
134 avsys_assert_r(shmctl(shmid, IPC_RMID, 0) == 0);
138 return AVSYS_STATE_SUCCESS;
140 return AVSYS_STATE_ERR_INTERNAL;
144 void* avsys_get_shm(const avsys_shm_param_t* param)
153 if (param->size < 1 || param->key_path == NULL || access(param->key_path, R_OK) != 0) {
157 key = ftok(param->key_path, param->key_prefix);
159 if ((shmid = shmget(key, param->size, 0)) == -1) {
161 avsys_error_r(AVAUDIO, "Not initialized.\n");
162 else if(errno == EACCES)
163 avsys_error_r(AVAUDIO, "Require ROOT permission.\n");
164 else if(errno == ENOSPC)
165 avsys_critical_r(AVAUDIO, "Resource is empty.\n");
169 ptr = shmat(shmid, 0, 0);
170 if(ptr == (void*)-1) {
172 avsys_error_r(AVAUDIO,"no permission\n");
173 else if(errno == EINVAL)
174 avsys_error_r(AVAUDIO,"invalid shmid\n");
175 else if(errno == ENOMEM)
176 avsys_error_r(AVAUDIO,"can not allocate memory\n");
178 avsys_error_r(AVAUDIO,"shmat() failed %d\n", errno);
184 int avsys_create_sync(const avsys_sync_param_t *param)
187 sem = sem_open(param->key_path, O_CREAT, 0666, 1);
188 if (sem == SEM_FAILED) {
193 "The semaphore already exist, but caller does not have permission %s\n",param->key_path);
196 avsys_error(AVAUDIO,"Insufficient memory in %s (%d)\n",__func__,__LINE__);
199 avsys_error(AVAUDIO,"Too many open files in system %s (%d)\n",__func__,__LINE__);
202 avsys_critical(AVAUDIO, "Semaphore create fail! (name:%s, errno %d)\n", param->key_path, errno);
205 return AVSYS_STATE_ERR_INTERNAL;
207 return AVSYS_STATE_SUCCESS;
210 int avsys_remove_sync(const avsys_sync_param_t *param)
214 err = sem_unlink(param->key_path);
216 avsys_critical(AVAUDIO, "Semaphore destroy Fail! (name:%s, errno %d)\n", param->key_path, errno);
217 return AVSYS_STATE_ERR_INTERNAL;
220 return AVSYS_STATE_SUCCESS;
223 int avsys_lock_sync(const avsys_sync_param_t *param)
227 int err = AVSYS_STATE_SUCCESS;
228 struct timespec wait_time;
230 sem = sem_open(param->key_path, O_CREAT, 0666, 1);
231 if (sem == SEM_FAILED) {
232 avsys_critical(AVAUDIO, "Semaphore open Fail! (name:%s, errno %d)\n", param->key_path, errno);
233 return AVSYS_STATE_ERR_INTERNAL;
236 wait_time.tv_sec = (long int)(time(NULL)) + LOCK_TIMEOUT_SEC;
237 wait_time.tv_nsec = 0;
238 ret = sem_timedwait(sem, &wait_time);
243 avsys_critical(AVAUDIO, "Lock RETRY LOCK\n");
247 avsys_critical(AVAUDIO, "Invalid semaphore\n");
248 err = AVSYS_STATE_ERR_INTERNAL;
251 avsys_critical(AVAUDIO, "EAGAIN\n");
252 err = AVSYS_STATE_ERR_INTERNAL;
255 avsys_critical(AVAUDIO, "sem_wait leached %d seconds timeout.\n", LOCK_TIMEOUT_SEC);
257 /* Recovery of sem_wait lock....in abnormal condition */
259 if (0 == sem_getvalue(sem, &sem_value)) {
260 avsys_critical(AVAUDIO,"%s sem value is %d\n",param->key_path, sem_value);
261 if (sem_value == 0) {
264 avsys_critical(AVAUDIO,"sem_post error %s : %d\n", param->key_path, sem_value);
266 avsys_critical_r(AVAUDIO,"lock recovery success...try lock again\n");
270 avsys_critical(AVAUDIO,"sem value is not 0. but failed sem_timedwait so retry.. : %s\n",param->key_path);
275 avsys_critical(AVAUDIO,"sem_getvalue failed : %s\n",param->key_path);
278 err = AVSYS_STATE_ERR_INTERNAL;
286 int avsys_unlock_sync(const avsys_sync_param_t *param)
290 int err = AVSYS_STATE_SUCCESS;
292 sem = sem_open(param->key_path, O_CREAT, 0666, 1);
293 if (sem == SEM_FAILED) {
294 avsys_critical(AVAUDIO, "Semaphore open Fail! (name:%s, errno %d)\n", param->key_path, errno);
295 return AVSYS_STATE_ERR_INTERNAL;
300 avsys_critical(AVAUDIO, "UNLOCK FAIL\n");
301 err = AVSYS_STATE_ERR_INTERNAL;
308 int avsys_check_root_privilege()
313 /* code from man page */
315 struct passwd *result;
320 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
321 if (bufsize == -1) /* Value was indeterminate */
322 bufsize = 16384; /* Should be more than enough */
324 buf = malloc(bufsize);
328 s= getpwuid_r (uid, &pwd, buf, bufsize, &result);
329 if (result == NULL) {
331 printf("Not found\n");
334 perror("getpwnam_r");
337 avsys_error_r(AVAUDIO,"super user privilege check failed (%s)\n", pwd.pw_name);
341 return AVSYS_STATE_ERR_PRIVILEGE;
343 return AVSYS_STATE_SUCCESS;
346 int avsys_dump_sync (const avsys_sync_param_t *param)
348 int err = AVSYS_STATE_SUCCESS;
354 sem = sem_open(param->key_path, O_CREAT, 0666, 1);
355 if (sem == SEM_FAILED) {
356 avsys_critical(AVAUDIO, "Semaphore open Fail! (name:%s, errno %d)\n", param->key_path, errno);
357 return AVSYS_STATE_ERR_INTERNAL;
360 if(0 == sem_getvalue(sem, &sem_value)) {
361 fprintf (stdout, " * [%d] sem value for [%s]\n", sem_value, param->key_path);
363 avsys_critical(AVAUDIO,"sem_getvalue failed : %s\n",param->key_path);