Hi all--

I'm stuck on a piece of code; I think it's an Oracle problem and wonder if anyone can shed any light.

In brief, I have a (tiny) multi-threaded server, written in C, running on my school's Solaris system. It uses detached threads and stream internet sockets. It operates properly, blocking on accept() until it receives a message, processing the message, and blocking again.

Problem: if I uncomment Pro*C code that connects to Oracle (9.0.1.0.0 Enterprise edition), that connection succeeds, and data is retrieved from the db. But accept() seems to unblock; the threads now run without restriction, failing on the accept and writing an error message to the logfile.

Thanks very much in advance to anyone who replies.

This is the db connection code:
Code:
int db_connect()
{
  char mstsLname[LOG_NAME_LEN] = "thelogname";
  char mstsPasswd[PWD_LEN] = "thepassword";

  varchar mstsLogname[LOG_NAME_LEN];
  varchar mstsPassword[PWD_LEN];

  strncpy((char *) mstsLogname.arr, mstsLname, LOG_NAME_LEN);  /* copy user name */
  mstsLogname.len = (unsigned short) strlen((char *) mstsLogname.arr);
  strncpy((char *) mstsPassword.arr, mstsPasswd, PWD_LEN);   /* copy password */
  mstsPassword.len = (unsigned short) strlen((char *) mstsPassword.arr);

  EXEC SQL WHENEVER SQLERROR DO err_report(sqlca);

  EXEC SQL CONNECT :mstsLogname IDENTIFIED BY :mstsPassword;

  printf("connected\n");

  EXEC SQL COMMIT WORK RELEASE;

  printf("logged off\n");

  return 53;
}
and this is the routine in my C code that contains the accept() loop:

Code:
void socket_routine(int port_num)
{
  thread_registry_t *thread_bank;
  int i = NUMTHREADS - 1;
  pthread_attr_t attr;
  FILE *fp_mylogfile;
  int soc, new_soc, status;
  int portno, clilen, pid;
  struct sockaddr_in serv_addr, cli_addr;
  int val;

#ifdef DEBUG
  int j = 0;
#endif

  /* open logfile or die */
  if ((fp_mylogfile = fopen(MY_LOGFILE, "a+")) == NULL) {
    fprintf(stderr, "failed to open logfile\n");
    exit(1);
  }

  /* turn off buffering for log output */
  setbuf(fp_mylogfile, NULL);

  /* redirect stderr to log file */
  if (freopen(MY_LOGFILE, "a+", stderr) == NULL)
    fprintf(fp_mylogfile, "Unable to redirect stderr\n");

  /* MUST turn off buffering for redirected stderr now also */
  setbuf(stderr, NULL);

  /* report AOK (only 1 thread here) */
  fprintf(fp_mylogfile, "[%s] Starting program\n", get_time_string());

  soc = socket(AF_INET, SOCK_STREAM, 0);
  if (soc < 0)
    error("ERROR opening socket");
  bzero((char *) &serv_addr, sizeof(serv_addr));

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(port_num);

  val = 1;
  if (setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) != 0) {
    fprintf(fp_mylogfile, "[%s] cant set sock opts\n", get_time_string());
    exit(1);
  }

  if (bind(soc, (struct sockaddr *) &serv_addr,
           sizeof(serv_addr)) < 0)
    error("ERROR on binding");
  listen(soc, 1);

  /* report (still only one thread) */
  fprintf(fp_mylogfile, "[%s] Listening...\n", get_time_string());

  /* get the threads ready */
  thread_bank = setup_thread_registry(fp_mylogfile);
  setup_thread_attr(&attr);

#ifdef DEBUG
  while(j++ < 10) {
#else
  while(1) {
#endif

    if (i == NUMTHREADS - 1)
      i = 0;
    else
      ++i;

    clilen = sizeof(cli_addr);

    new_soc = accept(soc, (struct sockaddr *) &cli_addr, &clilen);

    /* CRITICAL REGION */
    pthread_mutex_lock(&global_logfile_mutex);
    if (new_soc >= 0) {
      fprintf(fp_mylogfile, "[%s] Accept successful on socket %d\n",
              get_time_string(), new_soc);
    }
    else {
      fprintf(fp_mylogfile, "[%s] Accept failed\n", get_time_string());
      close(soc);
    }
    pthread_mutex_unlock(&global_logfile_mutex);
    /* END CRITICAL REGION */
    thread_bank[i].ns = new_soc;

    status = pthread_create(&thread_bank[i].tid, &attr, dispatch_routine, &thread_bank[i]);

    if (status) {
    /* CRITICAL REGION */
      pthread_mutex_lock(&global_logfile_mutex);
      fprintf(fp_mylogfile, "[s] pthread create failed\n", get_time_string());
      close(soc);
      pthread_mutex_unlock(&global_logfile_mutex);
    /* END CRITICAL REGION */
    }
  } /* end of while */

  close(soc);  // don't wanna get here

  /* CRITICAL REGION */
  pthread_mutex_lock(&global_logfile_mutex);
  fprintf(fp_mylogfile, "[%s] listening socket closed\n", get_time_string());
  pthread_mutex_unlock(&global_logfile_mutex);
  /* END CRITICAL REGION */

  cleanup_thread_registry(thread_bank);

  fprintf(fp_mylogfile, "[%s] thread registry deleted. stopping program.\n",
          get_time_string());

  fclose(fp_mylogfile);
}