Merge branch 'dev' of ssh://box.fiane.intra/var/lib/git/libucore into dev

This commit is contained in:
Nils O. Selåsdal
2013-06-05 20:52:16 +02:00
13 changed files with 435 additions and 82 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ struct UCoreClock uc_time_clock = {
.now = uc_time_now,
};
static void uc_cached_clock_now(struct UCoreClock *uclock UNUSED,
static void uc_cached_clock_now(struct UCoreClock *uclock,
struct timeval *tv)
{
struct UCoreCachedClock *cached_clock;
+68
View File
@@ -0,0 +1,68 @@
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "ucore/fd_utils.h"
int set_nonblocking(int fd)
{
int flags = fcntl(fd,F_GETFL,NULL);
if(flags < 0 ) {
return flags;
}
return fcntl(fd,F_SETFL,flags | O_NONBLOCK);
}
int set_blocking(int fd)
{
int flags = fcntl(fd,F_GETFL,NULL);
if(flags < 0 ) {
return flags;
}
return fcntl(fd,F_SETFL,flags&~O_NONBLOCK);
}
int set_reuseaddr(int sockfd)
{
int opt = 1;
return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
}
int set_tcp_keepalive(int sockfd)
{
int optval = 1;
return setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
}
int set_send_timeout(int sockfd,long timeoutms)
{
struct timeval so_sndtimeo = {timeoutms/1000,(timeoutms%1000)*1000};
return setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &so_sndtimeo, sizeof(so_sndtimeo));
}
int set_receive_timeout(int sockfd,long timeoutms)
{
struct timeval so_sndtimeo = {timeoutms/1000,(timeoutms%1000)*1000};
return setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &so_sndtimeo, sizeof(so_sndtimeo));
}
int
set_ip_dscp(int fd, unsigned int dscp) {
int val;
if (dscp > 63) {
errno = EINVAL;
return -1;
}
val = dscp << 2;
return setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val);
}
+87 -46
View File
@@ -10,6 +10,11 @@
#include "ucore/utils.h"
//used to realise per destination settings
struct uc_log_module_setting {
int log_level;
};
struct uc_log_destination {
SLIST_ENTRY(uc_log_destination) entry;
enum UC_LOG_DESTINATION dest_type;
@@ -17,6 +22,7 @@ struct uc_log_destination {
int log_level;
//Whether to log the source file name and line number
int log_location;
struct uc_log_module_setting *module_settings;
union {
//for syslog openlog()
struct {
@@ -112,6 +118,22 @@ static inline int uc_ll_2_syslog(enum UC_LOG_LEVEL l)
return LOG_INFO;
}
static int uc_log_init_settings(struct uc_log_destination *dest)
{
int i;
dest->module_settings = calloc(g_modules.cnt, sizeof *dest->module_settings);
if (dest->module_settings == NULL) {
return -1;
}
for (i = 0; i < g_modules.cnt; i++) {
dest->module_settings[i].log_level = g_modules.mods[i].log_level;
}
return 0;
}
struct uc_log_destination *uc_log_new_stderr(int log_level, int log_location)
{
struct uc_log_destination *dest = calloc(1, sizeof *dest);
@@ -119,6 +141,11 @@ struct uc_log_destination *uc_log_new_stderr(int log_level, int log_location)
if(dest == NULL)
return NULL;
if (uc_log_init_settings(dest) != 0) {
free(dest);
return NULL;
}
dest->dest_type = UC_LDEST_STDERR;
dest->type.file.file = stderr;
dest->log_level = log_level;;
@@ -135,9 +162,15 @@ struct uc_log_destination *uc_log_new_syslog(const char *ident, int facility, in
if (dest == NULL)
return NULL;
if (uc_log_init_settings(dest) != 0) {
free(dest);
return NULL;
}
//copy the ident - though it's unused inside the struct for now
id = strdup(ident);
if (id == NULL) {
free(dest->module_settings);
free(dest);
return NULL;
}
@@ -162,16 +195,25 @@ struct uc_log_destination *uc_log_new_file(const char *filename, int log_level,
if (dest == NULL)
return NULL;
if (uc_log_init_settings(dest) != 0) {
free(dest);
return NULL;
}
name = strdup(filename);
if (name == NULL) {
free(dest->module_settings);
free(dest);
return NULL;
}
f = fopen(filename, "a");
if (f == NULL) {
int saved_errno = errno;
free(dest->module_settings);
free(name);
free(dest);
errno = saved_errno;
return NULL;
}
@@ -262,12 +304,22 @@ void uc_log_delete_destination(struct uc_log_destination *dest)
free(dest->type.file.file_name);
break;
}
free(dest->module_settings);
free(dest);
pthread_mutex_unlock(&g_log_lock);
}
void uc_log_destination_set_loglevel(struct uc_log_destination *dest, enum UC_LOG_LEVEL level)
{
pthread_mutex_lock(&g_log_lock);
dest->log_level = level;
pthread_mutex_unlock(&g_log_lock);
}
static int uc_log_reopen_file(struct uc_log_destination *dest)
{
int rc = 0;
@@ -311,25 +363,6 @@ void uc_log_init(struct uc_log_modules *user_modules)
g_modules = *user_modules;
}
int uc_log_module_set_loglevel(int module, enum UC_LOG_LEVEL level)
{
int rc = -1;
int i;
pthread_mutex_lock(&g_log_lock);
for (i = 0; i < g_modules.cnt; i++) {
if (g_modules.mods[i].id == module) {
g_modules.mods[i].log_level = level;
rc = 0;
break;
}
}
pthread_mutex_unlock(&g_log_lock);
return rc;
}
static inline void uc_vlog_file(const struct uc_log_args *args, const char *fmt, va_list ap)
{
@@ -429,43 +462,51 @@ void uc_logf(int log_level, int module, int raw,
else
log_module = &g_unknown_module;
//Check if the module should not be logged
if (log_level < log_module->log_level)
goto out;
//do logging for each destination
SLIST_FOREACH(dest, &g_destinations, entry) {
va_list apc;
int module_log_level;
if (log_level >= dest->log_level) {
const struct uc_log_args args = {
.dest = dest,
.module = log_module,
.log_level = log_level,
.file = file,
.line = line,
.raw = raw
};
//log func will mess with the va_list,
//so make a copy
va_copy(apc, ap);
//destnation level
if (log_level < dest->log_level)
continue;
switch (dest->dest_type) {
case UC_LDEST_SYSLOG:
uc_vlog_syslog(&args, fmt, apc);
break;
case UC_LDEST_STDERR:
case UC_LDEST_FILE:
uc_vlog_file(&args, fmt, apc);
break;
}
//individual module level
if (module >= 0 && module < g_modules.cnt)
module_log_level = dest->module_settings[module].log_level;
else //application bug. use global/unknown module
module_log_level = log_module->log_level;
va_end(apc);
if (log_level < module_log_level)
continue;
const struct uc_log_args args = {
.dest = dest,
.module = log_module,
.log_level = log_level,
.file = file,
.line = line,
.raw = raw
};
//log func will mess with the va_list,
//so make a copy
va_copy(apc, ap);
switch (dest->dest_type) {
case UC_LDEST_SYSLOG:
uc_vlog_syslog(&args, fmt, apc);
break;
case UC_LDEST_STDERR:
case UC_LDEST_FILE:
uc_vlog_file(&args, fmt, apc);
break;
}
va_end(apc);
}
out:
pthread_mutex_unlock(&g_log_lock);
va_end(ap);
}
+9 -9
View File
@@ -26,9 +26,9 @@ static int timers_cmp(const void *a_, const void *b_)
return 0; //silence compiler
}
static void uc_timer_real_add(struct UCTimers *timers, struct UCTimer *timer)
static void uc_timers_real_add(struct UCTimers *timers, struct UCTimer *timer)
{
uc_timer_remove(timers, timer); //re schedule it if it's already running
uc_timers_remove(timers, timer); //re schedule it if it's already running
timer->state = UC_TIMER_ACTIVE;
timer->seq = timers->seq++;
timer->rb_node.data = timer; //point the data in a RBNode back to the timer that contains it.
@@ -49,7 +49,7 @@ void uc_timers_init_ex(struct UCTimers *t, struct UCoreClock *uclock)
TAILQ_INIT(&t->ready_list);
}
void uc_timer_add(struct UCTimers *timers, struct UCTimer *timer, int sec, int usec)
void uc_timers_add(struct UCTimers *timers, struct UCTimer *timer, int sec, int usec)
{
struct timeval now;
struct timeval tmp;
@@ -63,10 +63,10 @@ void uc_timer_add(struct UCTimers *timers, struct UCTimer *timer, int sec, int u
timers->uclock->now(timers->uclock, &now);
timeradd(&now, &tmp, &timer->timeout);
uc_timer_real_add(timers, timer);
uc_timers_real_add(timers, timer);
}
void uc_timer_remove(struct UCTimers *timers, struct UCTimer *timer)
void uc_timers_remove(struct UCTimers *timers, struct UCTimer *timer)
{
if (timer->state == UC_TIMER_INACTIVE) { //already removed , or never added.
//debugging bandaid
@@ -97,7 +97,7 @@ int uc_timers_first(const struct UCTimers *timers, struct timeval *first)
return 1;
}
size_t uc_timer_count(const struct UCTimers *timers)
size_t uc_timers_count(const struct UCTimers *timers)
{
struct RBNode *n;
size_t count = 0;
@@ -134,7 +134,7 @@ int uc_timers_run(struct UCTimers *timers)
//Note, timercmp does not work for >=
if (!timercmp(&now, &t->timeout, <)) {
TAILQ_INSERT_TAIL(&timers->ready_list, t, ready_entry);
t->state = UC_TIMER_PENDING; //so uc_timer_remove knows
t->state = UC_TIMER_PENDING; //so uc_timers_remove knows
//it have to remove it from the ready_list
} else {
break;
@@ -147,12 +147,12 @@ int uc_timers_run(struct UCTimers *timers)
* Need to always pick the first item in the tail queue
* since a timer callback could remove/free a timer
* we had in the list.
* uc_timer_remove will unlink the timer from this list.
* uc_timers_remove will unlink the timer from this list.
*/
while (!TAILQ_EMPTY(&timers->ready_list)) {
struct UCTimer *t = TAILQ_FIRST(&timers->ready_list);
uc_timer_remove(timers, t);
uc_timers_remove(timers, t);
assert(t->callback != NULL);
t->callback(timers, t); //fire
// callback might have free'd its timer.