Merge pull request #18 from bket/daemon

daemonizing fiche
This commit is contained in:
solusipse
2015-09-19 16:29:54 +02:00
3 changed files with 68 additions and 24 deletions

View File

@@ -47,7 +47,7 @@ providing fiche-based service all the time on this address `solusipse.net` and t
## Server-side usage ## ## Server-side usage ##
``` ```
usage: fiche [-pbsdolBuw]. usage: fiche [-DepbsdolBuw].
[-d domain] [-p port] [-s slug size] [-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name] [-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist] [-l log file] [-b banlist] [-w whitelist]
@@ -171,6 +171,22 @@ There is no specific syntax, there files may contain not only addresses.
----------------- -----------------
#### 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 #### #### Examples ####
Logging connections with banlist: Logging connections with banlist:

64
fiche.c
View File

@@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
usage: fiche [-epbsdolBuw]. usage: fiche [-DepbsdolBuw].
[-e] [-d domain] [-p port] [-s slug size] [-D] [-e] [-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name] [-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist] [-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 -e option is for using an extended character set for the URL
Compile with Makefile or manually with -O2 and -pthread flags. 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); server_address = set_address(server_address);
bind_to_port(listen_socket, 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) void *thread_connection(void *args)
@@ -134,7 +147,7 @@ void perform_connection(int listen_socket)
void display_date() void display_date()
{ {
printf("%s\n", get_date()); info("%s\n", get_date());
} }
char *get_date() char *get_date()
@@ -160,7 +173,7 @@ struct client_data get_client_address(struct sockaddr_in client_address)
hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET); hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET);
if (hostp == NULL) 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"; data.hostname = "n/a";
} }
else else
@@ -169,7 +182,7 @@ struct client_data get_client_address(struct sockaddr_in client_address)
hostaddrp = inet_ntoa(client_address.sin_addr); hostaddrp = inet_ntoa(client_address.sin_addr);
if (hostaddrp == NULL) 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"; data.ip_address = "n/a";
} }
else 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) void display_info(struct client_data data, char *slug, char *message)
{ {
if (slug == NULL) if (slug == NULL)
printf("%s\n", message); info("%s\n", message);
else printf("Saved to: %s\n", slug); else info("Saved to: %s\n", slug);
display_date(); display_date();
printf("Client: %s (%s)\n", data.ip_address, data.hostname); info("Client: %s (%s)\n", data.ip_address, data.hostname);
display_line(); info("====================================\n");
} }
char *check_banlist(char *ip_address) char *check_banlist(char *ip_address)
@@ -362,20 +375,33 @@ void set_basedir()
void startup_message() void startup_message()
{ {
display_line(); info("====================================\n");
printf("Domain name: %s\n", DOMAIN); info("Domain name: %s\n", DOMAIN);
printf("Saving files to: %s\n", BASEDIR); info("Saving files to: %s\n", BASEDIR);
printf("Fiche started listening on port %d.\n", PORT); info("Fiche started listening on port %d.\n", PORT);
display_line(); info("====================================\n");
}
void info(char *buffer, ...)
{
if (DAEMON)
return;
printf(buffer);
} }
void parse_parameters(int argc, char **argv) void parse_parameters(int argc, char **argv)
{ {
int c; 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) switch (c)
{ {
case 'D':
break;
case 'e': case 'e':
snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ"); snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ");
break; break;
@@ -387,7 +413,7 @@ void parse_parameters(int argc, char **argv)
break; break;
case 'B': case 'B':
BUFSIZE = atoi(optarg); BUFSIZE = atoi(optarg);
printf("Buffer size set to: %d.\n", BUFSIZE); info("Buffer size set to: %d.\n", BUFSIZE);
break; break;
case 'b': case 'b':
BANFILE = optarg; BANFILE = optarg;
@@ -395,14 +421,14 @@ void parse_parameters(int argc, char **argv)
break; break;
case 's': case 's':
SLUG_SIZE = atoi(optarg); SLUG_SIZE = atoi(optarg);
printf("Slug size set to: %d.\n", SLUG_SIZE); info("Slug size set to: %d.\n", SLUG_SIZE);
break; break;
case 'o': case 'o':
BASEDIR = optarg; BASEDIR = optarg;
break; break;
case 'l': case 'l':
LOG = optarg; LOG = optarg;
printf("Log file: %s\n", LOG); info("Log file: %s\n", LOG);
break; break;
case 'u': case 'u':
set_uid_gid(optarg); set_uid_gid(optarg);

10
fiche.h
View File

@@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
usage: fiche [-epbsdolBuw]. usage: fiche [-DepbsdolBuw].
[-e] [-d domain] [-p port] [-s slug size] [-D] [-e] [-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name] [-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist] [-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 -e option is for using an extended character set for the URL
Compile with Makefile or manually with -O2 and -pthread flags. Compile with Makefile or manually with -O2 and -pthread flags.
@@ -51,6 +53,7 @@ char *BANLIST;
char *BANFILE; char *BANFILE;
char *WHITEFILE; char *WHITEFILE;
char *WHITELIST; char *WHITELIST;
int DAEMON = 0;
int PORT = 9999; int PORT = 9999;
int SLUG_SIZE = 4; int SLUG_SIZE = 4;
int BUFSIZE = 32768; int BUFSIZE = 32768;
@@ -77,7 +80,6 @@ int create_directory(char *slug);
int check_protocol(char *buffer); int check_protocol(char *buffer);
void bind_to_port(int listen_socket, struct sockaddr_in serveraddr); 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 error(char *error_code){perror(error_code); exit(1);}
void display_date(); void display_date();
void perform_connection(int listen_socket); 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 save_log(char *slug, char *hostaddrp, char *h_name);
void change_owner(char *directory); void change_owner(char *directory);
void set_uid_gid(); void set_uid_gid();
void info(char *buffer, ...);
char *return_line(){return("\n====================================");}
char *check_banlist(char *ip_address); char *check_banlist(char *ip_address);
char *check_whitelist(char *ip_address); char *check_whitelist(char *ip_address);
char *get_date(); char *get_date();