Merge pull request #18 from bket/daemon - fiche - A pastebin adjusted for gophe… | |
git clone git://vernunftzentrum.de/fiche.git | |
Log | |
Files | |
Refs | |
LICENSE | |
--- | |
commit 888b578907be5e3aca8fa0c286b10eea813c52f9 | |
parent a0bbd2fa28625f03f41778f815572e4aa05dce02 | |
Author: solusipse <[email protected]> | |
Date: Sat, 19 Sep 2015 16:29:54 +0200 | |
Merge pull request #18 from bket/daemon | |
daemonizing fiche | |
Diffstat: | |
README.md | 18 +++++++++++++++++- | |
fiche.c | 64 ++++++++++++++++++++++--------- | |
fiche.h | 10 ++++++---- | |
3 files changed, 68 insertions(+), 24 deletions(-) | |
--- | |
diff --git a/README.md b/README.md | |
@@ -47,7 +47,7 @@ providing fiche-based service all the time on this address `s… | |
## Server-side usage ## | |
``` | |
-usage: fiche [-pbsdolBuw]. | |
+usage: fiche [-DepbsdolBuw]. | |
[-d domain] [-p port] [-s slug size] | |
[-o output directory] [-B buffer size] [-u user name] | |
[-l log file] [-b banlist] [-w whitelist] | |
@@ -171,6 +171,22 @@ There is no specific syntax, there files may contain not o… | |
----------------- | |
+#### Daemonize #### | |
+ | |
+Fork fiche to the background: | |
+ | |
+fiche -D | |
+ | |
+----------------- | |
+ | |
+#### Extended character set for the URL #### | |
+ | |
+Fork can extend the charcter set for the URL: | |
+ | |
+fiche -e | |
+ | |
+----------------- | |
+ | |
#### Examples #### | |
Logging connections with banlist: | |
diff --git a/fiche.c b/fiche.c | |
@@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/ | |
------------------------------------------------------------------------------- | |
-usage: fiche [-epbsdolBuw]. | |
- [-e] [-d domain] [-p port] [-s slug size] | |
+usage: fiche [-DepbsdolBuw]. | |
+ [-D] [-e] [-d domain] [-p port] [-s slug size] | |
[-o output directory] [-B buffer size] [-u user name] | |
[-l log file] [-b banlist] [-w whitelist] | |
+-D option is for daemonizing fiche | |
+ | |
-e option is for using an extended character set for the URL | |
Compile with Makefile or manually with -O2 and -pthread flags. | |
@@ -48,7 +50,18 @@ int main(int argc, char **argv) | |
server_address = set_address(server_address); | |
bind_to_port(listen_socket, server_address); | |
- while (1) perform_connection(listen_socket); | |
+ if (DAEMON) | |
+ { | |
+ pid_t pid; | |
+ | |
+ pid = fork(); | |
+ if (pid == -1) | |
+ error("ERROR: Failed to fork"); | |
+ if (pid == 0) | |
+ while (1) perform_connection(listen_socket); | |
+ } | |
+ else | |
+ while (1) perform_connection(listen_socket); | |
} | |
void *thread_connection(void *args) | |
@@ -134,7 +147,7 @@ void perform_connection(int listen_socket) | |
void display_date() | |
{ | |
- printf("%s\n", get_date()); | |
+ info("%s\n", get_date()); | |
} | |
char *get_date() | |
@@ -160,7 +173,7 @@ struct client_data get_client_address(struct sockaddr_in cl… | |
hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeo… | |
if (hostp == NULL) | |
{ | |
- printf("ERROR: Couldn't obtain client's hostname\n"); | |
+ info("ERROR: Couldn't obtain client's hostname\n"); | |
data.hostname = "n/a"; | |
} | |
else | |
@@ -169,7 +182,7 @@ struct client_data get_client_address(struct sockaddr_in cl… | |
hostaddrp = inet_ntoa(client_address.sin_addr); | |
if (hostaddrp == NULL) | |
{ | |
- printf("ERROR: Couldn't obtain client's address\n"); | |
+ info("ERROR: Couldn't obtain client's address\n"); | |
data.ip_address = "n/a"; | |
} | |
else | |
@@ -199,11 +212,11 @@ void save_log(char *slug, char *hostaddrp, char *h_name) | |
void display_info(struct client_data data, char *slug, char *message) | |
{ | |
if (slug == NULL) | |
- printf("%s\n", message); | |
- else printf("Saved to: %s\n", slug); | |
+ info("%s\n", message); | |
+ else info("Saved to: %s\n", slug); | |
display_date(); | |
- printf("Client: %s (%s)\n", data.ip_address, data.hostname); | |
- display_line(); | |
+ info("Client: %s (%s)\n", data.ip_address, data.hostname); | |
+ info("====================================\n"); | |
} | |
char *check_banlist(char *ip_address) | |
@@ -362,20 +375,33 @@ void set_basedir() | |
void startup_message() | |
{ | |
- display_line(); | |
- printf("Domain name: %s\n", DOMAIN); | |
- printf("Saving files to: %s\n", BASEDIR); | |
- printf("Fiche started listening on port %d.\n", PORT); | |
- display_line(); | |
+ info("====================================\n"); | |
+ info("Domain name: %s\n", DOMAIN); | |
+ info("Saving files to: %s\n", BASEDIR); | |
+ info("Fiche started listening on port %d.\n", PORT); | |
+ info("====================================\n"); | |
+} | |
+ | |
+void info(char *buffer, ...) | |
+{ | |
+ if (DAEMON) | |
+ return; | |
+ | |
+ printf(buffer); | |
} | |
void parse_parameters(int argc, char **argv) | |
{ | |
int c; | |
- while ((c = getopt (argc, argv, "ep:b:s:d:o:l:B:u:w:")) != -1) | |
+ if (strcmp(*argv, "-D")) | |
+ DAEMON = 1; | |
+ | |
+ while ((c = getopt (argc, argv, "Dep:b:s:d:o:l:B:u:w:")) != -1) | |
switch (c) | |
{ | |
+ case 'D': | |
+ break; | |
case 'e': | |
snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstu… | |
break; | |
@@ -387,7 +413,7 @@ void parse_parameters(int argc, char **argv) | |
break; | |
case 'B': | |
BUFSIZE = atoi(optarg); | |
- printf("Buffer size set to: %d.\n", BUFSIZE); | |
+ info("Buffer size set to: %d.\n", BUFSIZE); | |
break; | |
case 'b': | |
BANFILE = optarg; | |
@@ -395,14 +421,14 @@ void parse_parameters(int argc, char **argv) | |
break; | |
case 's': | |
SLUG_SIZE = atoi(optarg); | |
- printf("Slug size set to: %d.\n", SLUG_SIZE); | |
+ info("Slug size set to: %d.\n", SLUG_SIZE); | |
break; | |
case 'o': | |
BASEDIR = optarg; | |
break; | |
case 'l': | |
LOG = optarg; | |
- printf("Log file: %s\n", LOG); | |
+ info("Log file: %s\n", LOG); | |
break; | |
case 'u': | |
set_uid_gid(optarg); | |
diff --git a/fiche.h b/fiche.h | |
@@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/ | |
------------------------------------------------------------------------------- | |
-usage: fiche [-epbsdolBuw]. | |
- [-e] [-d domain] [-p port] [-s slug size] | |
+usage: fiche [-DepbsdolBuw]. | |
+ [-D] [-e] [-d domain] [-p port] [-s slug size] | |
[-o output directory] [-B buffer size] [-u user name] | |
[-l log file] [-b banlist] [-w whitelist] | |
+-D option is for daemonizing fiche | |
+ | |
-e option is for using an extended character set for the URL | |
Compile with Makefile or manually with -O2 and -pthread flags. | |
@@ -51,6 +53,7 @@ char *BANLIST; | |
char *BANFILE; | |
char *WHITEFILE; | |
char *WHITELIST; | |
+int DAEMON = 0; | |
int PORT = 9999; | |
int SLUG_SIZE = 4; | |
int BUFSIZE = 32768; | |
@@ -77,7 +80,6 @@ int create_directory(char *slug); | |
int check_protocol(char *buffer); | |
void bind_to_port(int listen_socket, struct sockaddr_in serveraddr); | |
-void display_line(){printf("====================================\n");} | |
void error(char *error_code){perror(error_code); exit(1);} | |
void display_date(); | |
void perform_connection(int listen_socket); | |
@@ -91,8 +93,8 @@ void parse_parameters(int argc, char **argv); | |
void save_log(char *slug, char *hostaddrp, char *h_name); | |
void change_owner(char *directory); | |
void set_uid_gid(); | |
+void info(char *buffer, ...); | |
-char *return_line(){return("\n====================================");} | |
char *check_banlist(char *ip_address); | |
char *check_whitelist(char *ip_address); | |
char *get_date(); |