summaryrefslogtreecommitdiff
path: root/src/anylike.c
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anylike.org>2009-12-25 10:54:18 +0000
committerChristian Pointner <equinox@anylike.org>2009-12-25 10:54:18 +0000
commit4c1843a0723e2a973d81d6b1895dafcd30fead62 (patch)
treea272d4058f334bc132d685402afa03579ccd57fd /src/anylike.c
parentcleanup (diff)
added daemonizing
init of libgcrypt
Diffstat (limited to 'src/anylike.c')
-rw-r--r--src/anylike.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/anylike.c b/src/anylike.c
index 0cd0138..1735631 100644
--- a/src/anylike.c
+++ b/src/anylike.c
@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <stdio.h>
+#include <errno.h>
#include <lua50/lua.h>
@@ -33,6 +34,40 @@
#include "log.h"
#include "l_log.h"
+#ifndef USE_SSL_CRYPTO
+#include <gcrypt.h>
+#endif
+
+#include "daemon.h"
+
+#ifndef USE_SSL_CRYPTO
+
+#define MIN_GCRYPT_VERSION "1.2.0"
+
+int init_libgcrypt()
+{
+ if(!gcry_check_version(MIN_GCRYPT_VERSION)) {
+ log_printf(NOTICE, "invalid Version of libgcrypt, should be >= %s", MIN_GCRYPT_VERSION);
+ return -1;
+ }
+
+ gcry_error_t err = gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
+ if(err) {
+ log_printf(ERROR, "failed to disable secure memory: %s", gcry_strerror(err));
+ return -1;
+ }
+
+ err = gcry_control(GCRYCTL_INITIALIZATION_FINISHED);
+ if(err) {
+ log_printf(ERROR, "failed to finish libgcrypt initialization: %s", gcry_strerror(err));
+ return -1;
+ }
+
+ log_printf(NOTICE, "libgcrypt init finished");
+ return 0;
+}
+#endif
+
int main_loop(options_t* opt)
{
lua_State *L;
@@ -101,6 +136,60 @@ int main(int argc, char* argv[])
log_printf(NOTICE, "just started...");
options_parse_post(&opt);
+
+
+
+ priv_info_t priv;
+ if(opt.username_)
+ if(priv_init(&priv, opt.username_, opt.groupname_)) {
+ options_clear(&opt);
+ log_close();
+ exit(-1);
+ }
+
+#ifndef USE_SSL_CRYPTO
+ ret = init_libgcrypt();
+ if(ret) {
+ log_printf(ERROR, "error on libgcrpyt initialization, exitting");
+ options_clear(&opt);
+ log_close();
+ exit(ret);
+ }
+#endif
+
+ FILE* pid_file = NULL;
+ if(opt.pid_file_) {
+ pid_file = fopen(opt.pid_file_, "w");
+ if(!pid_file) {
+ log_printf(WARNING, "unable to open pid file: %s", strerror(errno));
+ }
+ }
+
+ if(opt.chroot_dir_)
+ if(do_chroot(opt.chroot_dir_)) {
+ options_clear(&opt);
+ log_close();
+ exit(-1);
+ }
+ if(opt.username_)
+ if(priv_drop(&priv)) {
+ options_clear(&opt);
+ log_close();
+ exit(-1);
+ }
+
+ if(opt.daemonize_) {
+ pid_t oldpid = getpid();
+ daemonize();
+ log_printf(INFO, "running in background now (old pid: %d)", oldpid);
+ }
+
+ if(pid_file) {
+ pid_t pid = getpid();
+ fprintf(pid_file, "%d", pid);
+ fclose(pid_file);
+ }
+
ret = main_loop(&opt);
options_clear(&opt);