mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-04-19 08:16:20 +00:00
Improve error reports for write_str() etc.
Now write_str(), write_nonblock(), write_all(), set_nonblock() etc. report the file/line/function of their caller, rather than the function in net.c. Done using macros, similar in style to WHY() etc.
This commit is contained in:
parent
86eb482ed9
commit
7c2090e61c
65
net.c
65
net.c
@ -19,37 +19,49 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include "serval.h"
|
||||
|
||||
int set_nonblock(int fd)
|
||||
int _set_nonblock(int fd, const char *file, unsigned int line, const char *function)
|
||||
{
|
||||
int flags;
|
||||
if ((flags = fcntl(fd, F_GETFL, NULL)) == -1)
|
||||
return WHY_perror("fcntl(F_GETFL)");
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
|
||||
return WHY_perror("fcntl(F_SETFL)");
|
||||
if ((flags = fcntl(fd, F_GETFL, NULL)) == -1) {
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "set_nonblock: fcntl(%d,F_GETFL,NULL)", fd);
|
||||
return -1;
|
||||
}
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "set_nonblock: fcntl(%d,F_SETFL,0x%x|O_NONBLOCK)", fd, flags);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_block(int fd)
|
||||
int _set_block(int fd, const char *file, unsigned int line, const char *function)
|
||||
{
|
||||
int flags;
|
||||
if ((flags = fcntl(fd, F_GETFL, NULL)) == -1)
|
||||
return WHY_perror("fcntl(F_GETFL)");
|
||||
if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1)
|
||||
return WHY_perror("fcntl(F_SETFL)");
|
||||
if ((flags = fcntl(fd, F_GETFL, NULL)) == -1) {
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "set_block: fcntl(%d,F_GETFL,NULL)", fd);
|
||||
return -1;
|
||||
}
|
||||
if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1) {
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "set_block: fcntl(%d,F_SETFL,0x%x&~O_NONBLOCK)", fd, flags);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_all(int fd, const char *buf, size_t len)
|
||||
int _write_all(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function)
|
||||
{
|
||||
ssize_t written = write(fd, buf, len);
|
||||
if (written == -1)
|
||||
return WHY_perror("write");
|
||||
if (written != len)
|
||||
return WHYF("write(%u bytes) returned %d", len, written);
|
||||
if (written == -1) {
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_all: write(%d,%p,%lu)", fd, buf, (unsigned long)len);
|
||||
return -1;
|
||||
}
|
||||
if (written != len) {
|
||||
logMessage(LOG_LEVEL_ERROR, file, line, function, "write_all: write(%d,%p,%lu) returned %ld", fd, buf, (unsigned long)len, (long)written);
|
||||
return -1;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
int write_nonblock(int fd, const char *buf, size_t len)
|
||||
int _write_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function)
|
||||
{
|
||||
ssize_t written = write(fd, buf, len);
|
||||
if (written == -1) {
|
||||
@ -58,25 +70,28 @@ int write_nonblock(int fd, const char *buf, size_t len)
|
||||
case EINTR:
|
||||
return 0;
|
||||
}
|
||||
return WHY_perror("write");
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_nonblock: write(%d,%p,%lu)", fd, buf, (unsigned long)len);
|
||||
return -1;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
int write_all_nonblock(int fd, const char *buf, size_t len)
|
||||
int _write_all_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function)
|
||||
{
|
||||
ssize_t written = write_nonblock(fd, buf, len);
|
||||
if (written != -1 && written != len)
|
||||
return WHYF("write(%u bytes) returned %d", len, written);
|
||||
ssize_t written = _write_nonblock(fd, buf, len, file, line, function);
|
||||
if (written != -1 && written != len) {
|
||||
logMessage(LOG_LEVEL_ERROR, file, line, function, "write_all_nonblock: write(%d,%p,%lu) returned %ld", fd, buf, (unsigned long)len, (long)written);
|
||||
return -1;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
int write_str(int fd, const char *str)
|
||||
int _write_str(int fd, const char *str, const char *file, unsigned int line, const char *function)
|
||||
{
|
||||
return write_all(fd, str, strlen(str));
|
||||
return _write_all(fd, str, strlen(str), file, line, function);
|
||||
}
|
||||
|
||||
int write_str_nonblock(int fd, const char *str)
|
||||
int _write_str_nonblock(int fd, const char *str, const char *file, unsigned int line, const char *function)
|
||||
{
|
||||
return write_all_nonblock(fd, str, strlen(str));
|
||||
return _write_all_nonblock(fd, str, strlen(str), file, line, function);
|
||||
}
|
||||
|
24
serval.h
24
serval.h
@ -798,6 +798,8 @@ int dump(char *name, unsigned char *addr, size_t len);
|
||||
const char *trimbuildpath(const char *s);
|
||||
|
||||
#define LOGF(L,F,...) (logMessage(L, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__))
|
||||
#define logMessage_perror(L,file,line,func,F,...) \
|
||||
(logMessage(L, file, line, func, F ": %s [errno=%d]", ##__VA_ARGS__, strerror(errno), errno))
|
||||
|
||||
#define FATALF(F,...) do { LOGF(LOG_LEVEL_FATAL, F, ##__VA_ARGS__); exit(-1); } while (1)
|
||||
#define FATAL(X) FATALF("%s", (X))
|
||||
@ -1560,13 +1562,21 @@ void sigIoHandler(int signal);
|
||||
#define DEFAULT_MONITOR_SOCKET_NAME "org.servalproject.servald.monitor.socket"
|
||||
#define DEFAULT_MDP_SOCKET_NAME "org.servalproject.servald.mdp.socket"
|
||||
|
||||
int set_nonblock(int fd);
|
||||
int set_block(int fd);
|
||||
int write_all(int fd, const char *buf, size_t len);
|
||||
int write_nonblock(int fd, const char *buf, size_t len);
|
||||
int write_all_nonblock(int fd, const char *buf, size_t len);
|
||||
int write_str(int fd, const char *str);
|
||||
int write_str_nonblock(int fd, const char *str);
|
||||
#define set_nonblock(fd) (_set_nonblock(fd, __FILE__, __LINE__, __FUNCTION__))
|
||||
#define set_block(fd) (_set_block(fd, __FILE__, __LINE__, __FUNCTION__))
|
||||
#define write_all(fd,buf,len) (_write_all(fd, buf, len, __FILE__, __LINE__, __FUNCTION__))
|
||||
#define write_nonblock(fd,buf,len) (_write_nonblock(fd, buf, len, __FILE__, __LINE__, __FUNCTION__))
|
||||
#define write_all_nonblock(fd,buf,len) (_write_all_nonblock(fd, buf, len, __FILE__, __LINE__, __FUNCTION__))
|
||||
#define write_str(fd,str) (_write_str(fd, str, __FILE__, __LINE__, __FUNCTION__))
|
||||
#define write_str_nonblock(fd,str) (_write_str_nonblock(fd, str, __FILE__, __LINE__, __FUNCTION__))
|
||||
|
||||
int _set_nonblock(int fd, const char *file, unsigned int line, const char *function);
|
||||
int _set_block(int fd, const char *file, unsigned int line, const char *function);
|
||||
int _write_all(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function);
|
||||
int _write_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function);
|
||||
int _write_all_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function);
|
||||
int _write_str(int fd, const char *str, const char *file, unsigned int line, const char *function);
|
||||
int _write_str_nonblock(int fd, const char *str, const char *file, unsigned int line, const char *function);
|
||||
|
||||
int rhizome_http_server_start();
|
||||
int overlay_mdp_setup_sockets();
|
||||
|
Loading…
x
Reference in New Issue
Block a user