Revision f82a8605 janus.c

View differences:

janus.c
113 113
	json_object_set_new(info, "version", json_integer(JANUS_VERSION));
114 114
	json_object_set_new(info, "version_string", json_string(JANUS_VERSION_STRING));
115 115
	json_object_set_new(info, "author", json_string(JANUS_AUTHOR));
116
	json_object_set_new(info, "log-to-stdout", json_string(janus_log_is_stdout_enabled() ? "true" : "false"));
117
	json_object_set_new(info, "log-to-file", json_string(janus_log_is_logfile_enabled() ? "true" : "false"));
118
	if(janus_log_is_logfile_enabled())
119
		json_object_set_new(info, "log-path", json_string(janus_log_get_logfile_path()));
116 120
#ifdef HAVE_SCTP
117 121
	json_object_set_new(info, "data_channels", json_string("true"));
118 122
#else
......
2117 2121
	if(!request)
2118 2122
		return -1;
2119 2123
	gchar *error_string = NULL;
2120
	gchar  error_buf[512];
2124
	gchar error_buf[512];
2121 2125
	if(format == NULL) {
2122 2126
		/* No error string provided, use the default one */
2123 2127
		error_string = (gchar *)janus_get_api_error(error);
......
2897 2901
	core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
2898 2902
	setrlimit(RLIMIT_CORE, &core_limits);
2899 2903

  
2900
	/* TODO Check if we need to log to console and/or file, assuming console only for now */
2901
	if(janus_log_init(TRUE, NULL) < 0)
2902
		exit(1);
2903

  
2904 2904
	struct gengetopt_args_info args_info;
2905 2905
	/* Let's call our cmdline parser */
2906 2906
	if(cmdline_parser(argc, argv, &args_info) != 0)
2907 2907
		exit(1);
2908 2908
	
2909
	JANUS_PRINT("---------------------------------------------------\n");
2910
	JANUS_PRINT("  Starting Meetecho Janus (WebRTC Gateway) v%s\n", JANUS_VERSION_STRING);
2911
	JANUS_PRINT("---------------------------------------------------\n\n");
2912
	
2913
	/* Handle SIGINT (CTRL-C), SIGTERM (from service managers) */
2914
	signal(SIGINT, janus_handle_signal);
2915
	signal(SIGTERM, janus_handle_signal);
2909
	/* Any configuration to open? */
2910
	if(args_info.config_given) {
2911
		config_file = g_strdup(args_info.config_arg);
2912
	}
2913
	if(args_info.configs_folder_given) {
2914
		configs_folder = g_strdup(args_info.configs_folder_arg);
2915
	} else {
2916
		configs_folder = g_strdup (CONFDIR);
2917
	}
2918
	if(config_file == NULL) {
2919
		char file[255];
2920
		g_snprintf(file, 255, "%s/janus.cfg", configs_folder);
2921
		config_file = g_strdup(file);
2922
	}
2923
	if((config = janus_config_parse(config_file)) == NULL) {
2924
		if(args_info.config_given) {
2925
			/* We only give up if the configuration file was explicitly provided */
2926
			g_print("Error reading configuration from %s\n", config_file);
2927
			exit(1);
2928
		}
2929
		g_print("Error reading/parsing the configuration file, going on with the defaults and the command line arguments\n");
2930
		config = janus_config_create("janus.cfg");
2931
		if(config == NULL) {
2932
			/* If we can't even create an empty configuration, something's definitely wrong */
2933
			exit(1);
2934
		}
2935
	}
2916 2936

  
2917
	/* Setup Glib */
2918
#if !GLIB_CHECK_VERSION(2, 36, 0)
2919
	g_type_init();
2920
#endif
2937
	/* Check if we need to log to console and/or file */
2938
	gboolean use_stdout = TRUE;
2939
	if(args_info.disable_stdout_given) {
2940
		use_stdout = FALSE;
2941
	} else {
2942
		/* Check if the configuration file is saying anything about this */
2943
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "log_to_stdout");
2944
		if(item && item->value && !janus_is_true(item->value))
2945
			use_stdout = FALSE;
2946
	}
2947
	const char *logfile = NULL;
2948
	if(args_info.log_file_given) {
2949
		logfile = args_info.log_file_arg;
2950
	} else {
2951
		/* Check if the configuration file is saying anything about this */
2952
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "log_to_file");
2953
		if(item && item->value)
2954
			logfile = item->value;
2955
	}
2921 2956

  
2957
	/* Check if we're going to daemonize Janus */
2958
	gboolean daemonize = FALSE;
2922 2959
	if(args_info.daemon_given) {
2923
		JANUS_PRINT("Running Janus as a daemon\n");
2924
		/* FIXME Logging is still stdout/stderr based, and we close those */
2925
		JANUS_PRINT("\nNOTE: This is still WIP, and no logging/output is available when running as\n");
2926
		JANUS_PRINT("      a daemon at the moment. Check the documentation for alternatives on\n");
2927
		JANUS_PRINT("      running Janus in background.\n");
2960
		daemonize = TRUE;
2961
	} else {
2962
		/* Check if the configuration file is saying anything about this */
2963
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "daemonize");
2964
		if(item && item->value && janus_is_true(item->value))
2965
			daemonize = TRUE;
2966
	}
2967
	/* If we're going to daemonize, make sure logging to stdout is disabled and a log file has been specified */
2968
	if(daemonize && use_stdout) {
2969
		use_stdout = FALSE;
2970
	}
2971
	if(daemonize && logfile == NULL) {
2972
		g_print("Running Janus as a daemon but no log file provided, giving up...\n");
2973
		exit(1);
2974
	}
2975
	/* Daemonize now, if we need to */
2976
	if(daemonize) {
2977
		g_print("Running Janus as a daemon\n");
2928 2978

  
2929 2979
		/* Fork off the parent process */
2930 2980
		pid_t pid = fork();
2931 2981
		if(pid < 0) {
2932
			JANUS_PRINT("Fork error!\n");
2982
			g_print("Fork error!\n");
2933 2983
			exit(1);
2934 2984
		}
2935 2985
		if(pid > 0) {
......
2941 2991
		/* Create a new SID for the child process */
2942 2992
		pid_t sid = setsid();
2943 2993
		if(sid < 0) {
2944
			JANUS_PRINT("Error setting SID!\n");
2994
			g_print("Error setting SID!\n");
2945 2995
			exit(1);
2946 2996
		}
2947 2997
		/* Change the current working directory */
2948 2998
		if((chdir("/")) < 0) {
2949
			JANUS_PRINT("Error changing the current working directory!\n");
2999
			g_print("Error changing the current working directory!\n");
2950 3000
			exit(1);
2951 3001
		}
2952 3002

  
......
2956 3006
		close(STDERR_FILENO);
2957 3007
	}
2958 3008

  
3009
	/* Initialize logger */
3010
	if(janus_log_init(use_stdout, logfile) < 0)
3011
		exit(1);
3012

  
3013
	JANUS_PRINT("---------------------------------------------------\n");
3014
	JANUS_PRINT("  Starting Meetecho Janus (WebRTC Gateway) v%s\n", JANUS_VERSION_STRING);
3015
	JANUS_PRINT("---------------------------------------------------\n\n");
3016

  
3017
	/* Handle SIGINT (CTRL-C), SIGTERM (from service managers) */
3018
	signal(SIGINT, janus_handle_signal);
3019
	signal(SIGTERM, janus_handle_signal);
3020

  
3021
	/* Setup Glib */
3022
#if !GLIB_CHECK_VERSION(2, 36, 0)
3023
	g_type_init();
3024
#endif
3025

  
2959 3026
	/* Logging level: default is info and no timestamps */
2960 3027
	janus_log_level = LOG_INFO;
2961 3028
	janus_log_timestamps = FALSE;
......
2968 3035
		janus_log_level = args_info.debug_level_arg;
2969 3036
	}
2970 3037

  
2971
	/* Any configuration to open? */
2972
	if(args_info.config_given) {
2973
		config_file = g_strdup(args_info.config_arg);
2974
		if(config_file == NULL) {
2975
			JANUS_PRINT("Memory error!\n");
2976
			exit(1);
2977
		}
2978
	}
2979
	if(args_info.configs_folder_given) {
2980
		configs_folder = g_strdup(args_info.configs_folder_arg);
2981
		if(configs_folder == NULL) {
2982
			JANUS_PRINT("Memory error!\n");
2983
			exit(1);
2984
		}
2985
	} else {
2986
		configs_folder = g_strdup (CONFDIR);
2987
	}
2988
	if(config_file == NULL) {
2989
		char file[255];
2990
		g_snprintf(file, 255, "%s/janus.cfg", configs_folder);
2991
		config_file = g_strdup(file);
2992
		if(config_file == NULL) {
2993
			JANUS_PRINT("Memory error!\n");
2994
			exit(1);
2995
		}
2996
	}
2997
	JANUS_PRINT("Reading configuration from %s\n", config_file);
2998
	if((config = janus_config_parse(config_file)) == NULL) {
2999
		if(args_info.config_given) {
3000
			/* We only give up if the configuration file was explicitly provided */
3001
			exit(1);
3002
		}
3003
		JANUS_PRINT("Error reading/parsing the configuration file, going on with the defaults and the command line arguments\n");
3004
		config = janus_config_create("janus.cfg");
3005
		if(config == NULL) {
3006
			/* If we can't even create an empty configuration, something's definitely wrong */
3007
			exit(1);
3008
		}
3009
	}
3038
	/* Proceed with the rest of the configuration */
3010 3039
	janus_config_print(config);
3011 3040
	if(args_info.debug_level_given) {
3012 3041
		char debug[5];
......
3710 3739
	}
3711 3740

  
3712 3741
	JANUS_PRINT("Bye!\n");
3742

  
3713 3743
	janus_log_destroy();
3744

  
3714 3745
	exit(0);
3715 3746
}

Also available in: Unified diff