Ejemplo n.º 1
0
static int initialize() {
    int retval;

    if (!cc_config.allow_multiple_clients) {
        retval = wait_client_mutex(".", 10);
        if (retval) {
            log_message_error("Another instance of BOINC is running.");
            return ERR_EXEC;
        }
    }


    // Initialize WinSock
#if defined(_WIN32) && defined(USE_WINSOCK)
    if (WinsockInitialize() != 0) {
        log_message_error("Failed to initialize the Windows Sockets interface.");
        return ERR_IO;
    }
#endif

    curl_init();

#ifdef _WIN32
    if(!startup_idle_monitor()) {
        log_message_error(
            "Failed to initialize the BOINC idle monitor interface."
            "BOINC will not be able to determine if the user is idle or not...\n"
        );
    }
#endif

    return 0;
}
Ejemplo n.º 2
0
int is_valid_request(char* request) {
	int ret;
	char* token;
	char* mid_token;
	char* end_token;

	if(request==NULL) {
		log_message_error("MidNet: Verifying request",PROGRAM,"NULL request");
		return 0;
	}

	end_token = strstr(request, "\n\n");
	if(end_token==NULL) {
		log_message_error("MidNet: Verifying request",PROGRAM,"Invalid request, no end detected");
		return 0;
	}

	ret = strcmp(request, "list\n\n");
	if(ret==0) {
		return 1;
	}

	token = strstr(request, "\n");
	if(token==end_token) {
		log_message_error("MidNet: Verifying request",PROGRAM,"Wrong command! 2 Aborted.");
		return 0;
	}

	mid_token = strstr(token+1, "\n");
	if(mid_token!=end_token) {
		log_message_error("MidNet: Verifying request",PROGRAM,"Wrong command! 3 Aborted.");
		return 0;
	}

	*token = '\0';

	ret = strcmp(request, "get");
	if(ret==0) {
		*token = '\n';
		return 1;
	} else {
		ret = strcmp(request, "put");
		if(ret==0) {
			*token = '\n';
			return 1;
		} else {
			log_message_error("MidNet: Verifying request",PROGRAM,"Wrong command! 4 Aborted.");
			return 0;
		}
	}
}
Ejemplo n.º 3
0
// Create a thread to monitor system events
static DWORD WINAPI WindowsMonitorSystemPowerThread( LPVOID  ) {
    WNDCLASS wc;
    MSG msg;

    // Initialize diagnostics framework for this thread
    //
    diagnostics_thread_init();

    wc.style         = CS_GLOBALCLASS;
    wc.lpfnWndProc   = (WNDPROC)WindowsMonitorSystemPowerWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = NULL;
    wc.hIcon         = NULL;
    wc.hCursor       = NULL;
    wc.hbrBackground = NULL;
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "BOINCWindowsMonitorSystemPower";

    if (!RegisterClass(&wc)) {
        log_message_error("Failed to register the WindowsMonitorSystem window class.");
        return 1;
    }

    g_hWndWindowsMonitorSystemPower = CreateWindow(
        wc.lpszClassName,
        "BOINC Monitor System (Power)",
        WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        NULL,
        NULL);

    if (!g_hWndWindowsMonitorSystemPower) {
        log_message_error("Failed to create the WindowsMonitorSystem window.");
        return 0;
    }

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}
Ejemplo n.º 4
0
static int finalize() {
    static bool finalized = false;
    if (finalized) return 0;
    finalized = true;
    gstate.quit_activities();
    daily_xfer_history.write_state();

#ifdef _WIN32
    shutdown_idle_monitor();

#ifdef USE_WINSOCK
    if (WinsockCleanup()) {
        log_message_error("Failed to cleanup the Windows Sockets interface");
        return ERR_IO;
    }
#endif

    cleanup_system_monitor();

#endif

	curl_cleanup();

    gstate.free_mem();

    gstate.cleanup_completed = true;
    return 0;
}
Ejemplo n.º 5
0
static int finalize() {
    static bool finalized = false;
    if (finalized) return 0;
    finalized = true;
    gstate.quit_activities();

#ifdef _WIN32
    shutdown_idle_monitor();

#ifdef USE_WINSOCK
    if (WinsockCleanup()) {
        log_message_error("WinSockCleanup() failed");
        return ERR_IO;
    }
#endif

    cleanup_system_monitor();

#endif

    curl_cleanup();

#ifdef _DEBUG
    gstate.free_mem();
#endif

    diagnostics_finish();
    gstate.cleanup_completed = true;
    return 0;
}
Ejemplo n.º 6
0
int midnet_create_structure(struct _tcp_session* client_request) {
	if(client_request==NULL) {
		log_message_error("MidNet: Creating protocol structure",PROGRAM,"NULL TCP structure");
		return 0;
	}
	return 1;
}
Ejemplo n.º 7
0
int boinc_main_loop() {
    int retval;

    retval = initialize();
    if (retval) return retval;

#ifdef __APPLE__
    // If we run too soon during system boot we can cause a kernel panic
    if (gstate.executing_as_daemon) {
        if (TickCount() < (120*60)) {   // If system has been up for less than 2 minutes
            boinc_sleep(30.);
        }
    }
#endif

    retval = gstate.init();
    if (retval) {
        log_message_error("gstate.init() failed", retval);
        return retval;
    }

    log_message_startup("Initialization completed");

    while (1) {
        if (!gstate.poll_slow_events()) {
            gstate.do_io_or_sleep(POLL_INTERVAL);
        }
        fflush(stderr);
        fflush(stdout);

        if (gstate.time_to_exit()) {
            msg_printf(NULL, MSG_INFO, "Time to exit");
            break;
        }
        if (gstate.requested_exit) {
            if (cc_config.abort_jobs_on_exit) {
                if (!gstate.in_abort_sequence) {
                    msg_printf(NULL, MSG_INFO,
                               "Exit requested; starting abort sequence"
                              );
                    gstate.start_abort_sequence();
                }
            } else {
                msg_printf(NULL, MSG_INFO, "Exiting");
                break;
            }
        }
        if (gstate.in_abort_sequence) {
            if (gstate.abort_sequence_done()) {
                msg_printf(NULL, MSG_INFO, "Abort sequence done; exiting");
                break;
            }
        }
    }

    return finalize();
}
Ejemplo n.º 8
0
// Inform the service control manager that the service is about to
// start.
int initialize_service_dispatcher(int /*argc*/, char** /*argv*/) {
    fprintf(stdout, "\nStartServiceCtrlDispatcher being called.\n");
    fprintf(stdout, "This may take several seconds.  Please wait.\n");

    if (!StartServiceCtrlDispatcher(service_dispatch_table)) {
        log_message_error("StartServiceCtrlDispatcher failed.");
        return ERR_IO;
    }
    return 0;
}
Ejemplo n.º 9
0
void midnet_set_protocol(struct _tcp_session* client_request) {
	if(client_request==NULL) {
		log_message_error("MidNet: Setting Application Protocol",PROGRAM,"NULL request or TCP structure");
		return;
	}
	client_request->protocol_reader = &midnet_reader;
	client_request->protocol_writer = &midnet_writer;
	client_request->protocol_struct_alloc = &midnet_create_structure;
	client_request->protocol_struct = NULL;
}
Ejemplo n.º 10
0
int midnet_reader(struct _tcp_session* client_request) {
	char* token;

	if(client_request==NULL) {
		log_message_error("MidNet: Reading data from TCP",PROGRAM,"NULL TCP client structure");
		return -1;
	}
	if((client_request->position)>BUFFER_LENGTH || (client_request->position)<0) {
		log_message_error("MidNet: Reading data from TCP",PROGRAM,"Invalid buffer");
		return -1;
	}

	client_request->buffer[client_request->position] = '\0';

	token = strstr(client_request->buffer, "\n\n");
	if(token==NULL) {
		return 0;
	}
	return 1;
}
Ejemplo n.º 11
0
gboolean reconnect_mpd ( gpointer data )
{
  struct MAIN_DATA* main_data = data;
  int status;
  log_message_warn( main_data->logger, "Reconnecting to MPD again!" );

  status = mpd_reconnect( main_data->mpd );

  if ( status < 0 ) {
    log_message_error( main_data->logger, "Reconnection failed" );
    return TRUE;
  }
  (void)g_timeout_add_seconds( 1, poll_mpd, data );
  return FALSE;
}
Ejemplo n.º 12
0
Archivo: main.cpp Proyecto: Ashod/Boinc
int boinc_main_loop() {
    int retval;

    retval = initialize();
    if (retval) return retval;

    retval = gstate.init();
    if (retval) {
        log_message_error("gstate.init() failed", retval);
        return retval;
    }

    log_message_startup("Initialization completed");

    while (1) {
        if (!gstate.poll_slow_events()) {
            gstate.do_io_or_sleep(POLL_INTERVAL);
        }
        fflush(stderr);
        fflush(stdout);

        if (gstate.time_to_exit()) {
            msg_printf(NULL, MSG_INFO, "Time to exit");
            break;
        }
        if (gstate.requested_exit) {
            if (cc_config.abort_jobs_on_exit) {
                if (!gstate.in_abort_sequence) {
                    msg_printf(NULL, MSG_INFO,
                        "Exit requested; starting abort sequence"
                    );
                    gstate.start_abort_sequence();
                }
            } else {
                msg_printf(NULL, MSG_INFO, "Exiting");
                break;
            }
        }
        if (gstate.in_abort_sequence) {
            if (gstate.abort_sequence_done()) {
                msg_printf(NULL, MSG_INFO, "Abort sequence done; exiting");
                break;
            }
        }
    }

    return finalize();
}
Ejemplo n.º 13
0
// Setup the client software to monitor various system events
int initialize_system_monitor(int /*argc*/, char** /*argv*/) {

    // Windows: install console controls, the service control manager will send us
    // the needed events when we are running as a service.
    //
    if (!gstate.executing_as_daemon) {
        if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)console_control_handler, TRUE)){
            log_message_error("Failed to register the console control handler.");
            return ERR_IO;
        }
    }

    // Create a thread to receive system power events.
    //
    g_hWindowsMonitorSystemPowerThread = CreateThread(
        NULL,
        0,
        WindowsMonitorSystemPowerThread,
        NULL,
        0,
        NULL
    );

    if (!g_hWindowsMonitorSystemPowerThread) {
        g_hWindowsMonitorSystemPowerThread = NULL;
        g_hWndWindowsMonitorSystemPower = NULL;
    }

    // Create a thread to handle proxy auto-detection.
    //
    if (!cc_config.proxy_info.no_autodetect) {
        g_hWindowsMonitorSystemProxyThread = CreateThread(
            NULL,
            0,
            WindowsMonitorSystemProxyThread,
            NULL,
            0,
            NULL
        );

        if (!g_hWindowsMonitorSystemProxyThread) {
            g_hWindowsMonitorSystemProxyThread = NULL;
        }
    }

    return 0;
}
Ejemplo n.º 14
0
int midnet_writer(struct _tcp_session* client_request) {
	int ret;
	char* buffer = client_request->buffer;
	char* token;

	if(client_request==NULL) {
		log_message_error("MidNet: Writing data to TCP",PROGRAM,"NULL TCP client structure");
		return 0;
	}

	token = strstr(client_request->buffer, "\n\n");
	if(token==NULL) {
		log_message_error("MidNet: Writing data to TCP",PROGRAM,"Invalid TCP buffer");
		return 0;
	}

	if(token>=(buffer+BUFFER_LENGTH-1)) {
		log_message_error("MidNet: Writing data to TCP",PROGRAM,"Buffer Overflow");
		return 0;
	}
	*(token+2) = '\0';

	ret = clear_fd(0);
	if(ret==0) {
		log_message_error("MidNet: Writing data to TCP",PROGRAM,"Error while cleaning file descriptor");
		return 0;
	}

	ret = is_valid_request(buffer);
	if(ret==0) {
		log_message_error("MidNet: Writing data to TCP",PROGRAM,"Wrong command");
		return 0;
	}

	ret = midnet_processing_request(client_request);
	if(ret==0) {
		log_message_error("MidNet: Writing data to TCP",PROGRAM,"Error while processing the request");
		return 0;
	}

	client_request->limit = client_request->position;
	client_request->position = 0;
	client_request->close_request = 1;
	return 1;
}
Ejemplo n.º 15
0
// Setup the client software to monitor various system events
int initialize_system_monitor(int /*argc*/, char** /*argv*/) {

    // Windows: install console controls
    if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)console_control_handler, TRUE)){
        log_message_error("Failed to register the console control handler.");
        return ERR_IO;
    }

    // Create a window to receive system power events.
    g_hWindowsMonitorSystemPowerThread = CreateThread(
        NULL,
        0,
        WindowsMonitorSystemPowerThread,
        NULL,
        0,
        NULL);

    if (!g_hWindowsMonitorSystemPowerThread) {
        g_hWindowsMonitorSystemPowerThread = NULL;
        g_hWndWindowsMonitorSystemPower = NULL;
    }

    // Create a thread to handle proxy auto-detection.
    g_hWindowsMonitorSystemProxyThread = CreateThread(
        NULL,
        0,
        WindowsMonitorSystemProxyThread,
        NULL,
        0,
        NULL);

    if (!g_hWindowsMonitorSystemProxyThread) {
        g_hWindowsMonitorSystemProxyThread = NULL;
    }

    return 0;
}
Ejemplo n.º 16
0
int midnet_processing_request(struct _tcp_session* client_request) {
	fd_set globalrfds;
	fd_set readfds;
	struct timeval tv;
	int nfds;
	int sel;
	FD_ZERO(&globalrfds);
	FD_SET(0, &globalrfds);
	nfds = 0;

	if(client_request==NULL) {
		log_message_error("MidNet: Processing request",PROGRAM,"NULL request or TCP structure");
		return 0;
	}

	char* request = client_request->buffer;

	int ret;
	int size;
	char* end_of_flow;
	int position = 0;

	size = strlen(request);
	if(size<=0) {
		log_message_error("MidNet: Processing request",PROGRAM,"Empty request");
		return 0;
	}

	char* buffer;
	buffer = client_request->buffer;

	ret = write(1, request, size);

	client_request->position = 0;

	do {
		if(position>=BUFFER_LENGTH) {
			log_message_error("MidNet: Processing request",PROGRAM,"Receiving from core: TCP buffer overflow");
			return 0;
		}
		readfds = globalrfds;
		tv.tv_sec = INTER_COMMUNICATION_DELAY_SEC;
		tv.tv_usec = INTER_COMMUNICATION_DELAY_USEC;
		sel = select(nfds+1, &readfds, NULL, NULL, &tv);
		if(sel==0) {
			log_message_error("MidNet: Processing request",PROGRAM,"The core server doesn't respond");
			return 0;
		}
		size = read(0, buffer+position, BUFFER_LENGTH - position);
		buffer[position+size] = '\0';
		position = position + size;

		end_of_flow = strstr(buffer, "\n\n");
		if(end_of_flow!=NULL) {
			client_request->position = position;
			return 1;
		}

		if(position==BUFFER_LENGTH) {
			client_request->position = position;
			ret = flush_buffer(client_request);
			if(ret==0) {
				log_message_error("MidNet: Processing request",PROGRAM,"Could not flush the TCP buffer, buffer overflow");
				return 0;
			}
		}
	} while(1);
}
Ejemplo n.º 17
0
int boinc_main_loop() {
    int retval;
    
    retval = initialize();
    if (retval) return retval;

    retval = gstate.init();
    if (retval) {
        log_message_error("gstate.init() failed", retval);
        return retval;
    }

    // must parse env vars after gstate.init();
    // otherwise items will get overwritten with state file info
    //
    gstate.parse_env_vars();

    // do this after parsing env vars
    //
    proxy_info_startup();

    if (gstate.projects.size() == 0) {
        msg_printf(NULL, MSG_INFO,
            "This computer is not attached to any projects"
        );
        msg_printf(NULL, MSG_INFO,
            "Visit http://boinc.berkeley.edu for instructions"
        );
    }

    log_message_startup("Initialization completed");

    while (1) {
        if (!gstate.poll_slow_events()) {
            gstate.do_io_or_sleep(POLL_INTERVAL);
        }
        fflush(stderr);
        fflush(stdout);

        if (gstate.time_to_exit()) {
            msg_printf(NULL, MSG_INFO, "Time to exit");
            break;
        }
        if (gstate.requested_exit) {
            if (config.abort_jobs_on_exit) {
                if (!gstate.in_abort_sequence) {
                    msg_printf(NULL, MSG_INFO,
                        "Exit requested; starting abort sequence"
                    );
                    gstate.start_abort_sequence();
                }
            } else {
                msg_printf(NULL, MSG_INFO, "Exit requested by user");
                break;
            }
        }
        if (gstate.in_abort_sequence) {
            if (gstate.abort_sequence_done()) {
                msg_printf(NULL, MSG_INFO, "Abort sequence done; exiting");
                break;
            }
        }
        if (gstate.requested_suspend) {
            gstate.run_mode.set(RUN_MODE_NEVER, 3600);
            gstate.network_mode.set(RUN_MODE_NEVER, 3600);
            gstate.requested_suspend = false;
        }
        if (gstate.requested_resume) {
            gstate.run_mode.set(RUN_MODE_RESTORE, 0);
            gstate.network_mode.set(RUN_MODE_RESTORE, 0);
            gstate.requested_resume = false;
        }
    }

    return finalize();
}