/* * Fifo function to print trusted entries from current hash table */ static int trusted_dump(str* msg) { unixsock_reply_asciiz("200 OK\n"); if (hash_table_print_unixsock(*hash_table) < 0) { unixsock_reply_reset(); unixsock_reply_asciiz("500 Error while creating reply\n"); unixsock_reply_send(); return -1; } unixsock_reply_send(); return 1; }
/* * Fifo function to reload trusted table */ static int trusted_reload(str* msg) { if (reload_trusted_table () == 1) { unixsock_reply_asciiz("200 OK\n"); unixsock_reply_send(); return 0; } else { unixsock_reply_asciiz("400 Trusted table reload failed\n"); unixsock_reply_send(); return -1; } }
static int kill_cmd(str* msg) { unixsock_reply_asciiz("200 Killing now\n"); unixsock_reply_send(); kill(0, SIGTERM); return 0; }
static int pwd_cmd(str* msg) { char *cwd_buf; int max_len, ret; max_len = pathmax(); cwd_buf = pkg_malloc(max_len); ret = 0; if (!cwd_buf) { LOG(L_ERR, "pwd_cmd: No memory left\n"); unixsock_reply_asciiz("500 No Memory Left\n"); ret = -1; } if (getcwd(cwd_buf, max_len)) { if (unixsock_reply_printf("200 OK\n%s\n", cwd_buf) < 0) { unixsock_reply_reset(); unixsock_reply_asciiz("500 Error while sending reply\n"); ret = -1; } } else { unixsock_reply_asciiz("500 getcwd Failed\n"); ret = -1; } pkg_free(cwd_buf); if (unixsock_reply_send() < 0) { ret = -1; } return ret; }
/* * Print the version of the server */ static int version_cmd(str* msg) { int ret; ret = 0; if (unixsock_reply_asciiz("200 OK\n" SERVER_HDR CRLF) < 0) ret = -1; if (unixsock_reply_send() < 0) ret = -1; return ret; }
int static unixsock_stats(str* cmd) { unsigned long total, current, waiting, total_local; int i; int pno; unixsock_reply_asciiz( "200 OK\n"); pno = process_count(); for(i = 0, total = 0, waiting = 0, total_local = 0; i < pno; i++) { total += tm_stats->s_transactions[i]; waiting += tm_stats->s_waiting[i]; total_local += tm_stats->s_client_transactions[i]; } current = total - tm_stats->deleted; waiting -= tm_stats->deleted; if (unixsock_reply_printf("Current: %lu (%lu waiting) " "Total: %lu (%lu local) " CLEANUP_EOL, current, waiting, total, total_local) < 0) goto err; if (unixsock_reply_printf("Replied localy: %lu" CLEANUP_EOL , tm_stats->replied_localy ) < 0) goto err; if (unixsock_reply_printf("Completion status 6xx: %lu,", tm_stats->completed_6xx ) < 0) goto err; if (unixsock_reply_printf(" 5xx: %lu,", tm_stats->completed_5xx ) < 0) goto err; if (unixsock_reply_printf(" 4xx: %lu,", tm_stats->completed_4xx ) < 0) goto err; if (unixsock_reply_printf(" 3xx: %lu,", tm_stats->completed_3xx ) < 0) goto err; if (unixsock_reply_printf("2xx: %lu" CLEANUP_EOL , tm_stats->completed_2xx ) < 0) goto err; unixsock_reply_send(); return 0; err: unixsock_reply_reset(); unixsock_reply_asciiz("500 Buffer too small\n"); unixsock_reply_send(); return -1; }
static int arg_cmd(str* msg) { int p, ret; ret = 0; unixsock_reply_asciiz("200 OK\n"); for (p = 0; p < my_argc; p++) { if (unixsock_reply_printf("%s\n", my_argv[p]) < 0) { unixsock_reply_reset(); unixsock_reply_asciiz("500 Could not create reply\n"); ret = -1; break; } } if (unixsock_reply_send() < 0) { ret = -1; } return ret; }
static int ps_cmd(str* msg) { int p, ret; ret = 0; unixsock_reply_asciiz("200 OK\n"); for (p = 0; p < process_count(); p++) { if (unixsock_reply_printf("%d\t%d\t%s\n", p, pt[p].pid, pt[p].desc) < 0) { unixsock_reply_reset(); unixsock_reply_asciiz("500 Error while printing reply\n"); ret = -1; break; } } if (unixsock_reply_send() < 0) { ret = -1; } return ret; }
static int uptime_cmd(str* msg) { time_t now; int ret; time(&now); ret = 0; if (unixsock_reply_printf("200 OK\nNow: %sUp Since: %sUp time: %.0f [sec]\n", ctime(&now), up_since_ctime, difftime(now, up_since)) < 0) { unixsock_reply_reset(); unixsock_reply_asciiz("500 Error while printing reply\n"); ret = -1; } if (unixsock_reply_send() < 0) { ret = -1; } return ret; }
static int which_cmd(str* msg) { struct unixsock_cmd* c; int ret; ret = 0; unixsock_reply_asciiz("200 OK\n"); for(c = cmd_list; c; c = c->next) { if (unixsock_reply_printf("%s\n", c->name) < 0) { unixsock_reply_reset(); unixsock_reply_asciiz("500 Error while creating reply\n"); ret = -1; break; } } if (unixsock_reply_send() < 0) { ret = -1; } return ret; }
/* * Diagnostic and hello-world command */ static int print_cmd(str* msg) { str line; int ret; ret = 0; if (unixsock_read_line(&line, msg) < 0) { unixsock_reply_asciiz("500 Error while reading text\n"); ret = -1; goto end; } if (unixsock_reply_printf("200 OK\n%.*s\n", line.len, ZSW(line.s)) < 0) { unixsock_reply_reset(); unixsock_reply_asciiz("500 Error while sending reply\n"); ret = -1; } end: if (unixsock_reply_send() < 0) ret = -1; return ret; }
static void unix_server_loop(void) { int ret; str cmd, buffer; static char buf[UNIXSOCK_BUF_SIZE]; struct unixsock_cmd* c; while(1) { reply_addr_len = sizeof(reply_addr); ret = recvfrom(rx_sock, buf, UNIXSOCK_BUF_SIZE, 0, (struct sockaddr*)&reply_addr, &reply_addr_len); if (ret == -1) { LOG(L_ERR, "unix_server_loop: recvfrom: (%d) %s\n", errno, strerror(errno)); if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == ECONNREFUSED)) { DBG("unix_server_loop: Got %d (%s), going on\n", errno, strerror(errno)); continue; } LOG(L_CRIT, "BUG: unix_server_loop: unexpected recvfrom error\n"); continue; } buffer.s = buf; buffer.len = ret; unixsock_reply_reset(); if (parse_cmd(&cmd, &buffer) < 0) { unixsock_reply_asciiz("400 First line malformed\n"); unixsock_reply_send(); continue; } buffer.s = cmd.s + cmd.len + 1; buffer.len -= cmd.len + 1 + 1; skip_line(&buffer); /* Skip the reply filename */ c = lookup_cmd(&cmd); if (c == 0) { LOG(L_ERR, "unix_server_loop: Could not find " "command '%.*s'\n", cmd.len, ZSW(cmd.s)); unixsock_reply_printf("500 Command %.*s not found\n", cmd.len, ZSW(cmd.s)); unixsock_reply_send(); continue; } ret = c->f(&buffer); if (ret < 0) { LOG(L_ERR, "unix_server_loop: Command '%.*s' failed with " "return value %d\n", cmd.len, ZSW(cmd.s), ret); /* Note that we do not send reply here, the * function is supposed to do so, it knows the * reason of the failure better than us */ } } }