This commit is contained in:
2023-08-16 01:01:16 +02:00
parent f626118f14
commit e7e9e20410
16 changed files with 1341 additions and 1200 deletions

View File

@@ -4,15 +4,15 @@
*
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <cllexer.h>
#include <clparser.h>
@@ -27,7 +27,69 @@
int log_fd = 0;
static int cworker_readconf(cworker_t* worker) {
static int mkdirall(const char* path, mode_t mode) {
char buffer[1024];
size_t psize = strlen(path);
if (psize == 0) return 0;
if (psize == 1 && path[0] == '/') {
return 0;
}
for (int i = 1; i < psize; i++) {
if (path[i] == '/') {
strncpy(buffer, path, i);
buffer[i] = '\0';
if (mkdir(buffer, mode) < 0) {
if (errno != EEXIST) {
return -1;
}
}
}
}
return 0;
}
char* last(const char* path) {
char buffer[1024];
size_t psize = strlen(path);
strcpy(buffer, path);
buffer[psize] = '\0';
if (buffer[psize - 1] == '/') {
buffer[--psize] = '\0';
}
size_t pos = psize;
for (size_t i = 1; i < psize; i++) {
if (buffer[i] == '/') pos = i;
}
char* b = &buffer[++pos];
int bsize = strlen(b);
char* res = malloc(bsize + 1);
strcpy(res, b);
//printf("\n%s\n", res);
return res;
}
char* file(const char* path) {
char buffer[1024];
size_t psize = strlen(path);
strcpy(buffer, path);
buffer[psize] = '\0';
//if (buffer[psize - 1] == '/') {
// buffer[--psize] = '\0';
//}
size_t pos = psize;
for (size_t i = 1; i < psize; i++) {
if (buffer[i] == '/') pos = i;
}
char* b = &buffer[++pos];
int bsize = strlen(b);
char* res = malloc(bsize + 1);
strcpy(res, b);
//printf("\n%s\n", res);
return res;
}
static int cworker_readconf(const cworker_t* worker) {
log_debug("reading configiration");
int conf_fd = -1;
@@ -58,7 +120,7 @@ static int cworker_readconf(cworker_t* worker) {
return 0;
}
static int cworker_readopts(cworker_t* worker, char** argv, int argc) {
static int cworker_readopts(const cworker_t* worker, char** argv, int argc) {
log_debug("reading options");
cllexer_t lexer;
@@ -77,9 +139,13 @@ static int cworker_readopts(cworker_t* worker, char** argv, int argc) {
return 0;
}
static int cworker_openlog(cworker_t* worker) {
static int cworker_openlog(const cworker_t* worker) {
log_debug("redirect output");
if (mkdirall(srv_logpath, S_IRWXU|S_IRGRP|S_IXGRP) < 0) {
log_error("creating log dir error: %s %s", strerror(errno), srv_logpath);
}
if ((log_fd = open(srv_logpath, O_WRONLY|O_APPEND|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP)) < 0) {
log_error("open log file error %s %s", strerror(errno), srv_logpath);
return -1;
@@ -90,8 +156,14 @@ static int cworker_openlog(cworker_t* worker) {
return 0;
}
int cworker_writepid(cworker_t* worker) {
int cworker_writepid(const cworker_t* worker) {
log_debug("write pid file");
if (mkdirall(srv_runpath, S_IRWXU|S_IRGRP|S_IXGRP) < 0) {
log_error("createing run dir error: %s %s", strerror(errno), srv_runpath);
}
int pid_fd = -1;
if ((pid_fd = open(srv_runpath, O_WRONLY|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP)) < 0) {
log_error("open pid file error %s %s", strerror(errno), srv_runpath);
@@ -131,7 +203,7 @@ int cworker_init(cworker_t* worker, char** argv, int argc) {
return 0;
}
int cworker_detach(cworker_t* worker) {
int cworker_detach(const cworker_t* worker) {
log_debug("detach service");
int childpid = -1;
@@ -144,19 +216,17 @@ int cworker_detach(cworker_t* worker) {
// child
} else {
// parent
int status = 0;
waitpid(childpid, &status, 0);
exit(0);
}
return 0;
}
int cworker_build(cworker_t* worker) {
int cworker_build(const cworker_t* worker) {
log_debug("build service");
return 0;
}
int cworker_run(cworker_t* worker) {
int cworker_run(const cworker_t* worker) {
log_debug("run service");
return 0;
}