Ejemplo n.º 1
0
ERROR_CODE run_service_s(char *program_name, struct hash_map_t *arguments) {
    /* allocates space for the error value that will be used
    to check for an error in the call */
    ERROR_CODE return_value;

    /* initializes the service creating the structures and starting
    the values for the configuration of it */
    return_value = init_service(program_name, arguments);
    if(IS_ERROR_CODE(return_value)) {
        RAISE_AGAIN(return_value);
    }

    /* run the service, blocking the call until the service is
    finished, the retrives the return value from it */
    return_value = run_service();
    if(IS_ERROR_CODE(return_value)) {
        RAISE_AGAIN(return_value);
    }

    /* destroys the service eliminating any structures that have
    been created in the service life-time */
    return_value = destroy_service();
    if(IS_ERROR_CODE(return_value)) {
        RAISE_AGAIN(return_value);
    }

    /* raises no error as the execution of the service went normally
    and no problems have been issued */
    RAISE_NO_ERROR;
}
Ejemplo n.º 2
0
ERROR_CODE ran_service() {
    /* allocates the return value */
    ERROR_CODE return_value;

    /* in case the service status is open */
    if(service->status == STATUS_CLOSED) {
        /* prints a debug message */
        V_DEBUG("No service to be stopped\n");
    } else {
        /* prints a debug message */
        V_DEBUG("Stopping service\n");

        /* stops the service, this call should make the
        required changes in the service structure so that
        it's stopped as soon as possible */
        return_value = stop_service(service);

        /* tests the error code for error */
        if(IS_ERROR_CODE(return_value)) {
            /* runs the socket finish so that the proper cleanup
            operations are performed and then re-raises the error*/
            SOCKET_FINISH();
            RAISE_AGAIN(return_value);
        }

        /* prints a debug message */
        V_DEBUG("Finished stopping service\n");
    }

    /* raises no error */
    RAISE_NO_ERROR;
}
Ejemplo n.º 3
0
ERROR_CODE run_service() {
    /* allocates the return value to be used to gather
    the error result from the service calls */
    ERROR_CODE return_value;

    /* allocates the socket data and then initializes
    the socket infrastructure (global structures) with it */
    SOCKET_DATA socket_data;
    SOCKET_INITIALIZE(&socket_data);

    /* starts the service, this call should be able to bootstrap
    all the required structures and initialize the main loop, this
    should block the control flow fduring the run of the service */
    return_value = start_service(service);

    /* tests the error code value for error and in case there's
    one runs the appropriate measures */
    if(IS_ERROR_CODE(return_value)) {
        /* runs the socket finish so that the proper cleanup
        operations are performed and then re-raises the error*/
        SOCKET_FINISH();
        RAISE_AGAIN(return_value);
    }

    /* runs the socket finish releasing any pending memory information
    regarding the socket infra-structure */
    SOCKET_FINISH();

    /* raises no error */
    RAISE_NO_ERROR;
}
Ejemplo n.º 4
0
void 
pnm_save(pnm self, pnmType type, char *path)
{
    FILE *output = L_w_open(path);

    HANDLE(error, L_save(self, type, output));
    fclose(output);
    if (EXCEPTION_RAISED(any))
 	RAISE_AGAIN();
}
Ejemplo n.º 5
0
int main(int argc, char *argv[]) {
    /* allocates the space for the "final" result code
    that is going to be returned as part of the normal
    command execution, a positive or negative values
    should idicate an error, a zero value indicates that
    a normal execution has just finished */
    ERROR_CODE return_value;

    /* allocates space for the name of the program
    (process) to be executed */
    char *program_name;

    /* allocates the map that will contain the various
    processed arguments, indexed by name */
    struct hash_map_t *arguments;

    /* prints a debug message */
    V_DEBUG_F("Receiving %d argument(s)\n", argc);

    /* in case the number of arguments is less than one
    (exception case) returns in error */
    if(argc < 1) {
        cleanup(NULL);
        RAISE_ERROR_S(1);
    }

    /* retrieves the first argument value as the name
    of the process (program) to be executed */
    program_name = argv[0];

    /* processes the various arguments into a map and then
    executes the corresponding (initial) actions */
    process_arguments(argc, argv, &arguments);
    return_value = execute_arguments(program_name, arguments);

    /* cleans the current process information so that
    no remaining structure or resource is left in an
    invalid or erroneous state */
    cleanup(arguments);

    /* deletes the processed arguments and then cleans up
    the pool based memory allocation system releasing all
    of its memory before the exit (no leaks) */
    delete_arguments(arguments);
    cleanup_palloc();

    /* prints a debug message about the ending of the sytem
    for the execution of the service and then returns the
    normal return code (success status) to the caller process */
    V_DEBUG_F(
        "Finishing process [%ld pending]\n",
        (long int) ALLOCATIONS
    );
    RAISE_AGAIN(return_value);
}
Ejemplo n.º 6
0
ERROR_CODE init_service(char *program_name, struct hash_map_t *arguments) {
    /* allocates the return value to be used to gather
    the error result from the service calls */
    ERROR_CODE return_value;

    /* creates the service and loads the options
    taking into account the arguments */
    create_service(
        &service,
        (unsigned char *) VIRIATUM_NAME,
        (unsigned char *) program_name
    );
    return_value = load_specifications(service);
    if(IS_ERROR_CODE(return_value)) {
        RAISE_AGAIN(return_value);
    }
    return_value = load_options_service(service, arguments);
    if(IS_ERROR_CODE(return_value)) {
        RAISE_AGAIN(return_value);
    }
    return_value = calculate_options_service(service);
    if(IS_ERROR_CODE(return_value)) {
        RAISE_AGAIN(return_value);
    }

    /* updates the registers signals handler so that the service
    may be able to register the handlers at the proper timing */
    service->register_signals = register_signals;

    /* calculates the locations structure for the service based
    on the currently loaded configuration, this a complex operation */
    calculate_locations_service(service);

    /* runs the printing operation on the service, this should
    output the information to the standar output */
    print_options_service(service);

    /* raises no error to the caller method, normal
    exit operation (should provide no problem) */
    RAISE_NO_ERROR;
}
Ejemplo n.º 7
0
pnm
pnm_load(char *path)
{
    FILE *input = L_r_open(path);
    pnm self = NULL;

    HANDLE(any, self = L_load(input));
    fclose(input);

    if (EXCEPTION_RAISED(any))
    {
	if (self != NULL)
	    memory_free(self);

	if (EXCEPTION_RAISED(get_int))
	    RAISE(error, "Truncated pnm file");

	if (EXCEPTION_RAISED(any))
	    RAISE_AGAIN();
    }
    return self;
}
Ejemplo n.º 8
0
ERROR_CODE run_speed_tests() {
    ERROR_CODE return_value = run_test_case(exec_speed_tests, "speed_tests");
    RAISE_AGAIN(return_value);
}
Ejemplo n.º 9
0
int execute_arguments(char *program_name, struct hash_map_t *arguments) {
    /* allocates space for the possible argument
    to be executed from the arguments map */
    void *value;

    /* allocates the value to be used to verify the
    exitence of error from the function */
    ERROR_CODE return_value;

    /* sets space for the flag that will control if
    the service should be run or not, this is used
    for certain situations (mostyle test) where the
    service is not meant to be run */
    char run_service = TRUE;

    /* tries to retrieve the help argument from the arguments
    map in case the value exists prints the help value and then
    exits the current system */
    get_value_string_hash_map(arguments, (unsigned char *) "help", &value);
    if(value != NULL) {
        return help();
    }

    /* tries to retrieve the version argument from the arguments
    map in case the value exists prints the version value and then
    exits the current system */
    get_value_string_hash_map(arguments, (unsigned char *) "version", &value);
    if(value != NULL) {
        return version();
    }

    /* retrieves the test argument value from the arguments map
    and in case it's set starts the test process runing a series
    of test functions in sequence */
    get_value_string_hash_map(arguments, (unsigned char *) "test", &value);
    if(value != NULL) {
        return test();
    }

    /* retrieves the speed argument value from the arguments map
    and in case it's set starts the speed measuring and disables
    the runnig of the service */
    get_value_string_hash_map(arguments, (unsigned char *) "speed", &value);
    if(value != NULL) {
        return speed();
    }

    /* tries to retrieve the daemon argument from the
    arguments map in case the value is set daemonizes
    the current process so that it remains in background
    and returns to the caller process immediately, otherwise
    prints the viriatum information into the standard
    output "file", the label should be standard */
    get_value_string_hash_map(arguments, (unsigned char *) "daemon", &value);
    if(value != NULL) {
        daemonize();
    }
    else {
        print_information();
    }

    /* tries to retrieve the local argument from the arguments
    map in case the value exists localizes the current service
    so that any file read is read from the current directory */
    get_value_string_hash_map(arguments, (unsigned char *) "local", &value);
    if(value != NULL) {
        localize();
    }

    /* in cas the flag that control if the service must be run is
    unset the control flow must be returned immediately (avoids
    running service) */
    if(run_service == FALSE) {
        RAISE_NO_ERROR;
    }

    /* runs the service, with the given arguments, this call
    should block the program control flow until an event
    stop the running of the main loop */
    return_value = run_service_s(program_name, arguments);

    /* tests the error code for error in case it exists
    prints a message indicating the problem that occurred */
    if(IS_ERROR_CODE(return_value)) {
        V_ERROR_F("Problem running service (%s)\n", (char *) GET_ERROR());
        RAISE_AGAIN(return_value);
    }

    /* returns the normal result value as no problems has
    occured during the execution of the command */
    RAISE_NO_ERROR;
}