/** * tds_read_config_info() will fill the tds connect_info structure based on configuration * information gathered in the following order: * 1) Program specified in TDSLOGIN structure * 2) The environment variables TDSVER, TDSDUMP, TDSPORT, TDSQUERY, TDSHOST * 3) A config file with the following search order: * a) a readable file specified by environment variable FREETDSCONF * b) a readable file in ~/.freetds.conf * c) a readable file in $prefix/etc/freetds.conf * 3) ~/.interfaces if exists * 4) $SYBASE/interfaces if exists * 5) TDS_DEF_* default values * * .tdsrc and freetds.conf have been added to make the package easier to * integration with various Linux and *BSD distributions. */ TDSCONNECTINFO * tds_read_config_info(TDSSOCKET * tds, TDSLOGIN * login, TDSLOCALE * locale) { TDSCONNECTINFO *connect_info; char *s; char *path; pid_t pid; int opened = 0; /* allocate a new structure with hard coded and build-time defaults */ connect_info = tds_alloc_connect(locale); if (!connect_info) return NULL; s = getenv("TDSDUMPCONFIG"); if (s) { if (*s) { opened = tdsdump_open(s); } else { pid = getpid(); if (asprintf(&path, "/tmp/tdsconfig.log.%d", pid) >= 0) { if (*path) { opened = tdsdump_open(path); } free(path); } } } tdsdump_log(TDS_DBG_INFO1, "%L Attempting to read conf files.\n"); if (!tds_read_conf_file(connect_info, tds_dstr_cstr(&login->server_name))) { /* fallback to interfaces file */ tdsdump_log(TDS_DBG_INFO1, "%L Failed in reading conf file. Trying interface files.\n"); tds_read_interfaces(tds_dstr_cstr(&login->server_name), connect_info); } if (parse_server_name_for_port(connect_info, login)) { tdsdump_log(TDS_DBG_INFO1, "%L Parsed servername, now %s on %d.\n", connect_info->server_name, login->port); } tds_fix_connect(connect_info); /* And finally the login structure */ tds_config_login(connect_info, login); if (opened) { tdsdump_close(); } return connect_info; }
static bool pool_open_logfile(TDS_POOL *pool) { int fd; tds_g_append_mode = 0; tdsdump_open(getenv("TDSDUMP")); if (!logfile_name) return true; fd = open(logfile_name, O_WRONLY|O_CREAT|O_APPEND, 0644); if (fd < 0) return false; fflush(stdout); fflush(stderr); while (dup2(fd, fileno(stdout)) < 0 && errno == EINTR) continue; while (dup2(fd, fileno(stderr)) < 0 && errno == EINTR) continue; close(fd); fflush(stdout); fflush(stderr); return true; }
int main(int argc, char **argv) { TDS_POOL *pool; signal(SIGTERM, term_handler); signal(SIGINT, term_handler); signal(SIGPIPE, SIG_IGN); if (argc < 2) { fprintf(stderr, "Usage: tdspool <pool name>\n"); return EXIT_FAILURE; } pool = pool_init(argv[1]); tdsdump_open(getenv("TDSDUMP")); pool_main_loop(pool); printf("tdspool Shutdown\n"); return EXIT_SUCCESS; }
/** * tds_read_config_info() will fill the tds connection structure based on configuration * information gathered in the following order: * 1) Program specified in TDSLOGIN structure * 2) The environment variables TDSVER, TDSDUMP, TDSPORT, TDSQUERY, TDSHOST * 3) A config file with the following search order: * a) a readable file specified by environment variable FREETDSCONF * b) a readable file in ~/.freetds.conf * c) a readable file in $prefix/etc/freetds.conf * 3) ~/.interfaces if exists * 4) $SYBASE/interfaces if exists * 5) TDS_DEF_* default values * * .tdsrc and freetds.conf have been added to make the package easier to * integration with various Linux and *BSD distributions. */ TDSLOGIN * tds_read_config_info(TDSSOCKET * tds, TDSLOGIN * login, TDSLOCALE * locale) { TDSLOGIN *connection; char *s; char *path; pid_t pid; int opened = 0, found; struct addrinfo *addrs; /* allocate a new structure with hard coded and build-time defaults */ connection = tds_alloc_login(0); if (!connection || !tds_init_login(connection, locale)) { tds_free_login(connection); return NULL; } s = getenv("TDSDUMPCONFIG"); if (s) { if (*s) { opened = tdsdump_open(s); } else { pid = getpid(); if (asprintf(&path, pid_config_logpath, pid) >= 0) { if (*path) { opened = tdsdump_open(path); } free(path); } } } tdsdump_log(TDS_DBG_INFO1, "Getting connection information for [%s].\n", tds_dstr_cstr(&login->server_name)); /* (The server name is set in login.c.) */ /* Read the config files. */ tdsdump_log(TDS_DBG_INFO1, "Attempting to read conf files.\n"); found = tds_read_conf_file(connection, tds_dstr_cstr(&login->server_name)); if (!found) { if (parse_server_name_for_port(connection, login)) { found = tds_read_conf_file(connection, tds_dstr_cstr(&connection->server_name)); /* do it again to really override what found in freetds.conf */ if (found) { parse_server_name_for_port(connection, login); } else if (TDS_SUCCEED(tds_lookup_host_set(tds_dstr_cstr(&connection->server_name), &connection->ip_addrs))) { if (!tds_dstr_dup(&connection->server_host_name, &connection->server_name)) { tds_free_login(connection); return NULL; } found = 1; } } } if (!found) { /* fallback to interfaces file */ tdsdump_log(TDS_DBG_INFO1, "Failed in reading conf file. Trying interface files.\n"); if (!tds_read_interfaces(tds_dstr_cstr(&login->server_name), connection)) { tdsdump_log(TDS_DBG_INFO1, "Failed to find [%s] in configuration files; trying '%s' instead.\n", tds_dstr_cstr(&login->server_name), tds_dstr_cstr(&connection->server_name)); if (connection->ip_addrs == NULL) tdserror(tds_get_ctx(tds), tds, TDSEINTF, 0); } } /* Override config file settings with environment variables. */ tds_fix_login(connection); /* And finally apply anything from the login structure */ if (!tds_config_login(connection, login)) { tds_free_login(connection); return NULL; } if (opened) { char tmp[128]; tdsdump_log(TDS_DBG_INFO1, "Final connection parameters:\n"); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "server_name", tds_dstr_cstr(&connection->server_name)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "server_host_name", tds_dstr_cstr(&connection->server_host_name)); for (addrs = connection->ip_addrs; addrs != NULL; addrs = addrs->ai_next) tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "ip_addr", tds_addrinfo2str(addrs, tmp, sizeof(tmp))); if (connection->ip_addrs == NULL) tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "ip_addr", ""); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "instance_name", tds_dstr_cstr(&connection->instance_name)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "port", connection->port); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "major_version", TDS_MAJOR(connection)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "minor_version", TDS_MINOR(connection)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "block_size", connection->block_size); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "language", tds_dstr_cstr(&connection->language)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "server_charset", tds_dstr_cstr(&connection->server_charset)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "connect_timeout", connection->connect_timeout); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "client_host_name", tds_dstr_cstr(&connection->client_host_name)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "client_charset", tds_dstr_cstr(&connection->client_charset)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "use_utf16", connection->use_utf16); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "app_name", tds_dstr_cstr(&connection->app_name)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "user_name", tds_dstr_cstr(&connection->user_name)); /* tdsdump_log(TDS_DBG_PASSWD, "\t%20s = %s\n", "password", tds_dstr_cstr(&connection->password)); (no such flag yet) */ tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "library", tds_dstr_cstr(&connection->library)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "bulk_copy", (int)connection->bulk_copy); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "suppress_language", (int)connection->suppress_language); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "encrypt level", (int)connection->encryption_level); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "query_timeout", connection->query_timeout); /* tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "capabilities", tds_dstr_cstr(&connection->capabilities)); (not null terminated) */ tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "database", tds_dstr_cstr(&connection->database)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "dump_file", tds_dstr_cstr(&connection->dump_file)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %x\n", "debug_flags", connection->debug_flags); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "text_size", connection->text_size); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "emul_little_endian", connection->emul_little_endian); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "server_realm_name", tds_dstr_cstr(&connection->server_realm_name)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "server_spn", tds_dstr_cstr(&connection->server_spn)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "cafile", tds_dstr_cstr(&connection->cafile)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "crlfile", tds_dstr_cstr(&connection->crlfile)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "check_ssl_hostname", connection->check_ssl_hostname); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %s\n", "db_filename", tds_dstr_cstr(&connection->db_filename)); tdsdump_log(TDS_DBG_INFO1, "\t%20s = %d\n", "readonly_intent", connection->readonly_intent); tdsdump_close(); } /* * If a dump file has been specified, start logging */ if (!tds_dstr_isempty(&connection->dump_file) && !tdsdump_isopen()) { if (connection->debug_flags) tds_debug_flags = connection->debug_flags; tdsdump_open(tds_dstr_cstr(&connection->dump_file)); } return connection; }
static int process_parameters(int argc, char **argv, BCPPARAMDATA * pdata) { int state; int i; char arg[FILENAME_MAX + 1]; char connection[256]; char owner[256]; char *tok; /* set some defaults */ pdata->batchsize = 1000; /* get the rest of the arguments */ state = GET_NEXTARG; for (i = 1; i < argc; i++) { strcpy(arg, argv[i]); switch (state) { case GET_NEXTARG: if (arg[0] != '-') return FALSE; switch (arg[1]) { case 'b': pdata->bflag++; if (strlen(arg) > 2) pdata->batchsize = atoi(&arg[2]); else state = GET_BATCHSIZE; break; case 'p': pdata->pflag++; if (strlen(arg) > 2) pdata->packetsize = atoi(&arg[2]); else state = GET_PACKETSIZE; break; case 't': pdata->tflag++; break; case 'a': pdata->aflag++; break; case 'c': pdata->cflag++; if (strlen(arg) > 2) { strcpy(owner, &arg[2]); pdata->owner = strdup(owner); } else state = GET_OWNER; break; case 'd': tdsdump_open(NULL); break; case 'S': pdata->Sflag++; if (strlen(arg) > 2) { strcpy(connection, &arg[2]); tok = strtok(connection, "/"); if (!tok) return FALSE; pdata->sserver = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->suser = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->spass = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->sdb = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->sdbobject = strdup(tok); } else state = GET_SOURCE; break; case 'D': pdata->Dflag++; if (strlen(arg) > 2) { strcpy(connection, &arg[2]); tok = strtok(connection, "/"); if (!tok) return FALSE; pdata->dserver = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->duser = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->dpass = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->ddb = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->ddbobject = strdup(tok); } else state = GET_DEST; break; case 'v': pdata->vflag++; break; default: return FALSE; } break; case GET_BATCHSIZE: pdata->batchsize = atoi(arg); state = GET_NEXTARG; break; case GET_PACKETSIZE: pdata->packetsize = atoi(arg); state = GET_NEXTARG; break; case GET_OWNER: if (arg[0] == '-') { fprintf(stderr, "If -c is specified an owner for the table must be provided.\n"); return FALSE; } strcpy(owner, arg); pdata->owner = strdup(owner); state = GET_NEXTARG; break; case GET_SOURCE: strcpy(connection, arg); tok = strtok(connection, "/"); if (!tok) return FALSE; pdata->sserver = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->suser = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->spass = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->sdb = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->sdbobject = strdup(tok); state = GET_NEXTARG; break; case GET_DEST: strcpy(connection, arg); tok = strtok(connection, "/"); if (!tok) return FALSE; pdata->dserver = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->duser = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->dpass = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->ddb = strdup(tok); tok = strtok(NULL, "/"); if (!tok) return FALSE; pdata->ddbobject = strdup(tok); state = GET_NEXTARG; break; default: break; } } /* one of these must be specified */ if ((pdata->tflag + pdata->aflag + pdata->cflag) != 1) { fprintf(stderr, "one (and only one) of -t, -a or -c must be specified\n"); return FALSE; } if (!pdata->Sflag) { printf("\nNo [-S]ource information supplied.\n\n"); printf("Enter Server : "); pdata->sserver = gets_alloc(); printf("Enter Login : "******"Enter Password : "******"Enter Database : "); pdata->sdb = gets_alloc(); printf("Enter Table : "); pdata->sdbobject = gets_alloc(); } if (!pdata->Dflag) { printf("\nNo [-D]estination information supplied.\n\n"); printf("Enter Server : "); pdata->dserver = gets_alloc(); printf("Enter Login : "******"Enter Password : "******"Enter Database : "); pdata->ddb = gets_alloc(); printf("Enter Table : "); pdata->ddbobject = gets_alloc(); } return TRUE; }
static int process_parameters(int argc, char **argv, BCPPARAMDATA * pdata) { int opt; /* set some defaults */ pdata->batchsize = 1000; /* get the rest of the arguments */ while ((opt = getopt(argc, argv, "b:p:tacdS:D:v")) != -1) { switch (opt) { case 'b': pdata->bflag++; pdata->batchsize = atoi(optarg); break; case 'p': pdata->pflag++; pdata->packetsize = atoi(optarg); break; case 't': pdata->tflag++; break; case 'a': pdata->aflag++; break; case 'c': pdata->cflag++; if (optarg[0] == '-') { fprintf(stderr, "Invalid owner specified.\n"); return FALSE; } pdata->owner = strdup(optarg); break; case 'd': tdsdump_open(NULL); break; case 'S': pdata->Sflag++; if (process_objectinfo(&pdata->src, optarg, "Enter Source Password: "******"Enter Destination Password: "******"one (and only one) of -t, -a or -c must be specified\n"); return FALSE; } if (!pdata->Sflag) { printf("\nNo [-S]ource information supplied.\n\n"); printf("Enter Server : "); pdata->src.server = gets_alloc(); printf("Enter Login : "******"Enter Password : "******"Enter Database : "); pdata->src.db = gets_alloc(); printf("Enter Table : "); pdata->src.dbobject = gets_alloc(); } if (!pdata->Dflag) { printf("\nNo [-D]estination information supplied.\n\n"); printf("Enter Server : "); pdata->dest.server = gets_alloc(); printf("Enter Login : "******"Enter Password : "******"Enter Database : "); pdata->dest.db = gets_alloc(); printf("Enter Table : "); pdata->dest.dbobject = gets_alloc(); } return TRUE; }