Compare commits

..

24 Commits

Author SHA1 Message Date
Nick Peng
d792e5f7f7 log: optimize log 2023-01-02 15:36:23 +08:00
Nick Peng
98ce7fd38c luci: fix translate typo 2023-01-01 09:37:10 +08:00
PikuZheng
f571b8714b fix Ubuntu service name (#1224)
Author: PikuZheng <cba321123@gmail.com>
Date:   Sat Dec 31 18:00:57 2022 +0800
2022-12-31 21:17:47 +08:00
Nick Peng
fffe4caf08 log: optimize log 2022-12-31 09:38:27 +08:00
Nick Peng
98498bf444 luci: fix option description 2022-12-31 09:27:38 +08:00
Nick Peng
69ab9585d7 log: optimize log 2022-12-30 21:13:50 +08:00
Nick Peng
dd9cf62d10 luci: fix UI not display after upgrade issue. 2022-12-28 19:45:37 +08:00
Nick Peng
58aaaa5d5b dns_server: use connect udp socket 2022-12-22 14:12:35 +08:00
孟古一
13a6892c17 Update ReadMe_en.md 2022-12-22 14:08:28 +08:00
孟古一
3099ef6ade readme: update readme for ubuntu installation (#1212) 2022-12-22 09:33:27 +08:00
Nick Peng
95524cab6b makefile: add extra cflags 2022-12-22 00:20:22 +08:00
Nick Peng
f7f1f37faa log: add option to set file permissions 2022-12-21 21:32:29 +08:00
Nick Peng
945653667f feature: support marking packet on upstream server 2022-12-21 18:40:55 +08:00
Nick Peng
4c2b8847f0 readme_en: fix link 2022-12-21 18:40:55 +08:00
Nick Peng
70df7938f3 conf: support disable expired cache on specific domain. 2022-12-21 18:40:55 +08:00
Nick Peng
4b42e1ef85 readme: update readme 2022-12-21 18:40:55 +08:00
Nick Peng
5bc8b3ad62 luci: support config speed check mode 2022-12-21 18:40:55 +08:00
Nick Peng
f300d6ba82 luci: add domain rule list tab 2022-12-21 18:40:55 +08:00
Nick Peng
cdf12f3cb4 luci: add report bugs button 2022-12-21 18:40:55 +08:00
Nick Peng
53593ba5b6 conf: support space in filename 2022-12-21 18:40:55 +08:00
Nick Peng
52e036ac96 dns_server: support force no cname for A,AAAA records 2022-12-21 18:40:55 +08:00
Nick Peng
0b723168bb luci-compat: support dns forwarding feature 2022-12-21 18:40:55 +08:00
Nick Peng
15427ffdf1 luci: support DNS forwarding and block 2022-12-21 18:40:55 +08:00
Nick Peng
3a1ba73386 log: support disable log 2022-12-14 22:26:45 +08:00
19 changed files with 198 additions and 63 deletions

View File

@@ -203,6 +203,10 @@ entware|ipkg update<br>ipkg install smartdns|软件源路径https://bin.entwa
# dpkg -i smartdns.1.yyyy.MM.dd-REL.arm-debian-all.deb
```
**对于Ubuntu系统**
* `systemd-resolved`会占用TCP53和UDP53端口。你需要手动解决端口占用问题或者修改smartdns监听端口
* 日志文件在`/var/log/smartdns/smartdns.log`
2. 修改配置
安装完成后,可配置 SmartDNS 的上游服务器信息。
@@ -578,11 +582,13 @@ entware|ipkg update<br>ipkg install smartdns|软件源路径https://bin.entwa
| log-level | 设置日志级别 | error | fatal、error、warn、notice、info 或 debug | log-level error |
| log-file | 日志文件路径 | /var/log/smartdns/smartdns.log | 合法路径字符串 | log-file /var/log/smartdns/smartdns.log |
| log-size | 日志大小 | 128K | 数字 + K、M 或 G | log-size 128K |
| log-num | 日志归档个数 | 2 | 大于等于 0 的数字 | log-num 2 |
| log-num | 日志归档个数 | openwrt为2 其他系统为8 | 大于等于 0 的数字0表示禁用日志 | log-num 2 |
| log-file-mode | 日志归档文件权限 | 0640 | 文件权限 | log-file-mode 644 |
| audit-enable | 设置审计启用 | no | [yes\|no] | audit-enable yes |
| audit-file | 审计文件路径 | /var/log/smartdns/smartdns-audit.log | 合法路径字符串 | audit-file /var/log/smartdns/smartdns-audit.log |
| audit-size | 审计大小 | 128K | 数字 + K、M 或 G | audit-size 128K |
| audit-num | 审计归档个数 | 2 | 大于等于 0 的数字 | audit-num 2 |
| audit-file-mode | 审计归档文件权限 | 0640 | 文件权限 | log-file-mode 644 |
| conf-file | 附加配置文件 | 无 | 合法路径字符串 | conf-file /etc/smartdns/smartdns.more.conf |
| server | 上游 UDP DNS | 无 | 可重复。<br>[ip][:port]:服务器 IP:端口(可选)<br>[-blacklist-ip]:配置 IP 过滤结果。<br>[-whitelist-ip]:指定仅接受参数中配置的 IP 范围<br>[-group [group] ...]DNS 服务器所属组,比如 office 和 foreign和 nameserver 配套使用<br>[-exclude-default-group]:将 DNS 服务器从默认组中排除。<br>[-set-mark]设置数据包标记so-mark| server 8.8.8.8:53 -blacklist-ip -group g1 |
| server-tcp | 上游 TCP DNS | 无 | 可重复。<br>[ip][:port]:服务器 IP:端口(可选)<br>[-blacklist-ip]:配置 IP 过滤结果<br>[-whitelist-ip]:指定仅接受参数中配置的 IP 范围。<br>[-group [group] ...]DNS 服务器所属组,比如 office 和 foreign和 nameserver 配套使用<br>[-exclude-default-group]:将 DNS 服务器从默认组中排除。<br>[-set-mark]设置数据包标记so-mark | server-tcp 8.8.8.8:53 |

View File

@@ -17,7 +17,7 @@ Support Raspberry Pi, openwrt, ASUS router, Windows and other devices.
- [Usage](#usage)
- [Use official installation source](#use-official-installation-source)
- [Download the package](#download-the-package)
- [Standard Linux system installation/Raspberry Pi, X86_64 system](#standard-linux-system-installationraspberry-pi-x86_64-system)
- [Standard Linux system installation/Raspberry Pi, X86\_64 system](#standard-linux-system-installationraspberry-pi-x86_64-system)
- [openwrt](#openwrt)
- [ASUS router native firmware / Merlin firmware](#asus-router-native-firmware--merlin-firmware)
- [optware/entware](#optwareentware)
@@ -195,6 +195,10 @@ https://github.com/pymumu/smartdns/releases
chmod +x ./install
./install -i
```
**For Ubuntu system:**
* `systemd-resolved` occupies TCP53 and UDP53 ports. You need to manually resolve the port occupancy problem or modify the SmartDNS listening port
* Log files in `/var/log/smartdns/smartdns.log`
1. Configuration
@@ -540,11 +544,13 @@ Note: Merlin firmware is derived from ASUS firmware and can theoretically be use
|log-level|log level|error|fatal,error,warn,notice,info,debug|log-level error
|log-file|log path|/var/log/smartdns/smartdns.log|File Pah|log-file /var/log/smartdns/smartdns.log
|log-size|log size|128K|number+K,M,G|log-size 128K
|log-num|archived log number|2|Integer|log-num 2
|log-num|archived log number|2 for openwrt, 8 for other system|Integer, 0 means turn off the log|log-num 2
|log-file-mode|archived log file mode|0640|Integer|log-file-mode 644
|audit-enable|audit log enable|no|[yes\|no]|audit-enable yes
|audit-file|audit log file|/var/log/smartdns/smartdns-audit.log|File Path|audit-file /var/log/smartdns/smartdns-audit.log
|audit-size|audit log size|128K|number+K,M,G|audit-size 128K
|audit-num|archived audit log number|2|Integer|audit-num 2
|audit-num|archived audit log number|2|Integer, 0 means turn off the log|audit-num 2
|audit-file-mode|archived audit log file mode|0640|Integer|audit-file-mode 644
|conf-file|additional conf file|None|File path|conf-file /etc/smartdns/smartdns.more.conf
|server|Upstream UDP DNS server|None|Repeatable <br>`[ip][:port]`: Server IP, port optional. <br>`[-blacklist-ip]`: The "-blacklist-ip" parameter is to filtering IPs which is configured by "blacklist-ip". <br>`[-whitelist-ip]`: whitelist-ip parameter specifies that only the IP range configured in whitelist-ip is accepted. <br>`[-group [group] ...]`: The group to which the DNS server belongs, such as office, foreign, use with nameserver. <br>`[-exclude-default-group]`: Exclude DNS servers from the default group. <br>`[-set-mark]`set mark on packets | server 8.8.8.8:53 -blacklist-ip
|server-tcp|Upstream TCP DNS server|None|Repeatable <br>`[ip][:port]`: Server IP, port optional. <br>`[-blacklist-ip]`: The "-blacklist-ip" parameter is to filtering IPs which is configured by "blacklist-ip". <br>`[-whitelist-ip]`: whitelist-ip parameter specifies that only the IP range configured in whitelist-ip is accepted. <br>`[-group [group] ...]`: The group to which the DNS server belongs, such as office, foreign, use with nameserver. <br>`[-exclude-default-group]`: Exclude DNS servers from the default group <br>`[-set-mark]`set mark on packets | server-tcp 8.8.8.8:53

View File

@@ -133,12 +133,13 @@ force-qtype-SOA 65
# log-level: [level], level=fatal, error, warn, notice, info, debug
# log-file: file path of log file.
# log-size: size of each log file, support k,m,g
# log-num: number of logs
# log-num: number of logs, 0 means disable log
log-level info
# log-file /var/log/smartdns/smartdns.log
# log-size 128k
# log-num 2
# log-file-mode [mode]: file mode of log file.
# dns audit
# audit-enable [yes|no]: enable or disable audit.
@@ -146,6 +147,7 @@ log-level info
# audit-SOA [yes|no]: enable or disable log soa result.
# audit-size size of each audit file, support k,m,g
# audit-file /var/log/smartdns-audit.log
# audit-file-mode [mode]: file mode of audit file.
# audit-size 128k
# audit-num 2

View File

@@ -429,7 +429,7 @@ msgid "Update Files"
msgstr "更新文件"
msgid "Upload Config File"
msgstr "上传域名列表文件"
msgstr "上传配置文件"
msgid "Upload Domain List File"
msgstr "上传域名列表文件"

View File

@@ -25,11 +25,11 @@ msgstr "自动设置Dnsmasq"
msgid "Automatically set as upstream of dnsmasq when port changes."
msgstr "端口更改时自动设为 dnsmasq 的上游。"
msgid "Block domain type"
msgstr "屏蔽域名类型"
msgid "Block domain"
msgstr "屏蔽域名"
msgid "Block domain type."
msgstr "屏蔽域名类型。"
msgid "Block domain."
msgstr "屏蔽域名。"
msgid "Cache Size"
msgstr "缓存大小"

View File

@@ -786,7 +786,7 @@ return view.extend({
so.rempty = true
so.root_directory = "/etc/smartdns/domain-set"
so = ss.option(form.ListValue, "block_domain_type", _("Block domain type"), _("Block domain type."));
so = ss.option(form.ListValue, "block_domain_type", _("Block domain"), _("Block domain."));
so.rmempty = true;
so.value("none", _("None"));
so.value("all", "IPv4/IPv6");

View File

@@ -655,6 +655,33 @@ download_file() {
fi
}
check_and_add_entry() {
local docommit=0
uci -q get smartdns.@smartdns[0] >/dev/null
if [ $? -ne 0 ]; then
uci -q add smartdns smartdns >/dev/null
docommit=1
fi
uci -q get smartdns.@domain-rule[0] >/dev/null
if [ $? -ne 0 ]; then
uci -q add smartdns domain-rule >/dev/null
docommit=1
fi
if [ "$docommit" = "1" ]; then
uci -q commit smartdns >/dev/null
fi
if [ ! -d "$SMARTDNS_DOMAIN_LIST_DOWNLOAD_DIR" ]; then
mkdir -p "$SMARTDNS_DOMAIN_LIST_DOWNLOAD_DIR"
fi
if [ ! -d "$SMARTDNS_CONF_DOWNLOAD_DIR" ]; then
mkdir -p "$SMARTDNS_CONF_DOWNLOAD_DIR"
fi
}
updatefiles() {
config_load "smartdns"
config_foreach download_file "download-file"
@@ -669,6 +696,7 @@ service_stopped()
start_service()
{
check_and_add_entry
config_load "smartdns"
config_foreach load_service "smartdns"
}

View File

@@ -24,6 +24,7 @@ CFLAGS =-O2 -g -Wall -Wstrict-prototypes -fno-omit-frame-pointer -Wstrict-aliasi
endif
override CFLAGS +=-Iinclude
override CFLAGS += -DBASE_FILE_NAME='"$(notdir $<)"'
override CFLAGS += $(EXTRA_CFLAGS)
ifdef VER
override CFLAGS += -DSMARTDNS_VERION='"$(VER)"'
endif

View File

@@ -1685,9 +1685,26 @@ static int _dns_client_create_socket_udp(struct dns_server_info *server_info)
goto errout;
}
if (set_fd_nonblock(fd, 1) != 0) {
tlog(TLOG_ERROR, "set socket non block failed, %s", strerror(errno));
goto errout;
}
server_info->fd = fd;
server_info->status = DNS_SERVER_STATUS_CONNECTIONLESS;
if (connect(fd, &server_info->addr, server_info->ai_addrlen) != 0) {
if (errno == ENETUNREACH || errno == EHOSTUNREACH || errno == ECONNREFUSED) {
tlog(TLOG_WARN, "connect %s failed, %s", server_info->ip, strerror(errno));
goto errout;
}
if (errno != EINPROGRESS) {
tlog(TLOG_ERROR, "connect %s failed, %s", server_info->ip, strerror(errno));
goto errout;
}
}
memset(&event, 0, sizeof(event));
event.events = EPOLLIN;
event.data.ptr = server_info;
@@ -1765,7 +1782,7 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info)
set_sock_keepalive(fd, 15, 3, 4);
if (connect(fd, &server_info->addr, server_info->ai_addrlen) != 0) {
if (errno == ENETUNREACH) {
if (errno == ENETUNREACH || errno == EHOSTUNREACH) {
tlog(TLOG_DEBUG, "connect %s failed, %s", server_info->ip, strerror(errno));
goto errout;
}
@@ -1854,7 +1871,7 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info, ch
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
if (connect(fd, &server_info->addr, server_info->ai_addrlen) != 0) {
if (errno == ENETUNREACH) {
if (errno == ENETUNREACH || errno == EHOSTUNREACH) {
tlog(TLOG_DEBUG, "connect %s failed, %s", server_info->ip, strerror(errno));
goto errout;
}
@@ -1972,8 +1989,17 @@ static int _dns_client_process_udp(struct dns_server_info *server_info, struct e
len = recvmsg(server_info->fd, &msg, MSG_DONTWAIT);
if (len < 0) {
tlog(TLOG_ERROR, "recvfrom failed, %s\n", strerror(errno));
return -1;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
if (errno == ECONNREFUSED) {
tlog(TLOG_DEBUG, "recvfrom %s failed, %s\n", server_info->ip, strerror(errno));
goto errout;
}
tlog(TLOG_ERROR, "recvfrom %s failed, %s\n", server_info->ip, strerror(errno));
goto errout;
}
from_len = msg.msg_namelen;
@@ -2004,6 +2030,15 @@ static int _dns_client_process_udp(struct dns_server_info *server_info, struct e
}
return 0;
errout:
pthread_mutex_lock(&client.server_list_lock);
server_info->recv_buff.len = 0;
server_info->send_buff.len = 0;
_dns_client_close_socket(server_info);
pthread_mutex_unlock(&client.server_list_lock);
return -1;
}
static int _dns_client_socket_ssl_send(struct dns_server_info *server, const void *buf, int num)
@@ -2273,16 +2308,17 @@ static int _dns_client_process_tcp(struct dns_server_info *server_info, struct e
len = _dns_client_socket_recv(server_info);
if (len < 0) {
/* no data to recv, try again */
if (errno == EAGAIN) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
/* FOR GFW */
if (errno == ECONNRESET) {
if (errno == ECONNRESET || errno == ENETUNREACH || errno == EHOSTUNREACH) {
tlog(TLOG_DEBUG, "recv failed, server %s:%d, %s\n", server_info->ip, server_info->port,
strerror(errno));
goto errout;
}
if (errno == ETIMEDOUT) {
if (errno == ETIMEDOUT || errno == ECONNREFUSED) {
tlog(TLOG_INFO, "recv failed, server %s:%d, %s\n", server_info->ip, server_info->port, strerror(errno));
goto errout;
}
@@ -2674,7 +2710,7 @@ static int _dns_client_send_udp(struct dns_server_info *server_info, void *packe
return -1;
}
send_len = sendto(server_info->fd, packet, len, 0, &server_info->addr, server_info->ai_addrlen);
send_len = sendto(server_info->fd, packet, len, 0, NULL, 0);
if (send_len != len) {
return -1;
}

View File

@@ -105,6 +105,7 @@ int dns_conf_log_level = TLOG_ERROR;
char dns_conf_log_file[DNS_MAX_PATH];
size_t dns_conf_log_size = 1024 * 1024;
int dns_conf_log_num = 8;
int dns_conf_log_file_mode;
/* CA file */
char dns_conf_ca_file[DNS_MAX_PATH];
@@ -119,6 +120,7 @@ int dns_conf_audit_log_SOA;
char dns_conf_audit_file[DNS_MAX_PATH];
size_t dns_conf_audit_size = 1024 * 1024;
int dns_conf_audit_num = 2;
int dns_conf_audit_file_mode;
/* address rules */
art_tree dns_conf_domain_rule;
@@ -2420,9 +2422,11 @@ static struct config_item _config_item[] = {
CONF_STRING("log-file", (char *)dns_conf_log_file, DNS_MAX_PATH),
CONF_SIZE("log-size", &dns_conf_log_size, 0, 1024 * 1024 * 1024),
CONF_INT("log-num", &dns_conf_log_num, 0, 1024),
CONF_INT_BASE("log-file-mode", &dns_conf_log_file_mode, 0, 511, 8),
CONF_YESNO("audit-enable", &dns_conf_audit_enable),
CONF_YESNO("audit-SOA", &dns_conf_audit_log_SOA),
CONF_STRING("audit-file", (char *)&dns_conf_audit_file, DNS_MAX_PATH),
CONF_INT_BASE("audit-file-mode", &dns_conf_audit_file_mode, 0, 511, 8),
CONF_SIZE("audit-size", &dns_conf_audit_size, 0, 1024 * 1024 * 1024),
CONF_INT("audit-num", &dns_conf_audit_num, 0, 1024),
CONF_INT("rr-ttl", &dns_conf_rr_ttl, 0, CONF_INT_MAX),

View File

@@ -350,6 +350,7 @@ extern int dns_conf_log_level;
extern char dns_conf_log_file[DNS_MAX_PATH];
extern size_t dns_conf_log_size;
extern int dns_conf_log_num;
extern int dns_conf_log_file_mode;;
extern char dns_conf_ca_file[DNS_MAX_PATH];
extern char dns_conf_ca_path[DNS_MAX_PATH];
@@ -367,6 +368,7 @@ extern int dns_conf_audit_log_SOA;
extern char dns_conf_audit_file[DNS_MAX_PATH];
extern size_t dns_conf_audit_size;
extern int dns_conf_audit_num;
extern int dns_conf_audit_file_mode;
extern char dns_conf_server_name[DNS_MAX_SERVER_NAME_LEN];
extern art_tree dns_conf_domain_rule;

View File

@@ -5532,6 +5532,10 @@ static int _dns_server_audit_init(void)
return -1;
}
if (dns_conf_audit_file_mode > 0) {
tlog_set_permission(dns_audit, dns_conf_audit_file_mode, dns_conf_audit_file_mode);
}
return 0;
}

View File

@@ -521,7 +521,7 @@ static int _fast_ping_sendping_v6(struct ping_host_struct *ping_host)
ping_host->addr_len);
if (len < 0 || len != sizeof(struct fast_ping_packet)) {
int err = errno;
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL) {
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL || errno == EHOSTUNREACH) {
goto errout;
}
@@ -668,7 +668,7 @@ static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host)
if (connect(fd, &ping_host->addr, ping_host->addr_len) != 0) {
if (errno != EINPROGRESS) {
char ping_host_name[PING_MAX_HOSTLEN];
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL) {
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL || errno == EHOSTUNREACH) {
goto errout;
}

View File

@@ -49,6 +49,13 @@ struct config_item_int {
int max;
};
struct config_item_int_base {
int *data;
int min;
int max;
int base;
};
struct config_item_string {
char *data;
size_t size;
@@ -81,6 +88,13 @@ struct config_enum {
.data = value, .min = min_value, .max = max_value \
} \
}
#define CONF_INT_BASE(key, value, min_value, max_value, base_value) \
{ \
key, conf_int_base, &(struct config_item_int_base) \
{ \
.data = value, .min = min_value, .max = max_value, .base = base_value \
} \
}
#define CONF_STRING(key, value, len_value) \
{ \
key, conf_string, &(struct config_item_string) \
@@ -131,6 +145,8 @@ extern int conf_custom(const char *item, void *data, int argc, char *argv[]);
extern int conf_int(const char *item, void *data, int argc, char *argv[]);
extern int conf_int_base(const char *item, void *data, int argc, char *argv[]);
extern int conf_string(const char *item, void *data, int argc, char *argv[]);
extern int conf_yesno(const char *item, void *data, int argc, char *argv[]);

View File

@@ -87,6 +87,27 @@ int conf_int(const char *item, void *data, int argc, char *argv[])
return 0;
}
int conf_int_base(const char *item, void *data, int argc, char *argv[])
{
struct config_item_int_base *item_int = data;
int value = 0;
if (argc < 2) {
return -1;
}
value = strtol(argv[1], NULL, item_int->base);
if (value < item_int->min) {
value = item_int->min;
} else if (value > item_int->max) {
value = item_int->max;
}
*(item_int->data) = value;
return 0;
}
int conf_string(const char *item, void *data, int argc, char *argv[])
{
struct config_item_string *item_string = data;

View File

@@ -361,6 +361,9 @@ static int _smartdns_init(void)
tlog_setlogscreen(verbose_screen);
tlog_setlevel(dns_conf_log_level);
if (dns_conf_log_file_mode > 0) {
tlog_set_permission(tlog_get_root(), dns_conf_log_file_mode, dns_conf_log_file_mode);
}
tlog(TLOG_NOTICE, "smartdns starting...(Copyright (C) Nick Peng <pymumu@gmail.com>, build: %s %s)", __DATE__,
__TIME__);

View File

@@ -79,9 +79,9 @@ struct tlog_log {
int zip_pid;
int multi_log;
int logscreen;
int no_write_log;
int segment_log;
int max_line_size;
int print_errmsg;
tlog_output_func output_func;
void *private_data;
@@ -90,6 +90,7 @@ struct tlog_log {
time_t last_waitpid;
mode_t file_perm;
mode_t archive_perm;
int mode_changed;
int waiters;
int is_exit;
@@ -315,10 +316,24 @@ void tlog_set_maxline_size(struct tlog_log *log, int size)
log->max_line_size = size;
}
void tlog_logcount(struct tlog_log *log, int count)
{
if (log == NULL) {
return;
}
if (count < 0) {
count = 0;
}
log->logcount = count;
}
void tlog_set_permission(struct tlog_log *log, unsigned int file, unsigned int archive)
{
log->file_perm = file;
log->archive_perm = archive;
log->mode_changed = 1;
}
int tlog_localtime(struct tlog_time *tm)
@@ -505,6 +520,10 @@ static int _tlog_vprintf(struct tlog_log *log, vprint_callback print_callback, v
return -1;
}
if (unlikely(log->logcount <= 0 && log->logscreen == 0) ) {
return 0;
}
if (_tlog_need_drop(log) == 0) {
return -1;
}
@@ -1130,7 +1149,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
unused = write(STDOUT_FILENO, buff, bufflen);
}
if (log->no_write_log) {
if (log->logcount <= 0) {
return 0;
}
@@ -1153,7 +1172,6 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
if (log->fd <= 0) {
/* open a new log file to write */
static int print_errmsg = 1;
time_t now;
time(&now);
@@ -1164,14 +1182,15 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
char logfile[PATH_MAX * 2];
if (_tlog_mkdir(log->logdir) != 0) {
if (print_errmsg == 0) {
if (log->print_errmsg == 0) {
return -1;
}
print_errmsg = 0;
log->print_errmsg = 0;
fprintf(stderr, "create log dir %s failed, %s\n", log->logdir, strerror(errno));
if (errno == EACCES && log->logscreen == 0) {
fprintf(stderr, "no permission to write log file, output log to console\n");
tlog_logscreen_only(log, 1);
tlog_logscreen(log, 1);
tlog_logcount(log, 0);
}
return -1;
}
@@ -1179,17 +1198,21 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
log->filesize = 0;
log->fd = open(logfile, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, log->file_perm);
if (log->fd < 0) {
if (print_errmsg == 0) {
if (log->print_errmsg == 0) {
return -1;
}
fprintf(stderr, "open log file %s failed, %s\n", logfile, strerror(errno));
print_errmsg = 0;
log->print_errmsg = 0;
return -1;
}
if (log->mode_changed != 0) {
fchmod(log->fd, log->file_perm);
}
log->last_try = 0;
print_errmsg = 1;
log->print_errmsg = 1;
/* get log file size */
log->filesize = lseek(log->fd, 0, SEEK_END);
}
@@ -1577,6 +1600,11 @@ const char *tlog_get_level_string(tlog_level level)
return tlog_level_str[level];
}
void tlog_set_maxlog_count(int count)
{
tlog_logcount(tlog.root, count);
}
static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
{
if (log == NULL) {
@@ -1586,26 +1614,11 @@ static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
log->logscreen = (enable != 0) ? 1 : 0;
}
static void _tlog_log_setlogscreen_only(struct tlog_log *log, int enable)
{
if (log == NULL) {
return;
}
log->logscreen = (enable != 0) ? 1 : 0;
log->no_write_log = (enable != 0) ? 1 : 0;
}
void tlog_setlogscreen(int enable)
{
_tlog_log_setlogscreen(tlog.root, enable);
}
void tlog_setlogscreen_only(int enable)
{
_tlog_log_setlogscreen_only(tlog.root, enable);
}
int tlog_write_log(char *buff, int bufflen)
{
if (unlikely(tlog.root == NULL)) {
@@ -1624,15 +1637,6 @@ void tlog_logscreen(tlog_log *log, int enable)
_tlog_log_setlogscreen(log, enable);
}
void tlog_logscreen_only(tlog_log *log, int enable)
{
if (log == NULL) {
return;
}
_tlog_log_setlogscreen_only(log, enable);
}
int tlog_reg_output_func(tlog_log *log, tlog_output_func output)
{
if (log == NULL) {
@@ -1704,12 +1708,13 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
log->dropped = 0;
log->buffsize = (buffsize > 0) ? buffsize : TLOG_BUFF_SIZE;
log->logsize = (maxlogsize >= 0) ? maxlogsize : TLOG_LOG_SIZE;
log->logcount = (maxlogcount > 0) ? maxlogcount : TLOG_LOG_COUNT;
log->logcount = (maxlogcount <= 0) ? 0 : maxlogcount;
log->fd = -1;
log->filesize = 0;
log->zip_pid = -1;
log->is_exit = 0;
log->fail = 0;
log->print_errmsg = 1;
log->waiters = 0;
log->block = ((flag & TLOG_NONBLOCK) == 0) ? 1 : 0;
log->nocompress = ((flag & TLOG_NOCOMPRESS) == 0) ? 0 : 1;

View File

@@ -104,15 +104,15 @@ extern void tlog_set_logfile(const char *logfile);
/* enalbe log to screen */
extern void tlog_setlogscreen(int enable);
/* output log to screen only */
extern void tlog_setlogscreen_only(int enable);
/* enalbe early log to screen */
extern void tlog_set_early_printf(int enable);
/* Get log level in string */
extern const char *tlog_get_level_string(tlog_level level);
/* set max log count */
extern void tlog_set_maxlog_count(int count);
/*
Function: Initialize log module
logfile: log file.
@@ -187,9 +187,6 @@ extern int tlog_vprintf(tlog_log *log, const char *format, va_list ap);
/* enalbe log to screen */
extern void tlog_logscreen(tlog_log *log, int enable);
/* enalbe log to screen only*/
extern void tlog_logscreen_only(tlog_log *log, int enable);
/* register output callback */
typedef int (*tlog_output_func)(struct tlog_log *log, const char *buff, int bufflen);
extern int tlog_reg_output_func(tlog_log *log, tlog_output_func output);
@@ -206,6 +203,9 @@ extern int tlog_localtime(struct tlog_time *tm);
/* set max line size */
extern void tlog_set_maxline_size(struct tlog_log *log, int size);
/* set max log count */
extern void tlog_logcount(struct tlog_log *log, int count);
/*
Function: set log file and archive permission
log: log stream

View File

@@ -1468,7 +1468,8 @@ int dns_packet_debug(const char *packet_file)
struct _dns_read_packet_info *info = NULL;
char buff[DNS_PACKSIZE];
tlog_setlogscreen_only(1);
tlog_set_maxlog_count(0);
tlog_setlogscreen(1);;
tlog_setlevel(TLOG_DEBUG);
info = _dns_read_packet_file(packet_file);