Skip to content
Snippets Groups Projects
Commit fa9dde99 authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Controller error codes.

Commit refs #692.
parent 64f202cd
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@
#include "knot/common.h"
#include "knot/ctl/process.h"
#include "knot/conf/conf.h"
#include "knot/other/error.h"
char* pid_filename()
{
......@@ -32,7 +33,7 @@ pid_t pid_read(const char* fn)
if (fn) {
FILE *fp = fopen(fn, "r");
if (!fp) {
return PID_NOFILE;
return KNOT_ENOENT;
}
int readb = 0;
......@@ -48,26 +49,26 @@ pid_t pid_read(const char* fn)
// Check read result
if (readb < 1) {
return PID_EMPTY;
return KNOT_ENOENT;
}
// Convert pid
char* ep = 0;
unsigned long pid = strtoul(buf, &ep, 10);
if ((errno == ERANGE) || (*ep && !isspace(*ep))) {
return PID_INVAL;
return KNOT_ERANGE;
}
return (pid_t)pid;
}
return PID_NOFILE;
return KNOT_EINVAL;
}
int pid_write(const char* fn)
{
if (!fn) {
return PID_NOFILE;
return KNOT_EINVAL;
}
// Convert
......@@ -75,28 +76,31 @@ int pid_write(const char* fn)
int wbytes = 0;
wbytes = snprintf(buf, sizeof(buf), "%lu", (unsigned long) getpid());
if (wbytes < 0) {
return PID_INVAL;
return KNOT_EINVAL;
}
// Write
FILE *fp = fopen(fn, "w");
if (fp) {
int rc = fwrite(buf, wbytes, 1, fp);
fclose(fp);
if (rc < 0) {
return PID_NOFILE;
return KNOT_ERROR;
}
return 0;
}
return PID_NOFILE;
return KNOT_ENOENT;
}
int pid_remove(const char* fn)
{
return unlink(fn);
if (unlink(fn) < 0) {
return KNOT_EINVAL;
}
return KNOT_EOK;
}
int pid_running(pid_t pid)
......
......@@ -14,15 +14,6 @@
#include <unistd.h>
/* Constants. */
enum {
PID_NOFILE = -1, /* Cannot open file. */
PID_EMPTY = -2, /* File is empty. */
PID_INVAL = -3 /* Invalid conversion to/from string. */
};
/* PID handling */
/*!
* \brief Return a filename of the default compiled database file.
*
......@@ -37,7 +28,7 @@ char* pid_filename();
* \param fn Filename containing PID.
*
* \retval PID on success.
* \retval negative integer on error.
* \retval negative integer on error (EINVAL, ENOENT, ERANGE).
*/
pid_t pid_read(const char* fn);
......@@ -46,8 +37,8 @@ pid_t pid_read(const char* fn);
*
* \param fn Filename containing PID.
*
* \retval 0 on success.
* \retval negative integer on error.
* \retval 0 on success (EOK).
* \retval negative integer on error (ENOENT, EINVAL, ERROR).
*/
int pid_write(const char* fn);
......@@ -58,8 +49,8 @@ int pid_write(const char* fn);
*
* \warning Filename content won't be checked.
*
* \retval 0 on success.
* \retval negative integer on error.
* \retval 0 on success (EOK).
* \retval negative integer on error (EINVAL).
*/
int pid_remove(const char* fn);
......
......@@ -33,8 +33,9 @@ const error_table_t *error_lookup_by_id(const error_table_t *table, int id)
/*! \brief Table linking error messages to error codes. */
static const error_table_t knot_error_msgs[] = {
/* Mapped errors. */
{KNOT_EOK, "OK"},
{KNOT_ERROR, "Generic error."},
{KNOT_ENOMEM, "Not enough memory."},
{KNOT_EINVAL, "Invalid parameter passed."},
{KNOT_ENOTSUP, "Parameter not supported."},
......@@ -44,6 +45,11 @@ static const error_table_t knot_error_msgs[] = {
{KNOT_ECONNREFUSED, "Connection is refused."},
{KNOT_EISCONN, "Already connected."},
{KNOT_EADDRINUSE, "Address already in use."},
{KNOT_ENOENT, "Resource not found."},
{KNOT_ERANGE, "Value is out of range."},
/* Custom errors. */
{KNOT_ERROR, "Generic error."},
{KNOT_EADDRINVAL, "Invalid address."},
{KNOT_EZONEINVAL, "Invalid zone file."},
{KNOT_ENOTRUNNING, "Resource is not running."},
......
......@@ -31,7 +31,9 @@ typedef enum knot_error_t {
KNOT_EACCES = -EACCES, /*!< \brief Permission is denied. */
KNOT_ECONNREFUSED = -ECONNREFUSED, /*!< \brief Connection is refused. */
KNOT_EISCONN = -EISCONN, /*!< \brief Already connected. */
KNOT_EADDRINUSE = -EADDRINUSE, /*! \brief Address already in use. */
KNOT_EADDRINUSE = -EADDRINUSE, /*!< \brief Address already in use. */
KNOT_ENOENT = -ENOENT, /*!< \brief Resource not found. */
KNOT_ERANGE = -ERANGE, /*!< \brief Value is out of range. */
/* Custom error codes. */
KNOT_ERROR = -16384, /*!< \brief Generic error. */
......
......@@ -13,6 +13,7 @@
#include "knot/common.h"
#include "knot/server/tcp-handler.h"
#include "knot/server/name-server.h"
#include "knot/other/error.h"
#include "knot/stat/stat.h"
/*! \brief TCP connection pool. */
......@@ -381,6 +382,13 @@ int tcp_master(dthread_t *thread)
dt_unit_t *unit = thread->unit;
iohandler_t *handler = (iohandler_t *)thread->data;
int master_sock = handler->fd;
/* Check socket. */
if (master_sock < 0) {
debug_net("tcp_master: null socket recevied, finishing.\n");
return KNOT_EINVAL;
}
debug_dt("dthreads: [%p] is TCP master, state: %d\n",
thread, thread->state);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment