RocketLogger  2.0.2
sem.c
Go to the documentation of this file.
1 
32 #ifndef _GNU_SOURCE
33 #define _GNU_SOURCE
34 #endif
35 
36 #include <errno.h>
37 #include <string.h>
38 
39 #include <sys/ipc.h>
40 #include <sys/sem.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 
44 #include "log.h"
45 #include "rl.h"
46 
47 #include "sem.h"
48 
49 int sem_create(key_t key, int count) {
50  int id = semget(key, count, IPC_CREAT | S_IRWXU);
51  if (id < 0) {
52  rl_log(RL_LOG_ERROR, "Failed to create semaphore; %d message: %s",
53  errno, strerror(errno));
54  }
55  return id;
56 }
57 
58 int sem_remove(int id) {
59  int ret = semctl(id, 0, IPC_RMID);
60  if (ret < 0) {
61  rl_log(RL_LOG_ERROR, "Failed to remove semaphore; %d message: %s",
62  errno, strerror(errno));
63  return ret;
64  }
65  return SUCCESS;
66 }
67 
68 int sem_open(key_t key, int count) {
69  int id = semget(key, count, S_IRWXU);
70  if (id < 0) {
71  rl_log(RL_LOG_ERROR, "Failed to open semaphore; %d message: %s", errno,
72  strerror(errno));
73  return id;
74  }
75  return id;
76 }
77 
78 int sem_wait(int id, int index, int timeout) {
79  int num_ops = 1;
80  int sem_op = -1;
81  struct sembuf sem_ops = {index, sem_op, SEM_OPT_NO_FLAG};
82  struct timespec timeout_struct = {timeout, 0};
83 
84  int ret = semtimedop(id, &sem_ops, num_ops, &timeout_struct);
85  if (ret < 0) {
86  if (errno == EAGAIN) {
87  rl_log(RL_LOG_ERROR, "Timeout waiting on semaphore; %d message: %s",
88  errno, strerror(errno));
89  } else if (errno == EIDRM) {
91  "Failed waiting on semaphore, semaphore removed; %d "
92  "message: %s",
93  errno, strerror(errno));
94  } else if (errno == EINVAL) {
96  "Failed waiting on semaphore, semaphore inexistent; %d "
97  "message %s",
98  errno, strerror(errno));
99  } else {
100  rl_log(RL_LOG_ERROR, "Failed waiting on semaphore; %d message: %s",
101  errno, strerror(errno));
102  }
103  return ret;
104  }
105  return SUCCESS;
106 }
107 
108 int sem_set(int id, int index, int value) {
109  int num_ops = 1;
110  struct sembuf sem_ops = {index, value, SEM_OPT_NO_FLAG};
111  struct timespec timeout_struct = {SEM_TIMEOUT_WRITE, 0};
112 
113  int ret = semtimedop(id, &sem_ops, num_ops, &timeout_struct);
114  if (ret < 0) {
115  if (errno == EAGAIN) {
117  "Timeout on setting semaphore count; %d message: %s", errno,
118  strerror(errno));
119  } else {
121  "Failed setting semaphore count; %d message: %s", errno,
122  strerror(errno));
123  }
124  return ret;
125  }
126  return SUCCESS;
127 }
128 
129 int sem_get(int id, int index) {
130  int count = semctl(id, index, GETNCNT);
131  if (count < 0) {
132  rl_log(RL_LOG_ERROR, "Failed getting semaphore count; %d message: %s",
133  errno, strerror(errno));
134  return count;
135  }
136  return count;
137 }
int rl_log(rl_log_level_t log_level, char const *const format,...)
Definition: log.c:82
@ RL_LOG_ERROR
Error.
Definition: log.h:49
#define SUCCESS
Function return value for successful completion.
Definition: rl.h:44
int sem_open(key_t key, int count)
Definition: sem.c:68
int sem_create(key_t key, int count)
Definition: sem.c:49
int sem_remove(int id)
Definition: sem.c:58
int sem_get(int id, int index)
Definition: sem.c:129
int sem_set(int id, int index, int value)
Definition: sem.c:108
int sem_wait(int id, int index, int timeout)
Definition: sem.c:78
#define SEM_TIMEOUT_WRITE
Time out time in seconds, waiting on semaphore write.
Definition: sem.h:44
#define SEM_OPT_NO_FLAG
Time out time in seconds, waiting on semaphore write.
Definition: sem.h:40