int dump_sql_error(SQLRETURN retcode, SQLSMALLINT type, SQLHANDLE hsql, unsigned int statflag) { SQLCHAR SqlState[6], Msg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER error; SQLSMALLINT len; SQLRETURN rc; if (statflag > 0) printf("err\n"); rc = SQLGetDiagRec(type, hsql, 1, SqlState, &error, Msg, SQL_MAX_MESSAGE_LENGTH, &len); if (rc != SQL_NO_DATA) { if (len >= SQL_MAX_MESSAGE_LENGTH) { Msg[SQL_MAX_MESSAGE_LENGTH] = '\0'; } else { Msg[len] = '\0'; } fprintf(stderr, "SQL error: %d -- %s\n%s\n", error, SqlState, Msg); } else { fprintf(stderr, "SQL error: %d\n", retcode); } close_handles(); return 1; }
/** * Parse and execute a simple command, by either creating a new processing or * internally process it. */ bool parse_simple(simple_command_t *s, int level, command_t *father, HANDLE *h) { BOOL ret; SECURITY_ATTRIBUTES sa; STARTUPINFO siStartupInfo; PROCESS_INFORMATION piProcessInfo; HANDLE hOutFile = INVALID_HANDLE_VALUE; HANDLE hInFile = INVALID_HANDLE_VALUE; HANDLE hErrFile = INVALID_HANDLE_VALUE; BOOL changed = FALSE; int f_ret; ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; ZeroMemory(&siStartupInfo, sizeof(siStartupInfo)); ZeroMemory(&piProcessInfo, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); /* TODO sanity checks */ if(s->verb->next_part != NULL){ ret = SetEnvironmentVariable((LPCTSTR)s->verb->string, (LPCTSTR)s->verb->next_part->next_part->string); return ret-1; } /* TODO if builtin command, execute the command */ if(strcmp(s->verb->string, "exit") == 0 || strcmp(s->verb->string, "quit") == 0){ exit(0); } if(strcmp(s->verb->string, "cd") == 0){ if(s->params){ changed = SetCurrentDirectory(get_word(s->params)); } if(changed){ return 0; } else{ hOutFile = FileOpen((LPCSTR)get_word(s->out), "w"); CloseHandle(hOutFile); return 0; } } /* TODO if variable assignment, execute the assignment and return * the exit status */ /* TODO if external command: * 1. set handles * 2. redirect standard input / output / error * 3. run command * 4. get exit code */ redirrect_command(s, &siStartupInfo, &hInFile, &hOutFile, &hErrFile); f_ret = exec_simple_proc(s, hErrFile, sa, siStartupInfo, piProcessInfo); /* TODO replace with actual exit status */ close_handles(hInFile, hOutFile, hErrFile); return f_ret; }
void Win32Socket::set_handle(SOCKET new_handle, bool new_close_handle) { close_handles(); handle = new_handle; close_handle = new_close_handle; create_event_handles(); select_events(); }
void Session::on_event(const SessionEvent& event) { switch (event.type) { case SessionEvent::CONNECT: { int port = config_.port(); const Config::ContactPointList& contact_points = config_.contact_points(); for (Config::ContactPointList::const_iterator it = contact_points.begin(), end = contact_points.end(); it != end; ++it) { const std::string& seed = *it; Address address; if (Address::from_string(seed, port, &address)) { add_host(address); } else { pending_resolve_count_++; Resolver::resolve(loop(), seed, port, this, on_resolve); } } if (pending_resolve_count_ == 0) { internal_connect(); } break; } case SessionEvent::NOTIFY_READY: if (pending_pool_count_ > 0) { if (--pending_pool_count_ == 0) { LOG_DEBUG("Session is connected"); notify_connected(); } LOG_DEBUG("Session pending pool count %d", pending_pool_count_); } break; case SessionEvent::NOTIFY_WORKER_CLOSED: if (--pending_workers_count_ == 0) { LOG_DEBUG("Session is disconnected"); control_connection_.close(); close_handles(); } break; case SessionEvent::NOTIFY_UP: control_connection_.on_up(event.address); break; case SessionEvent::NOTIFY_DOWN: control_connection_.on_down(event.address); break; default: assert(false); break; } }
void Win32Socket::disconnect_graceful(int timeout) { if (handle != INVALID_SOCKET) { linger linger_value; linger_value.l_onoff = 1; linger_value.l_linger = (timeout + 500) / 1000; setsockopt(handle, SOL_SOCKET, SO_LINGER, (const char *) &linger_value, sizeof(linger)); } close_handles(); }
Win32Socket::Win32Socket(SOCKET handle) : handle(handle), close_handle(true), event_handle(0), receive_handle(0), send_handle(0), except_handle(0) { try { create_event_handles(); select_events(); } catch (...) { close_handles(); throw; } }
void Win32Socket::disconnect_abortive() { if (handle != INVALID_SOCKET) { // This should cause a hard closure according to the winsock documentation, // so since that's what Microsoft officially says, it is probably not what // is going to happen.. linger linger_value; linger_value.l_onoff = 1; linger_value.l_linger = 0; setsockopt(handle, SOL_SOCKET, SO_LINGER, (const char *) &linger_value, sizeof(linger)); } close_handles(); }
int exec_with_redirect_from_stdout(char *command_line, BOOL use_cmd_exe, char *** results) { execution_details * details = (execution_details *)palloc(sizeof(execution_details)); if (!initialize_files(details)) return FALSE; // use the current OS comspec for DOS commands, i.e., DIR char cmd[MAX_PATH] = { 0 }; prepare_command_line(cmd, command_line, use_cmd_exe); if (!execute_process(details, cmd)) return FALSE; int result_count = extract_result(details, results); close_handles(details); DeleteFile(details->temp_filename); pfree(details); return result_count; }
/** * Process two commands in parallel, by creating two children. */ static bool do_in_parallel(command_t *cmd1, command_t *cmd2, int level, command_t *father) { /* TODO execute cmd1 and cmd2 simultaneously */ BOOL ret_1, ret_2; char* cmd; SECURITY_ATTRIBUTES sa_1; STARTUPINFO siStartupInfo_1; PROCESS_INFORMATION piProcessInfo_1; HANDLE hOutFile_1 = INVALID_HANDLE_VALUE; HANDLE hInFile_1 = INVALID_HANDLE_VALUE; HANDLE hErrFile_1 = INVALID_HANDLE_VALUE; SECURITY_ATTRIBUTES sa_2; STARTUPINFO siStartupInfo_2; PROCESS_INFORMATION piProcessInfo_2; HANDLE hOutFile_2 = INVALID_HANDLE_VALUE; HANDLE hInFile_2 = INVALID_HANDLE_VALUE; HANDLE hErrFile_2 = INVALID_HANDLE_VALUE; BOOL changed = FALSE; char buffer[100]; int x; ZeroMemory(&sa_1, sizeof(SECURITY_ATTRIBUTES)); sa_1.nLength = sizeof(SECURITY_ATTRIBUTES); sa_1.bInheritHandle = TRUE; ZeroMemory(&siStartupInfo_1, sizeof(siStartupInfo_1)); ZeroMemory(&piProcessInfo_1, sizeof(piProcessInfo_1)); siStartupInfo_1.cb = sizeof(siStartupInfo_1); ZeroMemory(&sa_2, sizeof(SECURITY_ATTRIBUTES)); sa_2.nLength = sizeof(SECURITY_ATTRIBUTES); sa_2.bInheritHandle = TRUE; ZeroMemory(&siStartupInfo_2, sizeof(siStartupInfo_2)); ZeroMemory(&piProcessInfo_2, sizeof(piProcessInfo_2)); siStartupInfo_2.cb = sizeof(siStartupInfo_2); if(cmd2->up != NULL){ if(cmd2->up->op != OP_PARALLEL){ return parse_command(cmd2->up, level, father, (void*)(0)) == 0; } } if(cmd1->scmd){ redirrect_command(cmd1->scmd, &siStartupInfo_1, &hInFile_1, &hOutFile_1, &hErrFile_1); cmd = get_argv(cmd1->scmd); ret_1 = CreateProcess(NULL, (LPSTR)cmd, (LPSECURITY_ATTRIBUTES)&sa_1, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &siStartupInfo_1, &piProcessInfo_1); free(cmd); } if(cmd2->scmd){ redirrect_command(cmd2->scmd, &siStartupInfo_2, &hInFile_2, &hOutFile_2, &hErrFile_2); cmd = get_argv(cmd2->scmd); ret_2 = CreateProcess(NULL, (LPSTR)cmd, (LPSECURITY_ATTRIBUTES)&sa_2, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &siStartupInfo_2, &piProcessInfo_2); free(cmd); } if(cmd1->scmd){ if(ret_1 == FALSE){ sprintf(buffer, "%s: No such file or directory\n", cmd); WriteFile(hErrFile_1, buffer, strlen(buffer), &x, NULL); } else{ WaitForSingleObject(piProcessInfo_1.hProcess, INFINITE); GetExitCodeProcess(piProcessInfo_1.hProcess, &ret_1); CloseHandle(piProcessInfo_1.hProcess); CloseHandle(piProcessInfo_1.hThread); } close_handles(hInFile_1, hOutFile_1, hErrFile_1); } if(cmd2->scmd){ if(ret_2 == FALSE){ sprintf(buffer, "%s: No such file or directory\n", cmd); WriteFile(hErrFile_2, buffer, strlen(buffer), &x, NULL); } else{ WaitForSingleObject(piProcessInfo_2.hProcess, INFINITE); GetExitCodeProcess(piProcessInfo_2.hProcess, &ret_2); CloseHandle(piProcessInfo_2.hProcess); CloseHandle(piProcessInfo_2.hThread); } close_handles(hInFile_2, hOutFile_2, hErrFile_2); } if(cmd1->scmd && cmd2->scmd){ return (ret_2 == 0 && ret_1 == 0); } if(!cmd1->scmd){ do_in_parallel(cmd1->cmd1, cmd1->cmd2, level, cmd1); } if(!cmd2->scmd){ do_in_parallel(cmd2->cmd1, cmd2->cmd2, level, cmd2); } return (ret_1 && ret_2); /* TODO replace with actual exit status */ }
int main(int argc, char* argv[]) { int c; char *dbname = (char *)DBNAME; char *dbinst = (char *)DBINST; char *hostname = (char *)HOSTNAME; char *datadir = NULL; char *verdesc = NULL; char *verstr = NULL; char *prefix = NULL; unsigned int version = 0; char *schemastr = NULL; unsigned short usage = 0; unsigned short dblist = 0; unsigned short schemalist = 0; unsigned short prefixlist = 0; unsigned short trusted = 1; unsigned char term = '\0'; // source files char udd_sql3[BUFLEN] = NULLSTR; char cert_yaml[BUFLEN] = NULLSTR; char gfxids_yaml[BUFLEN] = NULLSTR; char iconids_yaml[BUFLEN] = NULLSTR; char typeids_yaml[BUFLEN] = NULLSTR; char blueprints_yaml[BUFLEN] = NULLSTR; // SQL things int db3_rc; SQLRETURN ret; SQLCHAR connStrOut[BUFLEN] = NULLSTR; SQLCHAR dsn[BUFLEN] = NULLSTR; SQLCHAR auth[BUFLEN] = NULLSTR; SQLCHAR usr[BUFLEN] = NULLSTR; SQLCHAR pwd[BUFLEN] = NULLSTR; SQLSMALLINT connStrLen; // set globals GZIP_FLAG = 1; JSONP_FLAG = 1; SCHEMA = 0; JSON_DIR = NULL; H_ENV = SQL_NULL_HENV; H_DBC = SQL_NULL_HDBC; H_DBC2 = SQL_NULL_HDBC; DB3_UD = NULL; while ((c = getopt(argc, argv, "i:o:d:u:p:n:N:s:x:hvXDSZP")) != -1) { switch (c) { case 'i': datadir = (char *)malloc(BUFLEN); strlcpy(datadir, optarg, BUFLEN); term = datadir[strlen(datadir) - 1]; if (term != PATHSEP) { strlcat(datadir, SZPATHSEP, BUFLEN); } break; case 'o': JSON_DIR = (char *)malloc(BUFLEN); strlcpy(JSON_DIR, optarg, BUFLEN); term = JSON_DIR[strlen(JSON_DIR) - 1]; if (term != PATHSEP) { strlcat(JSON_DIR, SZPATHSEP, BUFLEN); } break; case 'x': prefix = (char *)malloc(BUFLEN); strlcpy(prefix, optarg, BUFLEN); break; case 'd': dbname = (char *)malloc(BUFLEN); strlcpy(dbname, optarg, BUFLEN); break; case 'n': verstr = (char *)malloc(BUFLEN); strlcpy(verstr, optarg, BUFLEN); break; case 'N': verdesc = (char *)malloc(BUFLEN); strlcpy(verdesc, optarg, BUFLEN); break; case 's': schemastr = (char *)malloc(BUFLEN); strlcpy(schemastr, optarg, BUFLEN); break; case 'S': schemalist = 1; break; case 'v': printf("sdd2json version %d.%d.%d", SDD2JSON_V_MAJOR, SDD2JSON_V_MINOR, SDD2JSON_V_PATCH); return 0; case 'D': dblist = 1; break; case 'u': trusted = 0; strlcpy(usr, optarg, BUFLEN); break; case 'p': trusted = 0; strlcpy(pwd, optarg, BUFLEN); break; case 'P': JSONP_FLAG = 0; break; case 'Z': GZIP_FLAG = 0; break; case 'X': prefixlist = 1; case '?': printf("\n"); usage = 1; break; } } if (1 == schemalist) { for (unsigned int i = 0; i < VERS_N; i++) { printf("%s:\tversion %d;\tschema %d\n", VERS[i].version_desc, VERS[i].version_id, VERS[i].schema_id); } return 0; } if (1 == prefixlist) { printf("\tcrp\n"); printf("\tdgm\n"); printf("\tinv\n"); printf("\tmap\n"); printf("\tram\n"); printf("\tsta\n"); return 0; } if (usage < 1 && datadir == NULL) { fprintf(stderr, "EVE static data dir path is required\n"); usage = 1; } if (usage < 1 && verstr == NULL) { fprintf(stderr, "static data version ID is required\n"); usage = 1; } if (usage > 0) return dump_usage(); // parse version/schema from params and known values if (verstr != NULL) { version = atoi(verstr); } if (version == 0) { fprintf(stderr, "the static data version ID provided is invalid: %s\n", verstr); return 1; } unsigned int found_version = 0; for (unsigned int i = 0; i < VERS_N; i++) { if (VERS[i].version_id == version) { found_version = 1; SCHEMA = VERS[i].schema_id; if (NULL == verdesc) { verdesc = (char *)malloc(BUFLEN); strlcpy(verdesc, VERS[i].version_desc, BUFLEN); } break; } } if (1 != found_version) { fprintf(stderr, "warning: using unknown static data version ID: %d\n", version); } if (schemastr != NULL) { if (0 != SCHEMA) { fprintf(stderr, "warning: overriding known schema ID\n"); } SCHEMA = atoi(schemastr); } if (0 == SCHEMA) { fprintf(stderr, "schema ID is required\n"); return 1; } printf("static data: '%s', version %d, schema %d\n", verdesc, version, SCHEMA); // validate input/schema printf("checking input: %s - ", datadir); if (ACCESS(datadir, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access output path\n"); return 1; } if (SCHEMA >= 100038) { strlcpy(cert_yaml, datadir, BUFLEN); strlcat(cert_yaml, "certificates.yaml", BUFLEN); if (ACCESS(cert_yaml, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access %s\n", cert_yaml); return 1; } } if (SCHEMA >= 100038) { strlcpy(gfxids_yaml, datadir, BUFLEN); strlcat(gfxids_yaml, "graphicIDs.yaml", BUFLEN); if (ACCESS(gfxids_yaml, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access %s\n", gfxids_yaml); return 1; } } if (SCHEMA >= 100038) { strlcpy(iconids_yaml, datadir, BUFLEN); strlcat(iconids_yaml, "iconIDs.yaml", BUFLEN); if (ACCESS(iconids_yaml, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access %s\n", iconids_yaml); return 1; } } if (SCHEMA >= 100038) { strlcpy(typeids_yaml, datadir, BUFLEN); strlcat(typeids_yaml, "typeIDs.yaml", BUFLEN); if (ACCESS(typeids_yaml, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access %s\n", typeids_yaml); return 1; } } if (SCHEMA >= 100038) { strlcpy(udd_sql3, datadir, BUFLEN); strlcat(udd_sql3, "universeDataDx.db", BUFLEN); if (ACCESS(udd_sql3, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access %s\n", udd_sql3); return 1; } } if (SCHEMA >= 100038) { strlcpy(blueprints_yaml, datadir, BUFLEN); strlcat(blueprints_yaml, "blueprints.yaml", BUFLEN); if (ACCESS(blueprints_yaml, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access %s\n", blueprints_yaml); return 1; } } printf("OK\n"); // validate output if (JSON_DIR == NULL) { JSON_DIR = (char *)malloc(2 * BUFLEN); strlcpy(JSON_DIR, datadir, 2 * BUFLEN); strlcat(JSON_DIR, SDD, 2* BUFLEN); strlcat(JSON_DIR, SZPATHSEP, 2 * BUFLEN); } printf("checking output: %s - ", JSON_DIR); if (ACCESS(JSON_DIR, 0) != 0) { if (MKDIR(JSON_DIR) != 0) { printf("err\n"); fprintf(stderr, "could not create output path\n"); return 1; } if (ACCESS(JSON_DIR, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access output path\n"); return 1; } } printf("OK\n"); // connect to SQLLITE dbs printf("connecting to [%s] - ", udd_sql3); db3_rc = sqlite3_open(udd_sql3, &DB3_UD); if (SQLITE_OK != db3_rc) return dump_db3_error(DB3_UD, 1); printf("OK\n"); // connect to SQL server if (0 != trusted) { SNPRINTF(auth, BUFLEN, "Trusted_Connection=yes"); } else { SNPRINTF(auth, BUFLEN, "User ID=%s;Password=%s", usr, pwd); } printf("connecting to [%s\\%s] using [%s] - ", hostname, dbinst, auth); ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, &H_ENV); if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1); ret = SQLSetEnvAttr(H_ENV, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0); if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1); if (dblist > 0) { printf("list drivers\n"); unsigned short ret = check_drivers(1); close_handles(); return ret; } if (0 != check_drivers(0)) { printf("err\n"); fprintf(stderr, "SQL server driver not found\n"); close_handles(); return 1; } ret = SQLAllocHandle(SQL_HANDLE_DBC, H_ENV, &H_DBC); if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1); ret = SQLAllocHandle(SQL_HANDLE_DBC, H_ENV, &H_DBC2); if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1); SNPRINTF(dsn, BUFLEN, "Driver={%s};Server=%s\\%s;Database=%s;%s;", SQLDRV, hostname, dbinst, dbname, auth); ret = SQLDriverConnect(H_DBC, NULL, dsn, strnlen(dsn, BUFLEN), connStrOut, BUFLEN, &connStrLen, SQL_DRIVER_NOPROMPT); if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_DBC, H_DBC, 1); ret = SQLDriverConnect(H_DBC2, NULL, dsn, strnlen(dsn, BUFLEN), connStrOut, BUFLEN, &connStrLen, SQL_DRIVER_NOPROMPT); if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_DBC, H_DBC2, 1); printf("OK\n"); // create metainfo file char metafile[BUFLEN] = NULLSTR; strlcpy(metafile, JSON_DIR, BUFLEN); strlcat(metafile, "metainf.json", BUFLEN); FILE *f = fopen(metafile, "w"); if (f == NULL) { printf("err\n"); fprintf(stderr, "error opening meta file: %s", metafile); close_handles(); return 1; } printf("OK\n"); fprintf(f, "{\n"); fprintf(f, "\"formatID\":%d,\n", FORMAT_ID); fprintf(f, "\"schema\":%d,\n", SCHEMA); fprintf(f, "\"copy\":\"%s\",\n", CCPR); fprintf(f, "\"version\":%d,\n", version); fprintf(f, "\"verdesc\":\"%s\",\n", verdesc); fprintf(f, "\"tables\":{\n"); // do stuff int rc = 0; int comma = 0; if (NULL == prefix || strncmp(prefix, "crp", 3) == 0) { if (comma++ > 0) fprintf(f, ",\n"); rc = create_crp(f); if (rc != 0) { close_handles(); fclose(f); return 1; } } if (NULL == prefix || strncmp(prefix, "dgm", 3) == 0) { if (comma++ > 0) fprintf(f, ",\n"); rc = create_dgm(f, cert_yaml); if (rc != 0) { close_handles(); fclose(f); return 1; } } if (NULL == prefix || strncmp(prefix, "inv", 3) == 0) { if (comma++ > 0) fprintf(f, ",\n"); rc = create_inv(f, typeids_yaml, iconids_yaml); if (rc != 0) { close_handles(); fclose(f); return 1; } } if (NULL == prefix || strncmp(prefix, "map", 3) == 0) { if (comma++ > 0) fprintf(f, ",\n"); rc = create_map(f); if (0 != rc) { close_handles(); fclose(f); return 1; } } if (NULL == prefix || strncmp(prefix, "ram", 3) == 0) { if (comma++ > 0) fprintf(f, ",\n"); ret = create_ram(f, blueprints_yaml); if (!SQL_SUCCEEDED(ret)) { close_handles(); fclose(f); return 1; } } if (NULL == prefix || strncmp(prefix, "sta", 3) == 0) { if (comma++ > 0) fprintf(f, ",\n"); ret = create_sta(f); if (!SQL_SUCCEEDED(ret)) { close_handles(); fclose(f); return 1; } } fprintf(f, "\n}\n"); // end of sources // clean up connections fprintf(f, "}\n"); fclose(f); printf("metainf - ", metafile); post_file("metainf"); close_handles(); free(datadir); free(verdesc); free(verstr); free(prefix); free(JSON_DIR); printf("\nall done!\n"); return 0; }
/** * main() **/ int main(int argc, char *argv[]) { int result; int nfds; fd_set rfds; int pscc_event_fd; int sterclog_fd = -1; struct sockaddr_un ctrl_addr_un; struct sockaddr_un event_addr_un; struct sockaddr_in ctrl_addr_in; struct sockaddr_in event_addr_in; pid_t pid; pid_t sid; sterc_transport_type_t transport_type = tt_unix; sterc_transport_type_t pscc_transport_type = tt_unix; char *pscc_ctrl_p = (char*)pscc_ctrl,*pscc_event_p=(char*)pscc_event; int pscc_reconnect_tries = 5; mpl_param_element_t* param_elem_p; /* optarg stuff */ int c; extern char *optarg; extern int optind, optopt, opterr; int option_index = 0; static struct option long_options[] = { {"pt", 1, 0, 0}, {"pc", 1, 0, 0}, {"pe", 1, 0, 0}, {0, 0, 0, 0} }; /* optarg end */ /* Set umask before anything else */ util_set_restricted_umask(); sterclog_fd = sterc_log_init(); while ((c = getopt(argc, argv, "fd:t:r:c:s:")) != -1) { switch (c) { case 0: if(strncmp(long_options[option_index].name,"pt",2) == 0) { pscc_transport_type = get_transport_type(optarg); if (pscc_transport_type == tt_unknown) { print_usage(argv[0]); exit(1); } } else if(strncmp(long_options[option_index].name,"pc",2) == 0) pscc_ctrl_p = optarg; else if(strncmp(long_options[option_index].name,"pe",2) == 0) pscc_event_p = optarg; else { print_usage(argv[0]); exit(1); } break; case 'f': daemon_mode = 0; break; case 'd': debug_mode = strtol(optarg, NULL, 10); break; case 'c': config_path = optarg; break; case 'r': sterc_runscript_path_connect = optarg; break; case 's': sterc_runscript_path_disconnect = optarg; break; case 't': transport_type = get_transport_type(optarg); break; case ':': case '?': print_usage(argv[0]); exit(1); } } if (daemon_mode) { /* Daemonize */ int fd; /* Become child of init */ pid = fork(); if (pid < 0) { exit(1); } if (pid > 0) { exit(0); } /* Override the umask */ umask(0); /* Start a new session */ sid = setsid(); if (sid < 0) { exit(1); } /* Change working directory to something that surely exists */ if (chdir("/") < 0) { exit(1); } /* re-direct standard file descriptors to /dev/null */ fd = open("/dev/null", O_RDWR); if(fd<0) { close(0); close(1); close(2); } else { if(dup2(fd, STDIN_FILENO)<0) close(0); if(dup2(fd, STDOUT_FILENO)<0) close(1); if(dup2(fd, STDERR_FILENO)<0) close(2); close(fd); } util_set_restricted_umask(); /* We are now daemonized */ } #ifndef STERC_SW_VARIANT_ANDROID /* Open any logs here */ openlog(argv[0], LOG_CONS | LOG_PID, LOG_DAEMON); #endif //STERC_SW_VARIANT_ANDROID /* Handle some signals */ signal(SIGTERM, handle_terminate); signal(SIGHUP, handle_hup); /*Handle SIGPIPE to Avoid Crash*/ if (signal((int)SIGPIPE, SIG_IGN) == SIG_ERR) { STERC_DBG_TRACE( LOG_ERR, "\n%d: SIG_ERR when ignoring SIGPIPE", __LINE__ ); abort(); } if (!daemon_mode) signal(SIGINT, handle_terminate); sterc_init(NULL, &stercd_log_func); mpl_config_init(NULL, &stercd_log_func, &stercd_config, STERC_PARAM_SET_ID); if(0!=mpl_config_read_config(config_path, &stercd_config, STERC_PARAM_SET_ID)) STERC_DBG_TRACE(LOG_WARNING, "Can't open config file %s\n",config_path); if (sterc_runscript_path_connect == NULL) STERC_DBG_TRACE(LOG_WARNING, "sterc_runscript_path_connect is NULL!\n"); if (sterc_runscript_path_disconnect == NULL) STERC_DBG_TRACE(LOG_WARNING, "sterc_runscript_path_disconnect is NULL!\n"); /* if debug mode is not enabled at startup, check if it is enabled in config */ if (0 == debug_mode) { param_elem_p = mpl_config_get_para(sterc_paramid_debug_mode, &stercd_config); if ((NULL != param_elem_p) && (NULL != param_elem_p->value_p) && *((bool*) param_elem_p->value_p)) { debug_mode = 1; } } if (0 != debug_mode) { STERC_DBG_TRACE(LOG_INFO, "Debug mode is enabled\n"); } if (transport_type == tt_unix) { memset(&ctrl_addr_un, 0, sizeof(ctrl_addr_un)); ctrl_addr_un.sun_family = AF_UNIX; strncpy(ctrl_addr_un.sun_path, STERC_CTRL_PATH, UNIX_PATH_MAX) ; unlink(ctrl_addr_un.sun_path); ctrl_handle_p = stec_opensrv((struct sockaddr*) &ctrl_addr_un, sizeof(ctrl_addr_un)); if (ctrl_handle_p == NULL) { STERC_DBG_TRACE( LOG_ERR, "\n%d: stec_opensrv failed\n", __LINE__ ); close_handles(); exit(-1); } memset(&event_addr_un, 0, sizeof(event_addr_un)); event_addr_un.sun_family = AF_UNIX; strncpy(event_addr_un.sun_path, STERC_EVENT_PATH, UNIX_PATH_MAX) ; unlink(event_addr_un.sun_path); event_handle_p = stec_opensrv((struct sockaddr*) &event_addr_un, sizeof(event_addr_un)); if (event_handle_p == NULL) { STERC_DBG_TRACE( LOG_ERR, "\n%d: stec_opensrv failed\n", __LINE__ ); close_handles(); exit(-1); } } else if (transport_type == tt_ip) { memset(&ctrl_addr_in, 0, sizeof(ctrl_addr_in)); ctrl_addr_in.sin_family = AF_INET; ctrl_addr_in.sin_port = htons(STERC_CTRL_PORT); ctrl_addr_in.sin_addr.s_addr = INADDR_ANY; ctrl_handle_p = stec_opensrv((struct sockaddr*) &ctrl_addr_in, sizeof(ctrl_addr_in)); if (ctrl_handle_p == NULL) { STERC_DBG_TRACE( LOG_ERR, "\n%d: stec_opensrv failed\n", __LINE__ ); close_handles(); exit(-1); } memset(&event_addr_in, 0, sizeof(event_addr_in)); event_addr_in.sin_family = AF_INET; event_addr_in.sin_port = htons(STERC_EVENT_PORT); event_addr_in.sin_addr.s_addr = INADDR_ANY; event_handle_p = stec_opensrv((struct sockaddr*) &event_addr_in, sizeof(event_addr_in)); if (event_handle_p == NULL) { STERC_DBG_TRACE( LOG_ERR, "\nstec_opensrv failed\n"); close_handles(); exit(-1); } } /* initialise pscc, if pscc has not started yet wait one second and try again */ while (pscc_reconnect_tries>0) { if((pscc_event_fd = sterc_pscc_init(pscc_transport_type,pscc_ctrl_p,pscc_event_p)) < 0) { STERC_DBG_TRACE( LOG_WARNING, "sterc_pscc_init failed, %d retries left.\n", pscc_reconnect_tries); pscc_reconnect_tries--; sleep(1); } else break; } /* failed to initialize pscc */ if (pscc_event_fd < 0) { STERC_DBG_TRACE( LOG_ERR, "failed to initialize pscc from sterc"); close_handles(); exit(-1); } do { FD_ZERO(&rfds); nfds = 0; FD_SET(stec_get_fd(ctrl_handle_p), &rfds); nfds = stec_get_fd(ctrl_handle_p) > nfds ? stec_get_fd(ctrl_handle_p) : nfds; FD_SET( stec_get_fd(event_handle_p), &rfds ); nfds = stec_get_fd(event_handle_p) > nfds ? stec_get_fd(event_handle_p) : nfds; FD_SET( pscc_event_fd, &rfds ); nfds = pscc_event_fd > nfds ? pscc_event_fd : nfds; FD_SET(sterclog_fd, &rfds); nfds = sterclog_fd > nfds ? sterclog_fd : nfds; #ifdef STERC_MODULE_TEST_ENABLED if(!daemon_mode) FD_SET( 0, &rfds ); #endif //STERC_MODULE_TEST_ENABLED nfds += 1; result = select(nfds, &rfds, NULL, NULL, 0); if (result < 0) { if (errno == EINTR) { STERC_DBG_TRACE(LOG_INFO, "select returned because of a signal\n"); continue; } else { STERC_DBG_TRACE(LOG_ERR, "select returned error: %s\n", strerror(errno)); break; } } if (FD_ISSET(stec_get_fd(ctrl_handle_p), &rfds)) { req_len = stec_recv(ctrl_handle_p, req, STERCD_BUFSIZ,-1); if (req_len < 0) { STERC_DBG_TRACE(LOG_ERR, "stec_recv: %s\n", strerror(errno)); return -1; } resp_len = STERCD_BUFSIZ; result = sterc_handler(ctrl_handle_p,req, (size_t)req_len, resp, (size_t*)&resp_len); if (result < 0) { STERC_DBG_TRACE(LOG_ERR, "sterc_handler failed\n"); /* Ignore and continue loop */ } else if (resp_len > 0) { if (stec_send(ctrl_handle_p, resp, resp_len) < 0) { STERC_DBG_TRACE(LOG_ERR, "stec_send: %s\n", strerror(errno)); } STERC_DBG_TRACE(LOG_DEBUG, "Sending response message: %s\n", resp); } } if (FD_ISSET(stec_get_fd(event_handle_p), &rfds)) { if(stec_process_recv(event_handle_p) < 0) { STERC_DBG_TRACE(LOG_ERR, "stec_process_recv failed\n"); } } if (FD_ISSET(pscc_event_fd, &rfds)) { if (sterc_pscc_handler(pscc_event_fd) < 0) { STERC_DBG_TRACE(LOG_ERR, "sterc_pscc_handler failed\n"); } } if (FD_ISSET(sterclog_fd, &rfds)) { if (sterc_log_select_callback(sterclog_fd, NULL) < 0) { STERC_DBG_TRACE(LOG_ERR,"pscc_log_select_callback failed\n"); } } #ifdef STERC_MODULE_TEST_ENABLED if (!daemon_mode && FD_ISSET(0, &rfds)) { read(0,req, STERCD_BUFSIZ); if(0 == strncmp(req,"quit",strlen("quit"))) break; if(0 == strncmp(req,"sync",strlen("sync"))) printf("stercd: sync\n"); } #endif //STERC_MODULE_TEST_ENABLED } while(!err); close_handles(); return(err); }
/** * handle_terminate() **/ static void handle_terminate(int signo) { STERC_DBG_TRACE(LOG_INFO, "Terminated by signal %d.\n", signo); close_handles(); exit(0); }
void IOWorker::maybe_notify_closed() { if (pools_.empty()) { session_->notify_worker_closed_async(); close_handles(); } }
int dump_db3_error(sqlite3 *db, unsigned int statflag) { if (statflag > 0) printf("err\n"); fprintf(stderr, "DB3 error: %s\n", sqlite3_errmsg(db)); close_handles(); return 1; }
~sc_ugen_factory(void) { close_handles(); }
/** * Run commands by creating an annonymous pipe (cmd1 | cmd2) */ static bool do_on_pipe(command_t *s, int level, command_t *father, simple_command_t *queue[100], unsigned int *queue_len) { SECURITY_ATTRIBUTES sa; STARTUPINFO siStartupInfo; PROCESS_INFORMATION piProcessInfo; HANDLE hOutFile = INVALID_HANDLE_VALUE; HANDLE hInFile = INVALID_HANDLE_VALUE; HANDLE hErrFile = INVALID_HANDLE_VALUE; BOOL changed = FALSE; unsigned int i; HANDLE fdes[30][2]; if(!s) return true; if(s->scmd){ queue[*queue_len] = s->scmd; (*queue_len)++; return true; } if(s->cmd1 != NULL){ do_on_pipe(s->cmd1, level+1, s, queue, queue_len); } if(s->cmd2 != NULL){ do_on_pipe(s->cmd2, level+1, s, queue, queue_len); } if(s->up == NULL || s->up->op != OP_PIPE){ //initializare atribute securitate ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; ZeroMemory(&siStartupInfo, sizeof(siStartupInfo)); ZeroMemory(&piProcessInfo, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); //creare n-1 pipe-uri for(i = 0; i < (*queue_len) - 1 ; i++){ changed = CreatePipe(&fdes[i][0], &fdes[i][1], &sa, 11* 1024 *1024); } redirrect_command(queue[0], &siStartupInfo, &hInFile, &hOutFile, &hErrFile); RedirectHandle(&siStartupInfo, fdes[0][1], STD_OUTPUT_HANDLE); exec_simple_proc(queue[0], hErrFile, sa, siStartupInfo, piProcessInfo); close_handles(hInFile, hOutFile, hErrFile); CloseHandle(fdes[0][1]); for(i = 1; i < (*queue_len) - 1; ++i){ redirrect_command(queue[0], &siStartupInfo, &hInFile, &hOutFile, &hErrFile); RedirectHandle(&siStartupInfo, fdes[i][1], STD_OUTPUT_HANDLE); RedirectHandle(&siStartupInfo, fdes[i-1][0], STD_INPUT_HANDLE); exec_simple_proc(queue[i], hErrFile, sa, siStartupInfo, piProcessInfo); close_handles(hInFile, hOutFile, hErrFile); CloseHandle(fdes[i][1]); CloseHandle(fdes[i-1][0]); } redirrect_command(queue[(*queue_len)-1], &siStartupInfo, &hInFile, &hOutFile, &hErrFile); RedirectHandle(&siStartupInfo, fdes[(*queue_len)-2][0], STD_INPUT_HANDLE); exec_simple_proc(queue[(*queue_len)-1], hErrFile, sa, siStartupInfo, piProcessInfo); close_handles(hInFile, hOutFile, hErrFile); CloseHandle(fdes[(*queue_len)-2][0]); } return true; /* TODO replace with actual exit status */ }
int main(int argc, char *argv[]) { int BUF_FRAMES = 44100 * 600; GCA_T gca; SF_INFO sinfo; sf_count_t st_frame, ed_frame, cnt_frames; sf_count_t frames; int datasize; int optchar; int mode_info = 0; opterr = 0; memset(&gca, 0, sizeof(GCA_T)); while((optchar = getopt(argc, argv, "b:e:o:ih")) != -1) { switch(optchar) { case 'b': case 'e': if(check_frame_format(optarg) != 0) { fprintf(stderr, "\"%s\" is invalid format for -%c", optarg, optchar); exit(EXIT_FAILURE); } else { if (optchar == 'b') { gca.st = strdup(optarg); } else { gca.ed = strdup(optarg); } } break; case 'o': gca.fname = strdup(optarg); break; case 'i': mode_info = 1; break; case 'h': case '?': default: disp_help(); exit(EXIT_SUCCESS); break; } } if (argv[optind] == NULL) { fprintf(stderr, "File is not specified.\n"); exit(EXIT_FAILURE); } // open input sound file gca.sf = sf_open(argv[optind], SFM_READ, &sinfo); // decode frame st_frame = str2frame(gca.st, sinfo.samplerate); ed_frame = str2frame(gca.ed, sinfo.samplerate); cnt_frames = ed_frame - st_frame; if (cnt_frames < 0) { fprintf(stderr, "Begin frame is later than end.\n"); close_handles(&gca); exit(EXIT_FAILURE); } if (mode_info == 1) { disp_info(&sinfo); printf("Specified count of frames: %d\n", (int)cnt_frames); close_handles(&gca); exit(EXIT_SUCCESS); } // get buffer datasize = sizeof(short) * sinfo.channels * BUF_FRAMES; gca.audiodata = (short *)malloc(datasize); // seek frames sf_seek(gca.sf, st_frame, SEEK_SET); // open output sndfile if (gca.fname != NULL) { gca.fp = fopen(gca.fname, "w"); if (gca.fp == NULL) { fprintf(stderr, "%s\n", strerror(errno)); close_handles(&gca); exit(EXIT_FAILURE); } } else { gca.fp = stdout; } // read frame while(cnt_frames > 0) { frames = sf_readf_short(gca.sf, gca.audiodata, BUF_FRAMES); if (frames > cnt_frames) { fwrite(gca.audiodata, sizeof(short), cnt_frames * sinfo.channels, gca.fp); } else { fwrite(gca.audiodata, sizeof(short), frames * sinfo.channels, gca.fp); } cnt_frames -= frames; } close_handles(&gca); return 0; }
void Session::on_event(const SessionEvent& event) { switch (event.type) { case SessionEvent::CONNECT: { int port = config_.port(); const ContactPointList& contact_points = config_.contact_points(); for (ContactPointList::const_iterator it = contact_points.begin(), end = contact_points.end(); it != end; ++it) { const std::string& seed = *it; Address address; if (Address::from_string(seed, port, &address)) { add_host(address); } else { pending_resolve_count_++; Resolver::resolve(loop(), seed, port, this, on_resolve); } } if (pending_resolve_count_ == 0) { internal_connect(); } break; } case SessionEvent::NOTIFY_READY: if (pending_pool_count_ > 0) { if (--pending_pool_count_ == 0) { LOG_DEBUG("Session is connected"); notify_connected(); } LOG_DEBUG("Session pending pool count %d", pending_pool_count_); } break; case SessionEvent::NOTIFY_KEYSPACE_ERROR: { // Currently, this is only called when the keyspace does not exist // and not for any other keyspace related errors. const CopyOnWritePtr<std::string> keyspace(keyspace_); notify_connect_error(CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE, "Keyspace '" + *keyspace + "' does not exist"); break; } case SessionEvent::NOTIFY_WORKER_CLOSED: if (--pending_workers_count_ == 0) { LOG_DEBUG("Session is disconnected"); control_connection_.close(); close_handles(); } break; case SessionEvent::NOTIFY_UP: control_connection_.on_up(event.address); break; case SessionEvent::NOTIFY_DOWN: control_connection_.on_down(event.address); break; default: assert(false); break; } }
void Session::on_event(const SessionEvent& event) { switch (event.type) { case SessionEvent::CONNECT: { int port = config_.port(); // This needs to be done on the session thread because it could pause // generating a new random seed. if (config_.use_randomized_contact_points()) { random_.reset(new Random()); } MultiResolver<Session*>::Ptr resolver( new MultiResolver<Session*>(this, on_resolve, #if UV_VERSION_MAJOR >= 1 on_resolve_name, #endif on_resolve_done)); const ContactPointList& contact_points = config_.contact_points(); for (ContactPointList::const_iterator it = contact_points.begin(), end = contact_points.end(); it != end; ++it) { const std::string& seed = *it; Address address; if (Address::from_string(seed, port, &address)) { #if UV_VERSION_MAJOR >= 1 if (config_.use_hostname_resolution()) { resolver->resolve_name(loop(), address, config_.resolve_timeout_ms()); } else { #endif add_host(address); #if UV_VERSION_MAJOR >= 1 } #endif } else { resolver->resolve(loop(), seed, port, config_.resolve_timeout_ms()); } } break; } case SessionEvent::NOTIFY_READY: if (pending_pool_count_ > 0) { if (--pending_pool_count_ == 0) { LOG_DEBUG("Session is connected"); notify_connected(); } LOG_DEBUG("Session pending pool count %d", pending_pool_count_); } break; case SessionEvent::NOTIFY_KEYSPACE_ERROR: { // Currently, this is only called when the keyspace does not exist // and not for any other keyspace related errors. notify_connect_error(CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE, "Keyspace '" + keyspace() + "' does not exist"); break; } case SessionEvent::NOTIFY_WORKER_CLOSED: if (--pending_workers_count_ == 0) { LOG_DEBUG("Session is disconnected"); control_connection_.close(); close_handles(); } break; case SessionEvent::NOTIFY_UP: control_connection_.on_up(event.address); break; case SessionEvent::NOTIFY_DOWN: control_connection_.on_down(event.address); break; default: assert(false); break; } }