-I/usr/include/guile/2.0 \
-lguile-2.0
-tomc:./src/tomc/main.c
- gcc -o tomc ./src/tomc/main.c
+tomc:./src/tomc/main.c ./src/tomc/socket_read.c
+ gcc -o tomc ./src/tomc/main.c ./src/tomc/socket_read.c
char *cmd;
char *args[10];
pid_t pid;
+ int last_status;
/* todo */
/* enum trigger start_trigger; */
/* enum trigger end_trigger; */
/* SUMMARY: */
/* Run each job in sequence via fork + execv */
void run_jobs(void);
+
+/* SUMMARY: */
+/* Lookup in the job table the job with a given name */
+struct job *lookup_job(char *buf);
+
+/* for tomc */
+void setup_socket(int sfd);
+void socket_read(void (*func)(char *line));
}
}
-void lookup_job(char *my_job_name)
+struct job *lookup_job(char *my_job_name)
{
-
+ for(int i = 0;
+ i < MAX_JOBS;
+ i++){
+ if(jobs[i].name == NULL){
+ continue;
+ }
+ if(strcmp(jobs[i].name, my_job_name) == 0){
+ return &jobs[i];
+ }
+ }
+ return NULL;
}
void run_jobs(void)
#include <unistd.h>
#include "../../include/macros.h"
+#include "../../include/manifest.h"
static int sfd;
perror("connect");
exit(EXIT_FAILURE);
}
+ setup_socket(sfd);
}
static char *default_name = "no_name";
int write_len = strlen(write_buf); \
ssize_t wrote = \
write(sfd, write_buf, write_len); \
- tomc_p("wrote '%s' (%d)", write_buf, write_len); \
+ /* tomc_p("wrote '%s' (%d)", write_buf, write_len); */ \
if(wrote != write_len){ \
perror("[tomc] write size mismatch"); \
exit(EXIT_FAILURE); \
}}
+int last_read_size = 0;
+
+static void do_read(char *line)
+{
+ printf("got %s as input.\n", line);
+ strcpy(socket_buf, line);
+ /* printf("socket_buf: %s\n", socket_buf); */
+}
#define SOCK_READ { \
- int size = read(sfd, socket_buf, 100); \
- if(read == 0) { \
- tomc_p("didn't actually get anything."); \
- socket_buf[0] = '\0'; \
- } \
- socket_buf[size] = '\0'; \
+ socket_read(do_read); \
}
+ /* int size = read(sfd, socket_buf, 100); \ */
+ /* if(size == 0) { \ */
+ /* tomc_p("didn't actually get anything."); \ */
+ /* socket_buf[0] = '\0'; \ */
+ /* }else{ \ */
+ /* tomc_p("__debug__:size=%d", size); \ */
+ /* } \ */
+ /* socket_buf[size] = '\0'; \ */
+ /* tomc_p("__debug__: '%s'", socket_buf);\ */
+ /* last_read_size = size;\ */
+ /* } */
#define SOCK_READ_X(X){ \
SOCK_READ; \
if(strcmp(socket_buf, X) != 0){ \
tomc_p("protocol error. instead of ACK we got '%s'", socket_buf); \
exit(EXIT_FAILURE); \
} \
- tomc_p("got an ACK"); \
}
#define SOCK_ACK {SOCK_READ_X("ACK")};
#define SOCK_REQUEST(X) { \
SOCK_ACK; \
SOCK_WRITE(options.name); \
SOCK_ACK; \
- SOCK_READ; \
- tomc_p("tomd reports '%s'", socket_buf); \
}
static void write_client(void)
if(options.status){
SOCK_REQUEST("status");
}
+
+ while(1){
+ printf("looking for that last ack.\n");
+ SOCK_READ;
+ if(strcmp(socket_buf, "ACK") == 0){
+ tomc_p("got the ending ack.");
+ break;
+ }else{
+ printf("in else.\n");
+ /* int len = strlen(socket_buf); */
+ /* int total = len; */
+ /* if(last_read_size == 0){ */
+ /* continue; */
+ /* } */
+ tomc_p(socket_buf);
+ /* while(total != last_read_size -1){ */
+ /* tomc_p("__debug__:total=%d, size=%d", total, last_read_size); */
+ /* char *ptr = socket_buf + total; */
+ /* tomc_p("'%s'", ptr); */
+ /* total += strlen(ptr); */
+ /* } */
+ }
+ }
}
int main(int argc, char **argv)
print_options();
init();
write_requests();
+
+ tomc_p("dying of our own accord.");
return EXIT_SUCCESS;
}
--- /dev/null
+/* Copyright (C) 2018 Thomas Balzer */
+
+/* This file is part of tomd. */
+
+/* tomd 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 3 of the License, or */
+/* (at your option) any later version. */
+
+/* tomd 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 tomd. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+
+#define MAX_LEN 1024
+
+struct line{
+ char *buf;
+ struct line *next;
+};
+
+static struct{
+ char *buf;
+ int sfd;
+ struct line *lines;
+}options;
+
+void setup_socket(int sfd)
+{
+ options.sfd = sfd;
+ options.buf = malloc(sizeof(char) * MAX_LEN);
+}
+
+static struct line *find_lines(void)
+{
+ /* printf("entering find_lines\n"); */
+ struct line *lines = malloc(sizeof(struct line));
+ struct line *cur_line = lines;
+ struct line *old_line = lines;
+ char *ptr = options.buf;
+ lines->buf = options.buf;
+ lines->next = NULL;
+
+ /* printf("starting loop.\n"); */
+ while(1){
+ if(*ptr == '\n' || *ptr == '\0'){
+ /* printf("----\n"); */
+ /* printf("found a new line.\n"); */
+ /* printf("'%c' -> \\0\n", *ptr); */
+ /* printf("'next \%c': %c\n", *(ptr+1)); */
+ if(*ptr == '\0'){
+ /* end of buf */
+ /* printf("found the end. (1)\n"); */
+ return lines;
+ }
+
+ *ptr = '\0';
+ ptr++;
+
+ while(*ptr == '\n'){
+ /* empty, discard */
+ /* printf("empty\n"); */
+ *ptr = '\0';
+ ptr++;
+ }
+
+ if(*ptr == '\0'){
+ /* end of buf */
+ /* printf("found the end. (2)\n"); */
+ return lines;
+ }
+
+ /* printf("hallo there.\n"); */
+ cur_line = malloc(sizeof(struct line));
+ old_line->next = cur_line;
+ old_line = cur_line;
+ cur_line->buf = ptr;
+ }
+ printf("________|%c|\n", *ptr);
+ ptr++;
+ }
+}
+
+void socket_read(void (*func)(char *line))
+{
+ if(options.lines != NULL){
+ printf("|||| already got some input. |||\n");
+ goto call_func;
+ }
+
+ int size;
+ do_read:
+ printf("||||| doing read. ||||\n");
+ size = read(options.sfd, options.buf, MAX_LEN);
+ printf("||||| got read. ||||\n");
+ if(size == 0){
+ printf("||||| reading again. ||||\n");
+ goto do_read;
+ }else{
+ printf("||||| done read. ||||\n");
+ options.lines = find_lines();
+
+ struct line *cur_line = options.lines;
+
+ int count = 0;
+ while(1){
+ printf("[%d] '%s'\n", count++, cur_line->buf);
+ if(cur_line->next == NULL){
+ printf("found the end.\n");
+ break;
+ }
+ cur_line = cur_line->next;
+ }
+ }
+
+ call_func:
+ printf("calling function with %s\n", options.lines->buf);
+ func(options.lines->buf);
+ options.lines = options.lines->next;
+ return;
+}
+
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/socket.h>
+#include <sys/wait.h>
#include "../../include/macros.h"
#include "../../include/manifest.h"
+#include "../../include/job.h"
static void header(void)
{
#define SEND_ACK { \
int size; \
- if(size = write(asfd, "ACK", sizeof "ACK") != sizeof "ACK") { \
- tomd_p("didn't send as much as we though (%d != %d)", size, sizeof "ACK"); \
+ if(size = write(asfd, "ACK\n", sizeof "ACK\n") != sizeof "ACK\n") { \
+ tomd_p("didn't send as much as we though (%d != %d)", size, sizeof "ACK\n"); \
} \
}
// cross reference given name against known services
tomd_p("looking up '%s'", buf);
SEND_ACK;
- /* lookup_job(buf); */
+ struct job *jp = lookup_job(buf);
+ #define DOUBLE_DUTY(...) { \
+ char tmp[100]; \
+ tomd_p(__VA_ARGS__); \
+ sprintf(tmp, __VA_ARGS__); \
+ int tmplen = strlen(tmp); \
+ tomd_p("__debug__:'%s'", tmp);\
+ write(asfd, tmp, tmplen); \
+ }
+ if(jp == NULL){
+ DOUBLE_DUTY("lookup of '%s' failed.\n", buf);
+ } else {
+
+ tomd_p("-----");
+ DOUBLE_DUTY("found job record for '%s'\n", jp->name);
+
+ if(request == STATUS){
+ if(jp->pid != -1){
+ int status = 0;
+ int n_pid = waitpid(jp->pid, &status, WNOHANG);
+ if(n_pid < 0){
+ perror("[tomd] waitpid");
+ }else if(n_pid == 0){
+ DOUBLE_DUTY("waitpid == 0, check manual\n");
+ }else{
+ DOUBLE_DUTY("running: %s\n",
+ !WIFEXITED(status) ? "yes"
+ : "no");
+
+ if(WIFEXITED(status)){
+ DOUBLE_DUTY(" status: %d\n", WEXITSTATUS(status));
+ jp->pid = -1;
+ jp->last_status = status;
+ }
+ }
+ }else{
+ DOUBLE_DUTY("running: no\n");
+ DOUBLE_DUTY(" status: %d\n", jp->last_status);
+ }
+
+ if(jp->pid != -1){
+ DOUBLE_DUTY(" pid: %d\n", jp->pid);
+ }
+ }else{
+ DOUBLE_DUTY("don't know how to handle that request.\n");
+ }
+ }
+ tomd_p("-----");
+ tomd_p("sending ack.");
+ SEND_ACK;
+ tomd_p("ack is sent.");
}
static void handle_connection(int asfd)
tomd_p("validated client");
handle_request(asfd, buf);
- close(asfd);
+ shutdown(asfd, SHUT_WR);
}
static void run(void)
header();
init();
run_jobs();
- daemonize();
+ /* daemonize(); */
run();
cleanup();