initial
This commit is contained in:
commit
6b4a2d6f18
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
libircclient
|
||||
*.o
|
||||
*.d
|
||||
irclogbot
|
||||
irclogbot.log
|
20
Makefile
Normal file
20
Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
CFLAGS := -O2 -pipe -Wall
|
||||
|
||||
OBJS := configfile.o logging.o
|
||||
|
||||
all: irclogbot
|
||||
|
||||
irclogbot: $(OBJS) irclogbot.o
|
||||
$(CC) $(CFLAGS) -o $@ $^ libircclient/src/libircclient.a
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
%.d: %.c
|
||||
$(CC) $(CFLAGS) -MM -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f irclogbot *.d *.o *.log
|
||||
|
||||
DEPS := $(wildcard *.c)
|
||||
-include $(DEPS:.c=.d)
|
217
configfile.c
Normal file
217
configfile.c
Normal file
@ -0,0 +1,217 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 06/2006 by Olaf Rempel *
|
||||
* razzor@kopf-tisch.de *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "configfile.h"
|
||||
#include "list.h"
|
||||
#include "logging.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
struct conf_section {
|
||||
struct list_head list;
|
||||
struct list_head tupel_list;
|
||||
const char *name;
|
||||
};
|
||||
|
||||
struct conf_tupel {
|
||||
struct list_head list;
|
||||
const char *option;
|
||||
const char *parameter;
|
||||
};
|
||||
|
||||
static LIST_HEAD(config_list);
|
||||
|
||||
static struct conf_section * config_add_section(const char *name)
|
||||
{
|
||||
struct conf_section *section;
|
||||
section = malloc(sizeof(struct conf_section) + strlen(name));
|
||||
if (section == NULL)
|
||||
return NULL;
|
||||
|
||||
INIT_LIST_HEAD(§ion->list);
|
||||
INIT_LIST_HEAD(§ion->tupel_list);
|
||||
|
||||
section->name = strdup(name);
|
||||
if (section->name == NULL) {
|
||||
free(section);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_add_tail(§ion->list, &config_list);
|
||||
return section;
|
||||
}
|
||||
|
||||
static int config_add_tupel(struct conf_section *section, const char *option, const char *parameter)
|
||||
{
|
||||
struct conf_tupel *tupel = malloc(sizeof(struct conf_tupel));
|
||||
if (tupel == NULL)
|
||||
return -1;
|
||||
|
||||
INIT_LIST_HEAD(&tupel->list);
|
||||
tupel->option = strdup(option);
|
||||
tupel->parameter = strdup(parameter);
|
||||
|
||||
if (tupel->option == NULL || tupel->parameter == NULL) {
|
||||
free(tupel);
|
||||
return -1;
|
||||
}
|
||||
|
||||
list_add_tail(&tupel->list, §ion->tupel_list);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_parse(const char *config)
|
||||
{
|
||||
FILE *fz = fopen(config, "r");
|
||||
if (fz == NULL) {
|
||||
log_print(LOG_ERROR, "config_parse(): %s", config);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *line = malloc(BUFSIZE);
|
||||
if (line == NULL) {
|
||||
log_print(LOG_ERROR, "config_parse(): out of memory");
|
||||
fclose(fz);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int linenum = 0;
|
||||
struct conf_section *section = NULL;
|
||||
while (fgets(line, BUFSIZE, fz) != NULL) {
|
||||
linenum++;
|
||||
|
||||
if (line[0] == '#' || line[0] <= ' ') {
|
||||
continue;
|
||||
|
||||
} else if (line[0] == '[') {
|
||||
char *tok = strtok(line +1, " ]\n");
|
||||
|
||||
if (tok == NULL || (section = config_add_section(tok)) == NULL) {
|
||||
log_print(LOG_WARN, "config_parse(): invalid section in row %d", linenum);
|
||||
free(line);
|
||||
fclose(fz);
|
||||
return -1;
|
||||
}
|
||||
continue;
|
||||
|
||||
} else if (section == NULL) {
|
||||
log_print(LOG_WARN, "config_parse(): missing section in row %d", linenum);
|
||||
free(line);
|
||||
fclose(fz);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *tok = strtok(line, " \n");
|
||||
if (tok != NULL) {
|
||||
char *tok2;
|
||||
while ((tok2 = strtok(NULL, " \n"))) {
|
||||
if (config_add_tupel(section, tok, tok2) != 0)
|
||||
log_print(LOG_WARN, "config_parse(): invalid row %d", linenum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fz);
|
||||
free(line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void config_free(void)
|
||||
{
|
||||
struct conf_section *section, *section_tmp;
|
||||
struct conf_tupel *tupel, *tupel_tmp;
|
||||
|
||||
list_for_each_entry_safe(section, section_tmp, &config_list, list) {
|
||||
list_for_each_entry_safe(tupel, tupel_tmp, §ion->tupel_list, list) {
|
||||
list_del(&tupel->list);
|
||||
free((char *)tupel->option);
|
||||
free((char *)tupel->parameter);
|
||||
free(tupel);
|
||||
}
|
||||
list_del(§ion->list);
|
||||
free(section);
|
||||
}
|
||||
}
|
||||
|
||||
static struct conf_section * config_get_section(const char *name)
|
||||
{
|
||||
struct conf_section *section;
|
||||
|
||||
list_for_each_entry(section, &config_list, list) {
|
||||
if (!strcmp(section->name, name))
|
||||
return section;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char * config_get_string(const char *section_str, const char *option, const char *def)
|
||||
{
|
||||
struct conf_section *section = config_get_section(section_str);
|
||||
if (section != NULL) {
|
||||
struct conf_tupel *tupel;
|
||||
list_for_each_entry(tupel, §ion->tupel_list, list) {
|
||||
if (!strcmp(tupel->option, option))
|
||||
return tupel->parameter;
|
||||
}
|
||||
}
|
||||
|
||||
if (def != NULL)
|
||||
log_print(LOG_WARN, "config [%s:%s] not found, using default: '%s'",
|
||||
section_str, option, def);
|
||||
return def;
|
||||
}
|
||||
|
||||
int config_get_int(const char *section, const char *option, int def)
|
||||
{
|
||||
const char *ret = config_get_string(section, option, NULL);
|
||||
if (ret == NULL) {
|
||||
log_print(LOG_WARN, "config [%s:%s] not found, using default: '%d'",
|
||||
section, option, def);
|
||||
return def;
|
||||
}
|
||||
return atoi(ret);
|
||||
}
|
||||
|
||||
int config_get_strings(const char *section_str, const char *option,
|
||||
int (*callback)(const char *value, void *privdata),
|
||||
void *privdata)
|
||||
{
|
||||
struct conf_section *section = config_get_section(section_str);
|
||||
if (section == NULL)
|
||||
return -1;
|
||||
|
||||
int cnt = 0;
|
||||
struct conf_tupel *tupel;
|
||||
list_for_each_entry(tupel, §ion->tupel_list, list) {
|
||||
if (!strcmp(tupel->option, option))
|
||||
if (callback(tupel->parameter, privdata) == 0)
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
15
configfile.h
Normal file
15
configfile.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef _CONFIG_H_
|
||||
#define _CONFIG_H_
|
||||
|
||||
int config_parse(const char *config);
|
||||
void config_free(void);
|
||||
|
||||
const char * config_get_string(const char *section_str, const char *option, const char *def);
|
||||
|
||||
int config_get_int(const char *section, const char *option, int def);
|
||||
|
||||
int config_get_strings(const char *section_str, const char *option,
|
||||
int (*callback)(const char *value, void *privdata),
|
||||
void *privdata);
|
||||
|
||||
#endif /* _CONFIG_H_ */
|
244
irclogbot.c
Normal file
244
irclogbot.c
Normal file
@ -0,0 +1,244 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "libircclient/include/libircclient.h"
|
||||
|
||||
#include "configfile.h"
|
||||
#include "list.h"
|
||||
#include "logging.h"
|
||||
|
||||
#define DEFAULT_CONFIG "irclogbot.conf"
|
||||
#define DEFAULT_LOGFILE "irclogbot.log"
|
||||
|
||||
static struct option opts[] = {
|
||||
{"config", 1, 0, 'c'},
|
||||
{"debug", 0, 0, 'd'},
|
||||
{"help", 0, 0, 'h'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
struct irc_client {
|
||||
struct list_head list;
|
||||
|
||||
irc_session_t *session;
|
||||
const char *server;
|
||||
const char *nickname;
|
||||
const char *channel;
|
||||
const char *channelkey;
|
||||
|
||||
struct in_addr addr;
|
||||
};
|
||||
|
||||
static LIST_HEAD(client_list);
|
||||
static irc_callbacks_t irc_cbs;
|
||||
|
||||
static int add_client(const char *parameter, void *privdata)
|
||||
{
|
||||
struct irc_client *entry = malloc(sizeof(struct irc_client));
|
||||
if (entry == NULL) {
|
||||
log_print(LOG_ERROR, "add_client(): out of memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (inet_pton(AF_INET, parameter, &entry->addr) <= 0) {
|
||||
log_print(LOG_ERROR, "add_client(): inet_pton()");
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *defserver = config_get_string("global", "server", NULL);
|
||||
entry->server = config_get_string(parameter, "server", defserver);
|
||||
if (entry->server == NULL) {
|
||||
log_print(LOG_ERROR, "add_client(): client %s: no server given", parameter);
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *defchannel = config_get_string("global", "channel", NULL);
|
||||
entry->channel = config_get_string(parameter, "channel", defchannel);
|
||||
if (entry->channel == NULL) {
|
||||
log_print(LOG_ERROR, "add_client(): client %s: no channel given", parameter);
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *defchannelkey = config_get_string("global", "channelkey", NULL);
|
||||
entry->channelkey = config_get_string(parameter, "channelkey", defchannelkey);
|
||||
|
||||
entry->nickname = config_get_string(parameter, "nickname", NULL);
|
||||
if (entry->nickname == NULL) {
|
||||
log_print(LOG_ERROR, "add_client(): client %s: no nickname given", parameter);
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
entry->session = irc_create_session(&irc_cbs);
|
||||
if (entry->session == NULL) {
|
||||
log_print(LOG_ERROR, "add_client(): irc_create_session()");
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
irc_set_ctx(entry->session, entry);
|
||||
irc_option_set(entry->session, LIBIRC_OPTION_STRIPNICKS);
|
||||
|
||||
int ret = irc_connect(entry->session, entry->server, 6667, NULL, entry->nickname, NULL, NULL);
|
||||
if (ret != 0) {
|
||||
log_print(LOG_ERROR, "add_client(): irc_connect(): %s", irc_strerror(irc_errno(entry->session)));
|
||||
irc_destroy_session(entry->session);
|
||||
free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* HACK: irc_connect() sets errno */
|
||||
errno = 0;
|
||||
|
||||
log_print(LOG_DEBUG, "added client %s (server: %s chan: %s nickname: %s)",
|
||||
parameter, entry->server, entry->channel, entry->nickname);
|
||||
|
||||
list_add(&entry->list, &client_list);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void event_connect(irc_session_t *session, const char *event, const char *origin, const char **params, unsigned int count)
|
||||
{
|
||||
struct irc_client *entry = (struct irc_client *) irc_get_ctx(session);
|
||||
irc_cmd_join(session, entry->channel, entry->channelkey);
|
||||
}
|
||||
|
||||
static void event_join(irc_session_t *session, const char *event, const char *origin, const char **params, unsigned int count)
|
||||
{
|
||||
struct irc_client *entry = (struct irc_client *) irc_get_ctx(session);
|
||||
irc_cmd_user_mode(session, "+i");
|
||||
|
||||
char buf[32] = { "forwarding from " };
|
||||
inet_ntop(AF_INET, &entry->addr, buf +16, 16);
|
||||
|
||||
irc_cmd_msg(session, params[0], buf);
|
||||
}
|
||||
|
||||
static void event_numeric(irc_session_t *session, unsigned int event, const char *origin, const char **params, unsigned int count)
|
||||
{
|
||||
if (event < 400)
|
||||
return;
|
||||
|
||||
log_print(LOG_INFO, "EVENT %d: %s: %s %s %s %s",
|
||||
event, (origin ? origin : "unknown"), params[0],
|
||||
count > 1 ? params[1] : "",
|
||||
count > 2 ? params[2] : "",
|
||||
count > 3 ? params[3] : "");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *config = DEFAULT_CONFIG;
|
||||
int code, arg = 0, debug = 0;
|
||||
|
||||
do {
|
||||
code = getopt_long(argc, argv, "c:dh", opts, &arg);
|
||||
|
||||
switch (code) {
|
||||
case 'c': /* config */
|
||||
config = optarg;
|
||||
break;
|
||||
|
||||
case 'd': /* debug */
|
||||
debug = 1;
|
||||
break;
|
||||
|
||||
case 'h': /* help */
|
||||
printf("Usage: ctstat [options]\n"
|
||||
"Options: \n"
|
||||
" --config -c configfile use this configfile\n"
|
||||
" --debug -d do not fork and log to stderr\n"
|
||||
" --help -h this help\n"
|
||||
"\n");
|
||||
exit(0);
|
||||
break;
|
||||
|
||||
case '?': /* error */
|
||||
exit(-1);
|
||||
break;
|
||||
|
||||
default: /* unknown / all options parsed */
|
||||
break;
|
||||
}
|
||||
} while (code != -1);
|
||||
|
||||
/* parse config file */
|
||||
if (config_parse(config))
|
||||
exit(1);
|
||||
|
||||
int sock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
log_print(LOG_ERROR, "socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in addr = {
|
||||
.sin_family = AF_INET,
|
||||
.sin_port = htons(514),
|
||||
.sin_addr.s_addr = INADDR_ANY,
|
||||
};
|
||||
|
||||
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
log_print(LOG_ERROR, "bind()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
irc_cbs.event_connect = event_connect;
|
||||
irc_cbs.event_join = event_join;
|
||||
irc_cbs.event_numeric = event_numeric;
|
||||
|
||||
int cnt = config_get_strings("global", "client", add_client, NULL);
|
||||
if (cnt == 0) {
|
||||
log_print(LOG_ERROR, "no clients defined");
|
||||
close(sock);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char buf[1500];
|
||||
while (1) {
|
||||
fd_set rd_fds, wr_fds;
|
||||
FD_ZERO(&rd_fds);
|
||||
FD_ZERO(&wr_fds);
|
||||
|
||||
FD_SET(sock, &rd_fds);
|
||||
|
||||
int maxfd = sock;
|
||||
struct irc_client *entry;
|
||||
list_for_each_entry(entry, &client_list, list) {
|
||||
int ret = irc_add_select_descriptors(entry->session, &rd_fds, &wr_fds, &maxfd);
|
||||
if (ret != 0)
|
||||
log_print(LOG_WARN, "irc_add_select_descriptors(): %s", irc_strerror(irc_errno(entry->session)));
|
||||
}
|
||||
|
||||
select(maxfd +1, &rd_fds, &wr_fds, NULL, NULL);
|
||||
|
||||
int len = 0;
|
||||
if (FD_ISSET(sock, &rd_fds)) {
|
||||
unsigned int i = sizeof(addr);
|
||||
len = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &i);
|
||||
}
|
||||
|
||||
list_for_each_entry(entry, &client_list, list) {
|
||||
if (len > 0 && (entry->addr.s_addr == addr.sin_addr.s_addr))
|
||||
irc_cmd_msg(entry->session, entry->channel, buf);
|
||||
|
||||
int ret = irc_process_select_descriptors(entry->session, &rd_fds, &wr_fds);
|
||||
if (ret != 0)
|
||||
log_print(LOG_WARN, "irc_process_select_descriptors(): %s", irc_strerror(irc_errno(entry->session)));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
12
irclogbot.conf
Normal file
12
irclogbot.conf
Normal file
@ -0,0 +1,12 @@
|
||||
[global]
|
||||
server irc.quakenet.org
|
||||
channel #logtest
|
||||
|
||||
client 127.0.0.1
|
||||
client 10.10.250.133
|
||||
|
||||
[127.0.0.1]
|
||||
nickname logtest1
|
||||
|
||||
[10.10.250.133]
|
||||
nickname logtest2
|
268
list.h
Normal file
268
list.h
Normal file
@ -0,0 +1,268 @@
|
||||
#ifndef _LIST_H_
|
||||
#define _LIST_H_
|
||||
|
||||
/*
|
||||
* stolen from linux kernel 2.6.11 (http://kernel.org/)
|
||||
* linux/include/linux/stddef.h (offsetoff)
|
||||
* linux/include/linux/kernel.h (container_of)
|
||||
* linux/include/linux/list.h (*list*)
|
||||
* linux/include/linux/netfilter_ipv4/listhelp.h (LIST_FIND)
|
||||
*
|
||||
* modified by Olaf Rempel <razzor@kopf-tisch.de>
|
||||
*/
|
||||
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
|
||||
|
||||
#define container_of(ptr, type, member) ({ \
|
||||
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
||||
(type *)( (char *)__mptr - offsetof(type,member) );})
|
||||
|
||||
struct list_head {
|
||||
struct list_head *next, *prev;
|
||||
};
|
||||
|
||||
#define LIST_HEAD_INIT(name) { &(name), &(name) }
|
||||
|
||||
#define LIST_HEAD(name) \
|
||||
struct list_head name = LIST_HEAD_INIT(name)
|
||||
|
||||
#define INIT_LIST_HEAD(ptr) do { \
|
||||
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Insert a new entry between two known consecutive entries.
|
||||
*
|
||||
* This is only for internal list manipulation where we know
|
||||
* the prev/next entries already!
|
||||
*/
|
||||
static inline void __list_add(struct list_head *new,
|
||||
struct list_head *prev,
|
||||
struct list_head *next)
|
||||
{
|
||||
next->prev = new;
|
||||
new->next = next;
|
||||
new->prev = prev;
|
||||
prev->next = new;
|
||||
}
|
||||
|
||||
/*
|
||||
* list_add - add a new entry
|
||||
* @new: new entry to be added
|
||||
* @head: list head to add it after
|
||||
*
|
||||
* Insert a new entry after the specified head.
|
||||
* This is good for implementing stacks.
|
||||
*/
|
||||
static inline void list_add(struct list_head *new, struct list_head *head)
|
||||
{
|
||||
__list_add(new, head, head->next);
|
||||
}
|
||||
|
||||
/*
|
||||
* list_add_tail - add a new entry
|
||||
* @new: new entry to be added
|
||||
* @head: list head to add it before
|
||||
*
|
||||
* Insert a new entry before the specified head.
|
||||
* This is useful for implementing queues.
|
||||
*/
|
||||
static inline void list_add_tail(struct list_head *new, struct list_head *head)
|
||||
{
|
||||
__list_add(new, head->prev, head);
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a list entry by making the prev/next entries
|
||||
* point to each other.
|
||||
*
|
||||
* This is only for internal list manipulation where we know
|
||||
* the prev/next entries already!
|
||||
*/
|
||||
static inline void __list_del(struct list_head * prev, struct list_head * next)
|
||||
{
|
||||
next->prev = prev;
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
/*
|
||||
* list_del - deletes entry from list.
|
||||
* @entry: the element to delete from the list.
|
||||
* Note: list_empty on entry does not return true after this, the entry is
|
||||
* in an undefined state.
|
||||
*/
|
||||
static inline void list_del(struct list_head *entry)
|
||||
{
|
||||
__list_del(entry->prev, entry->next);
|
||||
entry->next = NULL;
|
||||
entry->prev = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* list_del_init - deletes entry from list and reinitialize it.
|
||||
* entry: the element to delete from the list.
|
||||
*/
|
||||
static inline void list_del_init(struct list_head *entry)
|
||||
{
|
||||
__list_del(entry->prev, entry->next);
|
||||
INIT_LIST_HEAD(entry);
|
||||
}
|
||||
|
||||
/*
|
||||
* list_move - delete from one list and add as another's head
|
||||
* @list: the entry to move
|
||||
* @head: the head that will precede our entry
|
||||
*/
|
||||
static inline void list_move(struct list_head *list, struct list_head *head)
|
||||
{
|
||||
__list_del(list->prev, list->next);
|
||||
list_add(list, head);
|
||||
}
|
||||
|
||||
/*
|
||||
* list_move_tail - delete from one list and add as another's tail
|
||||
* @list: the entry to move
|
||||
* @head: the head that will follow our entry
|
||||
*/
|
||||
static inline void list_move_tail(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
__list_del(list->prev, list->next);
|
||||
list_add_tail(list, head);
|
||||
}
|
||||
|
||||
/*
|
||||
* list_empty - tests whether a list is empty
|
||||
* @head: the list to test.
|
||||
*/
|
||||
static inline int list_empty(const struct list_head *head)
|
||||
{
|
||||
return head->next == head;
|
||||
}
|
||||
|
||||
static inline void __list_splice(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
struct list_head *first = list->next;
|
||||
struct list_head *last = list->prev;
|
||||
struct list_head *at = head->next;
|
||||
|
||||
first->prev = head;
|
||||
head->next = first;
|
||||
|
||||
last->next = at;
|
||||
at->prev = last;
|
||||
}
|
||||
|
||||
/*
|
||||
* list_splice - join two lists
|
||||
* @list: the new list to add.
|
||||
* @head: the place to add it in the first list.
|
||||
*/
|
||||
static inline void list_splice(struct list_head *list, struct list_head *head)
|
||||
{
|
||||
if (!list_empty(list))
|
||||
__list_splice(list, head);
|
||||
}
|
||||
|
||||
/*
|
||||
* list_splice_init - join two lists and reinitialise the emptied list.
|
||||
* @list: the new list to add.
|
||||
* @head: the place to add it in the first list.
|
||||
*
|
||||
* The list at @list is reinitialised
|
||||
*/
|
||||
static inline void list_splice_init(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
if (!list_empty(list)) {
|
||||
__list_splice(list, head);
|
||||
INIT_LIST_HEAD(list);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* list_entry - get the struct for this entry
|
||||
* @ptr: the &struct list_head pointer.
|
||||
* @type: the type of the struct this is embedded in.
|
||||
* @member: the name of the list_struct within the struct.
|
||||
*/
|
||||
#define list_entry(ptr, type, member) \
|
||||
container_of(ptr, type, member)
|
||||
|
||||
/*
|
||||
* list_for_each - iterate over a list
|
||||
* @pos: the &struct list_head to use as a loop counter.
|
||||
* @head: the head for your list.
|
||||
*/
|
||||
#define list_for_each(pos, head) \
|
||||
for (pos = (head)->next; pos != (head); pos = pos->next)
|
||||
|
||||
/*
|
||||
* list_for_each_prev - iterate over a list backwards
|
||||
* @pos: the &struct list_head to use as a loop counter.
|
||||
* @head: the head for your list.
|
||||
*/
|
||||
#define list_for_each_prev(pos, head) \
|
||||
for (pos = (head)->prev; pos != (head); pos = pos->prev)
|
||||
|
||||
/*
|
||||
* list_for_each_safe - iterate over a list safe against removal of list entry
|
||||
* @pos: the &struct list_head to use as a loop counter.
|
||||
* @n: another &struct list_head to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
*/
|
||||
#define list_for_each_safe(pos, n, head) \
|
||||
for (pos = (head)->next, n = pos->next; pos != (head); \
|
||||
pos = n, n = pos->next)
|
||||
|
||||
/*
|
||||
* list_for_each_entry - iterate over list of given type
|
||||
* @pos: the type * to use as a loop counter.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_struct within the struct.
|
||||
*/
|
||||
#define list_for_each_entry(pos, head, member) \
|
||||
for (pos = list_entry((head)->next, typeof(*pos), member); \
|
||||
&pos->member != (head); \
|
||||
pos = list_entry(pos->member.next, typeof(*pos), member))
|
||||
|
||||
/*
|
||||
* list_for_each_entry_reverse - iterate backwards over list of given type.
|
||||
* @pos: the type * to use as a loop counter.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_struct within the struct.
|
||||
*/
|
||||
#define list_for_each_entry_reverse(pos, head, member) \
|
||||
for (pos = list_entry((head)->prev, typeof(*pos), member); \
|
||||
&pos->member != (head); \
|
||||
pos = list_entry(pos->member.prev, typeof(*pos), member))
|
||||
|
||||
/*
|
||||
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
|
||||
* @pos: the type * to use as a loop counter.
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_struct within the struct.
|
||||
*/
|
||||
#define list_for_each_entry_safe(pos, n, head, member) \
|
||||
for (pos = list_entry((head)->next, typeof(*pos), member), \
|
||||
n = list_entry(pos->member.next, typeof(*pos), member); \
|
||||
&pos->member != (head); \
|
||||
pos = n, n = list_entry(n->member.next, typeof(*n), member))
|
||||
|
||||
|
||||
/* Return pointer to first true entry, if any, or NULL. A macro
|
||||
required to allow inlining of cmpfn. */
|
||||
#define LIST_FIND(head, cmpfn, type, args...) \
|
||||
({ \
|
||||
const struct list_head *__i, *__j = NULL; \
|
||||
\
|
||||
list_for_each(__i, (head)) \
|
||||
if (cmpfn((const type)__i , ## args)) { \
|
||||
__j = __i; \
|
||||
break; \
|
||||
} \
|
||||
(type)__j; \
|
||||
})
|
||||
|
||||
#endif /* _LIST_H_ */
|
102
logging.c
Normal file
102
logging.c
Normal file
@ -0,0 +1,102 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 06/2006 by Olaf Rempel *
|
||||
* razzor@kopf-tisch.de *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "logging.h"
|
||||
|
||||
#define BUFSIZE 8192
|
||||
|
||||
static FILE *log_fd = NULL;
|
||||
static int log_prio = LOG_EVERYTIME;
|
||||
static char *buffer = NULL;
|
||||
|
||||
int log_print(int prio, const char *fmt, ...)
|
||||
{
|
||||
va_list az;
|
||||
int len = 0, retval;
|
||||
|
||||
if (prio < log_prio)
|
||||
return 0;
|
||||
|
||||
if (buffer == NULL) {
|
||||
buffer = malloc(BUFSIZE);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "log_print(): out of memory\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (log_fd != NULL) {
|
||||
time_t tzgr;
|
||||
time(&tzgr);
|
||||
|
||||
len += strftime(buffer, BUFSIZE, "%b %d %H:%M:%S :", localtime(&tzgr));
|
||||
}
|
||||
|
||||
va_start(az, fmt);
|
||||
len += vsnprintf(buffer + len, BUFSIZE - len, fmt, az);
|
||||
va_end(az);
|
||||
|
||||
if (len < 0 || len >= BUFSIZE) {
|
||||
errno = 0;
|
||||
return log_print(LOG_ERROR, "log_print: arguments too long");
|
||||
}
|
||||
|
||||
if (errno) {
|
||||
len += snprintf(buffer + len, BUFSIZE - len, ": %s", strerror(errno));
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
retval = fprintf((log_fd ? log_fd : stderr), "%s\n", buffer);
|
||||
fflush(log_fd);
|
||||
return retval;
|
||||
}
|
||||
|
||||
void log_close(void)
|
||||
{
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
|
||||
if (log_fd)
|
||||
fclose(log_fd);
|
||||
}
|
||||
|
||||
int log_init(const char *logfile)
|
||||
{
|
||||
if (log_fd != NULL)
|
||||
log_close();
|
||||
|
||||
log_fd = fopen(logfile, "a");
|
||||
if (log_fd == NULL) {
|
||||
fprintf(stderr, "log_init(): can not open logfile");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void log_setprio(int prio)
|
||||
{
|
||||
log_prio = prio;
|
||||
}
|
19
logging.h
Normal file
19
logging.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef _LOGGING_H_
|
||||
#define _LOGGING_H_
|
||||
|
||||
#define LOG_DEBUG 5
|
||||
#define LOG_INFO 4
|
||||
#define LOG_NOTICE 3
|
||||
#define LOG_WARN 2
|
||||
#define LOG_ERROR 1
|
||||
#define LOG_CRIT 0
|
||||
|
||||
#define LOG_EVERYTIME 0
|
||||
|
||||
int log_init(const char *logfile);
|
||||
void log_close(void);
|
||||
void log_setprio(int prio);
|
||||
|
||||
int log_print(int prio, const char *fmt, ... );
|
||||
|
||||
#endif /* _LOGGING_H_ */
|
Loading…
Reference in New Issue
Block a user