LCOV - code coverage report
Current view: top level - src/tds - util.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 104 128 81.2 %
Date: 2025-01-18 11:50:39 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  Brian Bruns
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library 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 GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : 
      22             : #include <stdarg.h>
      23             : 
      24             : #include <freetds/time.h>
      25             : 
      26             : #include <assert.h>
      27             : #include <ctype.h>
      28             : #include <limits.h>
      29             : #include <stdio.h>
      30             : 
      31             : #if HAVE_STDLIB_H
      32             : #include <stdlib.h>
      33             : #endif /* HAVE_STDLIB_H */
      34             : 
      35             : #if HAVE_STRING_H
      36             : #include <string.h>
      37             : #endif /* HAVE_STRING_H */
      38             : 
      39             : #if HAVE_UNISTD_H
      40             : #include <unistd.h>
      41             : #endif /* HAVE_UNISTD_H */
      42             : 
      43             : #ifdef _WIN32
      44             : #include <process.h>
      45             : #endif
      46             : 
      47             : #include <freetds/tds.h>
      48             : #include <freetds/checks.h>
      49             : #include <freetds/thread.h>
      50             : 
      51             : /**
      52             :  * Set state of TDS connection, with logging and checking.
      53             :  * \param tds     state information for the socket and the TDS protocol
      54             :  * \param state   the new state of the connection, cf. TDS_STATE.
      55             :  * \return        the new state, which might not be \a state.
      56             :  */
      57             : TDS_STATE
      58      968614 : tds_set_state(TDSSOCKET * tds, TDS_STATE state)
      59             : {
      60             :         TDS_STATE prior_state;
      61             :         static const char state_names[][8] = {
      62             :                 "IDLE",
      63             :                 "WRITING",
      64             :                 "SENDING",
      65             :                 "PENDING",
      66             :                 "READING",
      67             :                 "DEAD"
      68             :         };
      69      968614 :         assert(state < TDS_VECTOR_SIZE(state_names));
      70      968614 :         assert(tds->state < TDS_VECTOR_SIZE(state_names));
      71             : 
      72      968614 :         prior_state = tds->state;
      73      968614 :         if (state == prior_state)
      74             :                 return state;
      75             : 
      76      960734 :         switch(state) {
      77      447316 :         case TDS_PENDING:
      78      447316 :                 if (prior_state == TDS_READING || prior_state == TDS_WRITING) {
      79      388348 :                         tds->state = TDS_PENDING;
      80      388348 :                         tds_mutex_unlock(&tds->wire_mtx);
      81      388348 :                         break;
      82             :                 }
      83       58976 :                 tdsdump_log(TDS_DBG_ERROR, "logic error: cannot change query state from %s to %s\n",
      84           8 :                                                 state_names[prior_state], state_names[state]);
      85             :                 break;
      86      388284 :         case TDS_READING:
      87             :                 /* transition to READING are valid only from PENDING */
      88      388284 :                 if (tds_mutex_trylock(&tds->wire_mtx))
      89           8 :                         return tds->state;
      90      388276 :                 if (tds->state != TDS_PENDING) {
      91           0 :                         tds_mutex_unlock(&tds->wire_mtx);
      92           0 :                         tdsdump_log(TDS_DBG_ERROR, "logic error: cannot change query state from %s to %s\n", 
      93           0 :                                                         state_names[prior_state], state_names[state]);
      94             :                         break;
      95             :                 }
      96      388276 :                 tds->state = state;
      97      388276 :                 break;
      98        1650 :         case TDS_SENDING:
      99        1650 :                 if (prior_state != TDS_READING && prior_state != TDS_WRITING) {
     100           0 :                         tdsdump_log(TDS_DBG_ERROR, "logic error: cannot change query state from %s to %s\n",
     101           0 :                                 state_names[prior_state], state_names[state]);
     102             :                         break;
     103             :                 }
     104        1650 :                 if (tds->state == TDS_READING) {
     105             :                         /* TODO check this code, copied from tds_submit_prepare */
     106         312 :                         tds_free_all_results(tds);
     107         312 :                         tds->rows_affected = TDS_NO_COUNT;
     108         312 :                         tds_release_cursor(&tds->cur_cursor);
     109         312 :                         tds_release_cur_dyn(tds);
     110         312 :                         tds->current_op = TDS_OP_NONE;
     111             :                 }
     112             : 
     113        1650 :                 tds_mutex_unlock(&tds->wire_mtx);
     114        1650 :                 tds->state = state;
     115        1650 :                 break;
     116       59164 :         case TDS_IDLE:
     117       59164 :                 if (prior_state == TDS_DEAD && TDS_IS_SOCKET_INVALID(tds_get_s(tds))) {
     118          16 :                         tdsdump_log(TDS_DBG_ERROR, "logic error: cannot change query state from %s to %s\n",
     119           0 :                                 state_names[prior_state], state_names[state]);
     120             :                         break;
     121             :                 }
     122             :         case TDS_DEAD:
     123       62480 :                 if (prior_state == TDS_READING || prior_state == TDS_WRITING)
     124       59238 :                         tds_mutex_unlock(&tds->wire_mtx);
     125       62480 :                 tds->state = state;
     126             : 
     127             :                 /* invalid, code should have either close or aborted all freezes */
     128       62480 :                 if (TDS_UNLIKELY(tds->frozen)) {
     129             :                         TDSFREEZE freeze;
     130             : 
     131           0 :                         tds->frozen = 1;
     132           0 :                         freeze.tds = tds;
     133           0 :                         freeze.pkt = tds->frozen_packets;
     134           0 :                         freeze.pkt_pos = 8;
     135           0 :                         freeze.size_len = 0;
     136           0 :                         tds_freeze_abort(&freeze);
     137             : 
     138           0 :                         tds_connection_close(tds->conn);
     139             :                 }
     140             :                 break;
     141       60988 :         case TDS_WRITING:
     142       60988 :                 CHECK_TDS_EXTRA(tds);
     143             : 
     144       60988 :                 if (tds_mutex_trylock(&tds->wire_mtx))
     145           8 :                         return tds->state;
     146             : 
     147       60980 :                 if (tds->state == TDS_DEAD) {
     148           2 :                         tds_mutex_unlock(&tds->wire_mtx);
     149           2 :                         tdsdump_log(TDS_DBG_ERROR, "logic error: cannot change query state from %s to %s\n", 
     150           0 :                                                         state_names[prior_state], state_names[state]);
     151           2 :                         tdserror(tds_get_ctx(tds), tds, TDSEWRIT, 0);
     152           2 :                         break;
     153       60978 :                 } else if (tds->state != TDS_IDLE && tds->state != TDS_SENDING) {
     154          18 :                         tds_mutex_unlock(&tds->wire_mtx);
     155          18 :                         tdsdump_log(TDS_DBG_ERROR, "logic error: cannot change query state from %s to %s\n", 
     156           0 :                                                         state_names[prior_state], state_names[state]);
     157          18 :                         tdserror(tds_get_ctx(tds), tds, TDSERPND, 0);
     158          18 :                         break;
     159             :                 }
     160             : 
     161       60960 :                 if (tds->state == TDS_IDLE) {
     162             :                         /* TODO check this code, copied from tds_submit_prepare */
     163       59310 :                         tds_free_all_results(tds);
     164       59310 :                         tds->rows_affected = TDS_NO_COUNT;
     165       59310 :                         tds_release_cursor(&tds->cur_cursor);
     166       59310 :                         tds_release_cur_dyn(tds);
     167       59310 :                         tds->current_op = TDS_OP_NONE;
     168             :                 }
     169             : 
     170       60960 :                 tds->state = state;
     171       60960 :                 break;
     172             :         default:
     173           0 :                 assert(0);
     174             :                 break;
     175             :         }
     176             : 
     177      960718 :         state = tds->state;
     178             : 
     179      960718 :         tdsdump_log(TDS_DBG_INFO1, "Changed query state from %s to %s\n", state_names[prior_state], state_names[state]);
     180      960718 :         CHECK_TDS_EXTRA(tds);
     181             : 
     182      960718 :         return state;
     183             : }
     184             : 
     185             : 
     186             : void
     187        2762 : tds_swap_bytes(void *buf, int bytes)
     188             : {
     189             :         unsigned char tmp, *begin, *last;
     190             : 
     191        2762 :         begin = (unsigned char *) buf;
     192        2762 :         last  = begin + bytes;
     193             : 
     194       17534 :         while (begin < --last) {
     195       12010 :                 tmp      = *last;
     196       12010 :                 *last    = *begin;
     197       12010 :                 *begin++ = tmp;
     198             :         }
     199        2762 : }
     200             : 
     201             : unsigned int
     202        5862 : tds_gettime_ms(void)
     203             : {
     204             : #ifdef _WIN32
     205             :         return GetTickCount();
     206             : #elif defined(HAVE_GETHRTIME)
     207             :         return (unsigned int) (gethrtime() / 1000000u);
     208             : #elif defined(HAVE_CLOCK_GETTIME) && defined(TDS_GETTIMEMILLI_CONST)
     209             :         struct timespec ts;
     210        5862 :         clock_gettime(TDS_GETTIMEMILLI_CONST, &ts);
     211        5862 :         return (unsigned int) (ts.tv_sec * 1000u + ts.tv_nsec / 1000000u);
     212             : #elif defined(HAVE_GETTIMEOFDAY)
     213             :         struct timeval tv;
     214             :         gettimeofday(&tv, NULL);
     215             :         return (unsigned int) (tv.tv_sec * 1000u + tv.tv_usec / 1000u);
     216             : #else
     217             : #error How to implement tds_gettime_ms ??
     218             : #endif
     219             : }
     220             : 
     221             : /*
     222             :  * Call the client library's error handler
     223             :  */
     224             : #define EXINFO         1
     225             : #define EXUSER         2
     226             : #define EXNONFATAL     3
     227             : #define EXCONVERSION   4
     228             : #define EXSERVER       5
     229             : #define EXTIME         6
     230             : #define EXPROGRAM      7
     231             : #define EXRESOURCE     8
     232             : #define EXCOMM         9
     233             : #define EXFATAL       10
     234             : #define EXCONSISTENCY 11
     235             : 
     236             : typedef struct tds_error_message
     237             : {
     238             :         TDSERRNO msgno;
     239             :         int severity;
     240             :         const char *msgtext;
     241             : } TDS_ERROR_MESSAGE;
     242             : 
     243             : static const TDS_ERROR_MESSAGE tds_error_messages[] = 
     244             :         { { TDSEICONVIU,     EXCONVERSION,      "Buffer exhausted converting characters from client into server's character set" }
     245             :         , { TDSEICONVAVAIL,  EXCONVERSION,      "Character set conversion is not available between client character set '%.*s' and "
     246             :                                                 "server character set '%.*s'" }
     247             :         , { TDSEICONVO,      EXCONVERSION,      "Error converting characters into server's character set. Some character(s) could "
     248             :                                                 "not be converted" }
     249             :         , { TDSEICONVI,      EXCONVERSION,      "Some character(s) could not be converted into client's character set.  "
     250             :                                                 "Unconverted bytes were changed to question marks ('?')" }
     251             :         , { TDSEICONV2BIG,   EXCONVERSION,      "Some character(s) could not be converted into client's character set" }
     252             :         , { TDSEPORTINSTANCE,      EXUSER,      "Both port and instance specified" }
     253             :         , { TDSERPND,           EXPROGRAM,      "Attempt to initiate a new TDS server operation with results pending" }
     254             :         , { TDSEBTOK,              EXCOMM,      "Bad token from the server: Datastream processing out of sync" }
     255             :         , { TDSECAP,               EXCOMM,      "DB-Library capabilities not accepted by the Server" }
     256             :         , { TDSECAPTYP,            EXCOMM,      "Unexpected capability type in CAPABILITY datastream" }
     257             :         , { TDSECLOS,              EXCOMM,      "Error in closing network connection" }
     258             :         , { TDSECONN,              EXCOMM,      "Unable to connect: TDS server is unavailable or does not exist" }
     259             :         , { TDSEEUNR,              EXCOMM,      "Unsolicited event notification received" }
     260             :         , { TDSEFCON,              EXCOMM,      "TDS server connection failed" }
     261             :         , { TDSENEG,               EXCOMM,      "Negotiated login attempt failed" }
     262             :         , { TDSEOOB,               EXCOMM,      "Error in sending out-of-band data to the server" }
     263             :         , { TDSEREAD,              EXCOMM,      "Read from the server failed" }
     264             :         , { TDSETIME,              EXTIME,      "TDS server connection timed out" }
     265             :         , { TDSESEOF,              EXCOMM,      "Unexpected EOF from the server" }
     266             :         , { TDSEINTF,              EXUSER,      "Server name not found in configuration files." }
     267             :         , { TDSESOCK,              EXCOMM,      "Unable to open socket" }
     268             :         , { TDSESYNC,              EXCOMM,      "Read attempted while out of synchronization with TDS server" }
     269             :         , { TDSEUHST,              EXUSER,      "Unknown host machine name." }
     270             :         , { TDSEUMSG,              EXCOMM,      "Unknown message-id in MSG datastream" }
     271             :         , { TDSEUSCT,              EXCOMM,      "Unable to set communications timer" }
     272             :         , { TDSEUTDS,              EXCOMM,      "Unrecognized TDS version received from the server" }
     273             :         , { TDSEWRIT,              EXCOMM,      "Write to the server failed" }
     274             :         , { TDSECONF,              EXUSER,      "Local configuration error.  "
     275             :                                                 "Check TDSDUMPCONFIG log for details." }
     276             :         /* last, with msgno == TDSEOK */
     277             :         , { TDSEOK,              EXCONSISTENCY, "unrecognized msgno" }
     278             :         };
     279             : 
     280             : static const char *
     281           0 : retname(int retcode)
     282             : {
     283           0 :         switch(retcode) {
     284             :         case TDS_INT_CONTINUE:
     285             :                 return "TDS_INT_CONTINUE";
     286           0 :         case TDS_INT_CANCEL:
     287           0 :                 return "TDS_INT_CANCEL";
     288           0 :         case TDS_INT_TIMEOUT:
     289           0 :                 return "TDS_INT_TIMEOUT";
     290             :         }
     291           0 :         assert(0);
     292             :         return "nonesuch";
     293             : }
     294             : 
     295             : /**
     296             :  * \brief Call the client library's error handler (for library-generated errors only)
     297             :  *
     298             :  * The client library error handler may return: 
     299             :  * TDS_INT_CANCEL -- Return TDS_FAIL to the calling function.  For TDSETIME, closes the connection first. 
     300             :  * TDS_INT_CONTINUE -- For TDSETIME only, retry the network read/write operation. Else invalid.
     301             :  * TDS_INT_TIMEOUT -- For TDSETIME only, send a TDSCANCEL packet. Else invalid.
     302             :  *
     303             :  * These are Sybase semantics, but they serve all purposes.  
     304             :  * The application tells the library to quit, fail, retry, or attempt to cancel.  In the event of a network timeout, 
     305             :  * a failed operation necessarily means the connection becomes unusable, because no cancellation dialog was 
     306             :  * concluded with the server.  
     307             :  *
     308             :  * It is the client library's duty to call the error handler installed by the application, if any, and to interpret the
     309             :  * installed handler's return code.  It may return to this function one of the above codes only.  This function will not 
     310             :  * check the return code because there's nothing that can be done here except abort.  It is merely passed to the 
     311             :  * calling function, which will (we hope) DTRT.  
     312             :  *
     313             :  * \param tds_ctx       points to a TDSCONTEXT structure
     314             :  * \param tds           the connection structure, may be NULL if not connected
     315             :  * \param msgno         an enumerated libtds msgno, cf. tds.h
     316             :  * \param errnum        the OS errno, if it matters, else zero
     317             :  * 
     318             :  * \returns client library function's return code
     319             :  */
     320             : int
     321       33105 : tdserror (const TDSCONTEXT * tds_ctx, TDSSOCKET * tds, int msgno, int errnum)
     322             : {
     323             : #if 0
     324             :         static const char int_exit_text[] = "FreeTDS: libtds: exiting because client error handler returned %d for msgno %d\n";
     325             :         static const char int_invalid_text[] = "%s (%d) received from client library error handler for nontimeout for error %d."
     326             :                                                "  Treating as INT_EXIT\n";
     327             : #endif
     328             :         const TDS_ERROR_MESSAGE *err;
     329             : 
     330             :         TDSMESSAGE msg;
     331       33105 :         int rc = TDS_INT_CANCEL;
     332             : 
     333       33105 :         tdsdump_log(TDS_DBG_FUNC, "tdserror(%p, %p, %d, %d)\n", tds_ctx, tds, msgno, errnum);
     334             : 
     335             :         /* look up the error message */
     336       48627 :         for (err = tds_error_messages; err->msgno != TDSEOK; ++err) {
     337       48627 :                 if (err->msgno == msgno)
     338             :                         break;
     339             :         }
     340             : 
     341       33105 :         CHECK_CONTEXT_EXTRA(tds_ctx);
     342             : 
     343       33105 :         if (tds)
     344       33105 :                 CHECK_TDS_EXTRA(tds);
     345             : 
     346       33105 :         if (tds_ctx && tds_ctx->err_handler) {
     347       33056 :                 memset(&msg, 0, sizeof(TDSMESSAGE));
     348       33056 :                 msg.msgno = msgno;
     349       33056 :                 msg.severity = err->severity;
     350       33056 :                 msg.state = -1;
     351       33056 :                 msg.server = "OpenClient";
     352       33056 :                 msg.line_number = -1;
     353       33056 :                 msg.message = (TDS_CHAR*) err->msgtext;
     354       33056 :                 msg.sql_state = tds_alloc_client_sqlstate(msgno);
     355             :                 
     356       33056 :                 msg.oserr = errnum;
     357             : 
     358             :                 /*
     359             :                  * Call client library handler.
     360             :                  * The client library must return a valid code.  It is not checked again here.
     361             :                  */
     362       33056 :                 rc = tds_ctx->err_handler(tds_ctx, tds, &msg);
     363       33056 :                 tdsdump_log(TDS_DBG_FUNC, "tdserror: client library returned %s(%d)\n", retname(rc), rc);
     364             : 
     365       33056 :                 TDS_ZERO_FREE(msg.sql_state);
     366             :         } else {
     367             :                 static const char msg[] = "tdserror: client library not called because either "
     368             :                                           "tds_ctx (%p) or tds_ctx->err_handler is NULL\n";
     369          49 :                 tdsdump_log(TDS_DBG_ERROR, msg, tds_ctx);
     370             :         }
     371             : 
     372             :   
     373       33105 :         assert(msgno == TDSETIME || rc != TDS_INT_TIMEOUT);   /* client library should prevent */
     374       33105 :         assert(msgno == TDSETIME || rc != TDS_INT_CONTINUE);  /* client library should prevent */
     375             : 
     376       33105 :         if (msgno != TDSETIME && rc != TDS_INT_CANCEL) {
     377           0 :                 tdsdump_log(TDS_DBG_SEVERE, "exit: %s(%d) valid only for TDSETIME\n", retname(rc), rc);
     378             :                 rc = TDS_INT_CANCEL;
     379             :         }
     380             : 
     381       33105 :         if (rc == TDS_INT_TIMEOUT) {
     382         160 :                 tds_send_cancel(tds);
     383         160 :                 rc = TDS_INT_CONTINUE;
     384             :         } 
     385             : 
     386       33105 :         tdsdump_log(TDS_DBG_FUNC, "tdserror: returning %s(%d)\n", retname(rc), rc);
     387             :   
     388       33105 :         return rc;
     389             : }
     390             : 

Generated by: LCOV version 1.13