Ejemplo n.º 1
0
int main(int argc, char **argv)
{
	int rc;
	int result = EXIT_FAILURE;
	struct device *upnp_renderer;

	if (!g_thread_supported()) {
		g_thread_init(NULL);
	}

	rc = process_cmdline(argc, argv);
	if (rc != 0) {
		goto out;
	}

	if (show_version) {
		do_show_version();
		exit(EXIT_SUCCESS);
	}
	if (show_connmgr_scpd) {
		upnp_renderer_dump_connmgr_scpd();
		exit(EXIT_SUCCESS);
	}
	if (show_control_scpd) {
		upnp_renderer_dump_control_scpd();
		exit(EXIT_SUCCESS);
	}
	if (show_transport_scpd) {
		upnp_renderer_dump_transport_scpd();
		exit(EXIT_SUCCESS);
	}

	upnp_renderer = upnp_renderer_new(friendly_name, uuid);
	if (upnp_renderer == NULL) {
		goto out;
	}

	if (show_devicedesc) {
		fputs(upnp_get_device_desc(upnp_renderer), stdout);
		exit(EXIT_SUCCESS);
	}

	rc = output_gstreamer_init();
	if (rc != 0) {
		goto out;
	}

	rc = upnp_device_init(upnp_renderer, ip_address);
	if (rc != 0) {
		goto out;
	}

	printf("Ready for rendering..\n");
	output_loop();
	result = EXIT_SUCCESS;

out:
	return result;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
	int rc;
	int result = EXIT_FAILURE;
	struct device *upnp_renderer;

	rc = process_cmdline(argc, argv);
	if (rc != 0) {
		goto out;
	}

	if (show_version) {
		do_show_version();
		exit(EXIT_SUCCESS);
	}
	if (show_connmgr_scpd) {
		upnp_renderer_dump_connmgr_scpd();
		exit(EXIT_SUCCESS);
	}
	if (show_control_scpd) {
		upnp_renderer_dump_control_scpd();
		exit(EXIT_SUCCESS);
	}
	if (show_transport_scpd) {
		upnp_renderer_dump_transport_scpd();
		exit(EXIT_SUCCESS);
	}

	if (show_outputs) {
		output_dump_modules();
		exit(EXIT_SUCCESS);
	}

	if (pid_file && pid_file[0] != '/') {
		// We need to canonicalize the filename because our
		// cwd will change after becoming a daemon.
		char *buf = (char*) malloc(PATH_MAX);  // will leak. Ok.
		char *result = getcwd(buf, PATH_MAX);
		result = strcat(result, "/");
		pid_file = strcat(result, pid_file);
	}
	if (daemon_mode) {
		daemon(0, 0);  // TODO: check for daemon() in configure.
	}
	if (pid_file) {
		FILE *p = fopen(pid_file, "w+");
		if (p) {
			fprintf(p, "%d\n", getpid());
			fclose(p);
		} else {
			perror("Failed to write pid file");
		}
	}
	if (!g_thread_get_initialized()) {
		g_thread_init(NULL);
	}

	upnp_renderer = upnp_renderer_new(friendly_name, uuid);
	if (upnp_renderer == NULL) {
		goto out;
	}

	if (show_devicedesc) {
		char *buf;
		buf = upnp_get_device_desc(upnp_renderer);
		assert(buf != NULL);
		fputs(buf, stdout);
		exit(EXIT_SUCCESS);
	}

	rc = output_init(output);
	if (rc != 0) {
		fprintf(stderr,"ERROR: Failed to initialize Output subsystem\n");
		goto out;
	}

	struct device_private *device;
	device = upnp_device_init(upnp_renderer, ip_address);
	if (device == NULL) {
		fprintf(stderr,"ERROR: Failed to initialize UPnP device\n");
		goto out;
	}

	upnp_transport_init(device);
	upnp_control_init();

	printf("Ready for rendering..\n");
	output_loop();
	result = EXIT_SUCCESS;

out:
	return result;
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: thess/UPnPMPD
int main(int argc, char **argv)
{
	int rc, cnt;
	struct device *upnp_renderer;
	char mac_addr[18];
	char eth_name[sizeof(ether_tmpl) + 10];
	char **eth;
	FILE *fh = NULL;

#if defined(__i386__) && defined(DEBUG)
	struct sigaction sigact;

	sigact.sa_sigaction = crit_err_hdlr;
	sigact.sa_flags = SA_RESTART | SA_SIGINFO;

	if (sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL) != 0)
	{
		fprintf(stderr, "error setting signal handler for %d (%s)\n",
			SIGSEGV, strsignal(SIGSEGV));

		exit(EXIT_FAILURE);
	}
#endif
	// Init glib threads
	if (!g_thread_supported())
	{
		g_thread_init(NULL);
	}

	// Parse args early (may set debug-level)
	rc = process_cmdline(argc, argv);
	if (rc != 0)
	{
		exit(EXIT_FAILURE);
	}

	// Some commands are valid only if interactive
	if (!run_as_daemon)
	{
		// Info commenads (non-daemon)
		if (show_version)
		{
			do_show_version();
			return EXIT_SUCCESS;
		}

		if (show_connmgr_scpd)
		{
			upnp_renderer_dump_connmgr_scpd();
			return EXIT_SUCCESS;
		}

		if (show_control_scpd)
		{
			upnp_renderer_dump_control_scpd();
			return EXIT_SUCCESS;
		}

		if (show_transport_scpd)
		{
			upnp_renderer_dump_transport_scpd();
			return EXIT_SUCCESS;
		}
	}
	else
	{
		if (show_version || show_control_scpd || show_connmgr_scpd ||
				show_transport_scpd || show_devicedesc)
		{
			// Info commands not allow in daemon mode
			fprintf(stderr, "Info options incompatible with daemon\n");
			exit(EXIT_FAILURE);
		}

		rc = daemon(0, 0);
		if (rc != 0)
		{
			fprintf(stderr, "Failed to detach process\n");
			exit(EXIT_FAILURE);
		}
	}

	// Read and parse config file, OK if none
	if (config_file == NULL)
		config_file = CFG_FILE_NAME;


	config_init(&upnpmpd_cfg);
	rc = config_read_file(&upnpmpd_cfg, (config_file) ? config_file : CFG_FILE_NAME);
	if (rc != CONFIG_TRUE)
	{
		if (config_error_line(&upnpmpd_cfg) == 0)
		{
			printf("Config not found (%s), continuing...\n", config_file);
		}
		else
		{
			printf ("Error parsing config file: %s\nLine %d: %s", config_file,
				config_error_line(&upnpmpd_cfg), config_error_text(&upnpmpd_cfg));
			exit(EXIT_FAILURE);
		}
	}

	// Use bound interface if given
	if (if_name[0])
		eth = &if_name[0];
	else
		eth = &ethers[0];

	while (*eth)
	{
		sprintf(eth_name, ether_tmpl, *eth);
		// Obtain MAC address from interface
		fh = fopen(eth_name, "r");
		if (fh)
			break;
		// Try next
		eth++;
	}

	// anything?
	if (fh == NULL)
	{
		fprintf(stderr, "No usable network interface found\n");
		exit(EXIT_FAILURE);
	}
	// Read 6 groups of 3 chars
	cnt = fread(mac_addr, 3, 6, fh);
	fclose(fh);

	// Valid looking MAC?
	if (cnt != 6)
	{
		fprintf(stderr, "Bad MAC address %d\n", cnt);
		exit(EXIT_FAILURE);
	}

	mac_addr[17] = '\0';
	DBG_PRINT(DBG_LVL2, "Found MAC at %s: %s\n", eth_name, mac_addr);

	upnp_renderer = upnp_renderer_new(friendly_name, mac_addr, serial, &upnpmpd_cfg);
	if (upnp_renderer == NULL)
		exit(EXIT_FAILURE);

	if (show_devicedesc)
	{
		fputs(upnp_get_device_desc(upnp_renderer), stdout);
		return EXIT_SUCCESS;
	}

	// Connect to MPD
	rc = output_mpd_init(&upnpmpd_cfg);
	if (rc != 0)
		exit(EXIT_FAILURE);

	// Create UPnP device and broadcast
	rc = getIfInfo(if_name[0]);
	if (rc != 0)
		exit(EXIT_FAILURE);

	rc = upnp_device_init(upnp_renderer, gIF_IPV4);
	if (rc != 0)
		exit(EXIT_FAILURE);

	printf("UPnPMPD ready at IP: %s\n", gIF_IPV4);

	// Run main event loop
	// Process glib evnets
	output_loop();

	return EXIT_SUCCESS;
}