FreeTDS API
pool.h
1 /* TDSPool - Connection pooling for TDS based databases
2  * Copyright (C) 2001 Brian Bruns
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 #ifndef _pool_h_
21 #define _pool_h_
22 
23 #include <assert.h>
24 
25 #if HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28 
29 #if HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32 
33 /*
34  * POSIX says fd_set type may be defined in either sys/select.h or sys/time.h.
35  */
36 #if HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39 
40 #include <freetds/tds.h>
41 #include <freetds/utils/dlist.h>
42 
43 /* defines */
44 #define PGSIZ 2048
45 #define BLOCKSIZ 512
46 #define MAX_POOL_USERS 1024
47 
48 /* enums and typedefs */
49 typedef enum
50 {
51  TDS_SRV_WAIT, /* if no members are free wait */
52  TDS_SRV_QUERY,
53 } TDS_USER_STATE;
54 
55 /* forward declaration */
56 typedef struct tds_pool_event TDS_POOL_EVENT;
57 typedef struct tds_pool_socket TDS_POOL_SOCKET;
58 typedef struct tds_pool_member TDS_POOL_MEMBER;
59 typedef struct tds_pool_user TDS_POOL_USER;
60 typedef struct tds_pool TDS_POOL;
61 typedef void (*TDS_POOL_EXECUTE)(TDS_POOL_EVENT *event);
62 
64 {
65  TDS_POOL_EVENT *next;
66  TDS_POOL_EXECUTE execute;
67 };
68 
70 {
71  TDSSOCKET *tds;
72  bool poll_recv;
73  bool poll_send;
74 };
75 
77 {
78  TDS_POOL_SOCKET sock;
79  DLIST_FIELDS(dlist_user_item);
80  TDSLOGIN *login;
81  TDS_USER_STATE user_state;
82  TDS_POOL_MEMBER *assigned_member;
83 };
84 
86 {
87  TDS_POOL_SOCKET sock;
88  DLIST_FIELDS(dlist_member_item);
89  bool doing_async;
90  time_t last_used_tm;
91  TDS_POOL_USER *current_user;
92 };
93 
94 #define DLIST_PREFIX dlist_member
95 #define DLIST_LIST_TYPE dlist_members
96 #define DLIST_ITEM_TYPE TDS_POOL_MEMBER
97 #include <freetds/utils/dlist.tmpl.h>
98 
99 #define DLIST_PREFIX dlist_user
100 #define DLIST_LIST_TYPE dlist_users
101 #define DLIST_ITEM_TYPE TDS_POOL_USER
102 #include <freetds/utils/dlist.tmpl.h>
103 
104 struct tds_pool
105 {
106  char *name;
107  char *user;
108  char *password;
109  char *server;
110  char *database;
111  char *server_user;
112  char *server_password;
113  int port;
114  int max_member_age; /* in seconds */
115  int min_open_conn;
116  int max_open_conn;
117  tds_mutex events_mtx;
118  TDS_SYS_SOCKET listen_fd;
119  TDS_SYS_SOCKET wakeup_fd;
120  TDS_SYS_SOCKET event_fd;
121  TDS_POOL_EVENT *events;
122 
123  int num_active_members;
124  dlist_members active_members;
125  dlist_members idle_members;
126 
128  dlist_users waiters;
129  int num_users;
130  dlist_users users;
131  TDSCONTEXT *ctx;
132 
133  unsigned long user_logins;
134  unsigned long member_logins;
135 };
136 
137 /* prototypes */
138 
139 /* member.c */
140 void pool_process_members(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
141 TDS_POOL_MEMBER *pool_assign_idle_member(TDS_POOL * pool, TDS_POOL_USER *user);
142 void pool_mbr_init(TDS_POOL * pool);
143 void pool_mbr_destroy(TDS_POOL * pool);
144 void pool_free_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
145 void pool_assign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr, TDS_POOL_USER *puser);
146 void pool_deassign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
147 void pool_reset_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
148 bool pool_packet_read(TDSSOCKET * tds);
149 
150 /* user.c */
151 void pool_process_users(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
152 void pool_user_init(TDS_POOL * pool);
153 void pool_user_destroy(TDS_POOL * pool);
154 TDS_POOL_USER *pool_user_create(TDS_POOL * pool, TDS_SYS_SOCKET s);
155 void pool_free_user(TDS_POOL * pool, TDS_POOL_USER * puser);
156 void pool_user_query(TDS_POOL * pool, TDS_POOL_USER * puser);
157 bool pool_user_send_login_ack(TDS_POOL * pool, TDS_POOL_USER * puser);
158 void pool_user_finish_login(TDS_POOL * pool, TDS_POOL_USER * puser);
159 
160 /* util.c */
161 void dump_login(TDSLOGIN * login);
162 void pool_event_add(TDS_POOL *pool, TDS_POOL_EVENT *ev, TDS_POOL_EXECUTE execute);
163 int pool_write(TDS_SYS_SOCKET sock, const void *buf, size_t len);
164 bool pool_write_data(TDS_POOL_SOCKET *from, TDS_POOL_SOCKET *to);
165 
166 /* config.c */
167 bool pool_read_conf_files(const char *path, const char *poolname, TDS_POOL * pool, char **err);
168 
169 
170 #endif
tds_login::new_password
DSTR new_password
new password to set (TDS 7.2+)
Definition: tds.h:531
tds_login::user_name
DSTR user_name
account for login
Definition: tds.h:529
tds.h
tds_login::ip_addrs
struct addrinfo * ip_addrs
ip(s) of server
Definition: tds.h:541
tds_read_config_info
TDSLOGIN * tds_read_config_info(TDSSOCKET *tds, TDSLOGIN *login, TDSLOCALE *locale)
tds_read_config_info() will fill the tds connection structure based on configuration information gath...
Definition: config.c:138
tds_read_conf_section
bool tds_read_conf_section(FILE *in, const char *section, TDSCONFPARSE tds_conf_parse, void *param)
Read a section of configuration file (INI style file)
Definition: config.c:504
tds_login::server_charset
DSTR server_charset
charset of server e.g.
Definition: tds.h:518
tds_get_home_file
char * tds_get_home_file(const char *file)
Return filename from HOME directory.
Definition: config.c:326
tds_fix_login
void tds_fix_login(TDSLOGIN *login)
Fix configuration after reading it.
Definition: config.c:286
CONNECT_EVENT
Definition: member.c:392
DSNINFO
Definition: winsetup.c:62
tdsdump_log
void tdsdump_log(const char *file, unsigned int level_line, const char *fmt,...)
Write a message to the debug log.
Definition: log.c:354
tds_login::db_filename
DSTR db_filename
database filename to attach (MSSQL)
Definition: tds.h:524
DSNINFO::dsn
DSTR dsn
edited name of the data source
Definition: winsetup.c:65
tds_login::crlfile
DSTR crlfile
certificate revocation file
Definition: tds.h:526
tds_set_server
bool tds_set_server(TDSLOGIN *tds_login, const char *server) TDS_WUR
Set the servername in a TDSLOGIN structure.
Definition: login.c:125
tds_start_query
void tds_start_query(TDSSOCKET *tds, unsigned char packet_type)
Start query packet of a given type.
Definition: query.c:345
tds_dstr_set
DSTR * tds_dstr_set(DSTR *s, char *src)
set a string from another buffer.
Definition: tdsstring.c:107
tds_dstr_init
static void tds_dstr_init(DSTR *s)
init a string with empty
Definition: string.h:53
tds_login::bulk_copy
unsigned int bulk_copy
if bulk copy should be enabled
Definition: tds.h:551
tds_flush_packet
TDSRET tds_flush_packet(TDSSOCKET *tds)
Flush packet to server.
Definition: write.c:285
tds_process_simple_query
TDSRET tds_process_simple_query(TDSSOCKET *tds)
Process results for simple query as "SET TEXTSIZE" or "USE dbname" If the statement returns results,...
Definition: token.c:876
select_info
Definition: main.c:204
tds_socket::in_cancel
volatile unsigned char in_cancel
indicate we are waiting a cancel reply; discard tokens till acknowledge; 1 mean we have to send cance...
Definition: tds.h:1206
TDS_PENDING
@ TDS_PENDING
cilent is waiting for data
Definition: tds.h:794
tds_set_state
TDS_STATE tds_set_state(TDSSOCKET *tds, TDS_STATE state)
Set state of TDS connection, with logging and checking.
Definition: util.c:58
tds_dstr_free
void tds_dstr_free(DSTR *s)
free string
Definition: tdsstring.c:62
tds_socket::in_len
unsigned in_len
input buffer length
Definition: tds.h:1170
tds_dstr_isempty
static int tds_dstr_isempty(const DSTR *s)
test if string is empty
Definition: string.h:60
tds_login::cafile
DSTR cafile
certificate authorities file
Definition: tds.h:525
tds_config_verstr
TDS_USMALLINT * tds_config_verstr(const char *tdsver, TDSLOGIN *login)
Set TDS version from given string.
Definition: config.c:905
tds_dstr
Structure to hold a string.
Definition: string.h:36
tds_set_interfaces_file_loc
TDSRET tds_set_interfaces_file_loc(const char *interf)
Set the full name of interface file.
Definition: config.c:948
tds_login::server_spn
DSTR server_spn
server SPN (in freetds.conf)
Definition: tds.h:523
tds_init_login
TDSLOGIN * tds_init_login(TDSLOGIN *login, TDSLOCALE *locale)
Initialize login structure with locale information and other stuff for connection.
Definition: mem.c:800
DSNINFO::origdsn
DSTR origdsn
original name of the data source
Definition: winsetup.c:64
DSNINFO::login
TDSLOGIN * login
everything else
Definition: winsetup.c:66
tds_connection::env
TDSENV env
environment is shared between all sessions
Definition: tds.h:1080
tds_dstr_empty
#define tds_dstr_empty(s)
Make a string empty.
Definition: string.h:91
tds_login::server_name
DSTR server_name
server name (in freetds.conf)
Definition: tds.h:513
tds_dstr_cstr
static const char * tds_dstr_cstr(const DSTR *s)
Returns a C version (NUL terminated string) of dstr.
Definition: string.h:78
tds_login::tds_version
TDS_USMALLINT tds_version
TDS version.
Definition: tds.h:515
tds_dstr_dup
DSTR * tds_dstr_dup(DSTR *s, const DSTR *src)
Duplicate a string from another dynamic string.
Definition: tdsstring.c:134
tdsvername_t
Definition: config.c:886
tdsdump_close
void tdsdump_close(void)
Close the TDS dump log file.
Definition: log.c:193
search_interface_file
static int search_interface_file(TDSLOGIN *login, const char *dir, const char *file, const char *host)
Open and read the file 'file' searching for a logical server by the name of 'host'.
Definition: config.c:1051
tds_dstr_copy
DSTR * tds_dstr_copy(DSTR *s, const char *src)
copy a string from another
Definition: tdsstring.c:122
tds_lookup_port
static int tds_lookup_port(const char *portname)
Given a portname lookup the port.
Definition: config.c:1013
tds_pool_user
Definition: pool.h:76
DSTR_INITIALIZER
#define DSTR_INITIALIZER
Initializer, used to initialize string like in the following example.
Definition: string.h:49
parse_server_name_for_port
static int parse_server_name_for_port(TDSLOGIN *connection, TDSLOGIN *login, bool update_server)
Check the server name to find port info first Warning: connection-> & login-> are all modified when n...
Definition: config.c:1280
tds_pool::waiters
dlist_users waiters
users in wait state
Definition: pool.h:128
tds_socket::in_pos
unsigned in_pos
current position in in_buf
Definition: tds.h:1168
poll.h
Provide poll call where missing.
tds_read_conf_file
bool tds_read_conf_file(TDSLOGIN *login, const char *server)
Read configuration info for given server return 0 on error.
Definition: config.c:347
tds_socket::in_buf
unsigned char * in_buf
Input buffer.
Definition: tds.h:1154
tds_login::port
int port
port of database service
Definition: tds.h:514
tds_read_interfaces
static int tds_read_interfaces(const char *server, TDSLOGIN *login)
Try to find the IP number and port for a (possibly) logical server name.
Definition: config.c:1174
tds_pool
Definition: pool.h:104
tds_dstr_zero
void tds_dstr_zero(DSTR *s)
clear all string filling with zeroes (mainly for security reason)
Definition: tdsstring.c:55
tds_socket::out_flag
unsigned char out_flag
output buffer type
Definition: tds.h:1172
tds_pool_socket
Definition: pool.h:69
TDS_WRITING
@ TDS_WRITING
client is writing data
Definition: tds.h:792
tdserror
int tdserror(const TDSCONTEXT *tds_ctx, TDSSOCKET *tds, int msgno, int errnum)
Call the client library's error handler (for library-generated errors only)
Definition: util.c:307
tds_socket_set_nonblocking
int tds_socket_set_nonblocking(TDS_SYS_SOCKET sock)
Set socket to non-blocking.
Definition: net.c:168
tds_login::password
DSTR password
password of account login
Definition: tds.h:530
tds_socket
Information for a server connection.
Definition: tds.h:1141
conf_params
Definition: config.c:55
tds_get_compiletime_settings
const TDS_COMPILETIME_SETTINGS * tds_get_compiletime_settings(void)
Return a structure capturing the compile-time settings provided to the configure script.
Definition: config.c:1324
tds_locale
Definition: tds.h:576
tds_put_string
int tds_put_string(TDSSOCKET *tds, const char *s, int len)
Output a string to wire automatic translate string to unicode if needed.
Definition: write.c:93
tds_close_socket
void tds_close_socket(TDSSOCKET *tds)
Close current socket.
Definition: net.c:548
tdsdump_open
int tdsdump_open(const char *filename)
Create and truncate a human readable dump file for the TDS traffic.
Definition: log.c:112
tds_login::server_realm_name
DSTR server_realm_name
server realm name (in freetds.conf)
Definition: tds.h:522
tds_lookup_host
struct addrinfo * tds_lookup_host(const char *servername)
Get the IP address for a hostname.
Definition: config.c:974
tds_compiletime_settings
A structure to hold all the compile-time settings.
Definition: tds.h:82
tds_process_cancel
TDSRET tds_process_cancel(TDSSOCKET *tds)
Definition: token.c:2550
tds_pool_member
Definition: pool.h:85
tds_context
Definition: tds.h:1029
tds_login
Definition: tds.h:511
tds_pool_event
Definition: pool.h:63
tdsdump_dump_buf
void tdsdump_dump_buf(const char *file, unsigned int level_line, const char *msg, const void *buf, size_t length)
Dump the contents of data into the log file in a human readable format.
Definition: log.c:256
tds_dstr_copyn
DSTR * tds_dstr_copyn(DSTR *s, const char *src, size_t length)
Set string to a given buffer of characters.
Definition: tdsstring.c:77
tds_env::database
char * database
database name
Definition: tds.h:971