community.borland.com

Article #25524: How to get a list of users connected to a database

Problem:
How do I get a list of users connected to a database?

Solution:
Use the InterBase API function: isc_database_info

Here is an example of how to connect to a database a get a list
of users connected to the database.

---------------------- begin example ---------------------

/*
 * Name: getConnectedUsers.c
 *
 *
 * It then uses the isc_database_info call to fetch the number
 * of users connected to that database
 *
 *  Brett Bandy, June 1998
 */

#include 
#include 
#include "ibase.h"

void usage()
{
  printf("Syntax Error:n");
  printf("t getConnectedUsers n");
  return;
}

int main(int argc, char **argv)
{
  ISC_STATUS isc_status[20];
  isc_db_handle db;
  char dbname[128];
  char infoBuffer[] = {isc_info_user_names};
  char result_buffer[512];
  char *p;
  char userName[32];
  int length;
  char item;
  int i;
  int userNameSize;
  int totalUsers;

  /* init dbhandle */
  db = 0L;

  switch(argc)
  {
    case 1: 
      /* get dbname from user */
      printf("Enter Database to check for connections: ");
      gets(dbname); 
      break;
    case 2:
      /* dbname is passed in */
      strcpy(dbname, argv[1]);
      break;
    default:
	usage();
	return;
  }


  printf("nAttempting to attach to database:nt%snn", dbname);

  isc_attach_database(isc_status, strlen(dbname), dbname, 
                      &db, 0, NULL);
  if(isc_status[0] == 1 && isc_status[1])
  {
    isc_print_status(isc_status);
    exit(1);
  }

  /* now fetch the number of cache buffers database is using */
  isc_database_info(isc_status,
		    &db,                  /* ptr to db handle */
		    sizeof(infoBuffer),   /* size of items buffer */
		    infoBuffer,           /* array of items to retrieve */
		    sizeof(result_buffer),
		    result_buffer);       /* results will be written */
  if(isc_status[0] == 1 && isc_status[1])
  {
    isc_print_status(isc_status);
    exit(1);
  }

  /* initialize totalUsers */
  totalUsers = 0;

  /*
   * Format for this call is:
   * 
   * 	1 byte:  type of info item (isc_info_user_names)
   *	2 bytes: length of next item (must use isc_vax_integer 
   *             to get correct byte order)
   * 	x bytes: next item which consists of...
   *		1 byte:  length of username
   *		x bytes: username
   *
   */

  /* extract the info from result_buffer */
  /* loop because there can be more that one user */
  for(p = result_buffer; *p != isc_info_end;)
  {

    item = *p++;   /* item return type */

    if(item != isc_info_user_names)
    {
      printf("Error retrieving user namesn");
      return;
    }

    /* this will return the length of the */
    /* item in bytes (length is 2 byte) */
    length = isc_vax_integer(p, 2);  

    /* increment pointer so points to next username */
    p+=2;

    /* username items are stored in pascal style 
       <1 byte for length> */

    /* get length of username */
    userNameSize = isc_vax_integer(p,1);
    p+=1;

    /* copy username from result_buffer to userName char array */
    memcpy(userName, p, length);
    userName[userNameSize] = '0';

    /* advance p to point to start of next item */
    p+=userNameSize;

    printf("User [%s] is connectedn", userName);
    totalUsers++;
  }

  printf("nTotal Connected User Count is %dn", totalUsers);

  isc_detach_database(isc_status, &db);
  if(isc_status[0] == 1 && isc_status[1])
  {
    isc_print_status(isc_status);
    exit(1);
  }
}


---------------------- end example   ---------------------

Last Modified: 26-OCT-00