示例#1
0
CBaseSecurityManager::~CBaseSecurityManager()
{
    MapStrToUsers::iterator pos;
    for(pos=m_userList.begin();pos!=m_userList.end();){
        pos->second->Release();
        pos++;
    }
    dbDisconnect();
}
示例#2
0
int
codaPrestart()
{
  MYSQL *dbsock;
  char tmpp[1000];

  objClass object = localobject;

  MONp monp = (MONp) object->privated;

  printf("INFO: Prestarting\n");

  monp->nend = 1;

  /* connect to database */
  dbsock = dbConnect(getenv("MYSQL_HOST"), expid);

  /* Get the run number */
  sprintf(tmpp,"SELECT runNumber FROM sessions WHERE name='%s'",session);
  if(dbGetInt(dbsock,tmpp,&(object->runNumber))==MON_ERROR) return(MON_ERROR);

  /* DO WE NEED TO GET runType AS WELL ??? */

  /* disconnect from database */
  dbDisconnect(dbsock);

  printf("INFO: prestarting,run %d, type %d\n",
    object->runNumber, object->runType);

  PrestartCount = 0;
  object->nevents = 0;
  object->nlongs = 0;

  codaUpdateStatus("paused");

  return(MON_OK);
}
示例#3
0
char *fetchHelp(int getThis, int getType, struct threadInfo * ti) {
	int (*dbConnect)(struct threadStorageInfo *);
	void (*dbDisconnect)(struct threadStorageInfo *);
	void (*dbEscape)(struct threadStorageInfo *, char *, char *);
	void (*dbPush)(struct threadStorageInfo *, char *);
	char *(*dbPull)(struct threadStorageInfo *, char *);
	void (*dbFree)(struct threadStorageInfo *);

	dbConnect = ti->dbInfo.connect;
	dbDisconnect = ti->dbInfo.disconnect;
	dbEscape = ti->dbInfo.escape;
	dbPush = ti->dbInfo.push;
	dbPull = ti->dbInfo.pull;
	dbFree = ti->dbInfo.free;

	ti->commandInfo.esc1Buffer = NULL;
	ti->commandInfo.esc2Buffer = NULL;
	ti->commandInfo.esc3Buffer = NULL;
	ti->commandInfo.esc4Buffer = NULL;
	ti->commandInfo.esc5Buffer = NULL;

	// Allocate memory for buffers
	ti->commandInfo.s =
		(ti->handlerArrays[HANDLER_ARRAY_UID].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 8) +
		CONFIG_SPACE_SIZE;

	if((ti->commandInfo.statBuffer = malloc(ti->commandInfo.s + 1)) == NULL) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

		return(ti->dataBuffer);
	}

	// Escape parameters received from the client
	if(ti->handlerArrays[HANDLER_ARRAY_UID].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_UID].buffer[0] != 0) {
		if((ti->commandInfo.esc1Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc1Buffer, ti->handlerArrays[HANDLER_ARRAY_UID].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer[0] != 0) {
		if((ti->commandInfo.esc2Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc2Buffer, ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer[0] != 0) {
		if((ti->commandInfo.esc3Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc3Buffer, ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] != 0) {
		if((ti->commandInfo.esc4Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc4Buffer, ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer[0] != 0) {
		if((ti->commandInfo.esc5Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);
			free(ti->commandInfo.esc4Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc5Buffer, ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer);
	}

	// Connect to database
	if(dbConnect(&ti->storageInfo) != 0) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_CONNECTFAILED, ERROR_MESS_DATABASE_CONNECTFAILED, ti);
	}
	else {
		// Construct SQL statement for requested item
		switch(getThis) {
			case 1:
				// List all help pages
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_HELP_HELP " FROM " TABLE_HELP " ORDER BY " TABLEORD_HELP_HELP "%c",
					0
				);

				break;
			case 2:
				// Get requested help page
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_HELP_HELP " FROM " TABLE_HELP " WHERE " TABLECOL_HELP_NAME " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 3:
				// Search from help pages
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_HELP_HELP " FROM " TABLE_HELP " WHERE " TABLECOL_HELP_TITLE " LIKE '%%%s%%' OR " TABLECOL_HELP_HELP " LIKE '%%%s%%' OR " TABLECOL_HELP_DESCR " LIKE '%%%s%%' OR " TABLECOL_HELP_NOTE " LIKE '%%%s%%' ORDER BY " TABLECOL_HELP_TITLE "%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		// Pull data out from database
		switch(getType) {
			case QUERY_TYPE_PULL:
				if(dbPull(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			case QUERY_TYPE_PUSH:
				dbPush(&ti->storageInfo, ti->commandInfo.statBuffer);

				if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer == NULL || ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] == 0 || ti->handlerArrays[HANDLER_ARRAY_PARAM].size == 0) {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Help \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}
				else {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Help \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}

				logMessage(ti->logSpace);

				replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		dbDisconnect(&ti->storageInfo);
	}

	// Free allocated resources and reply to the client
	if(ti->commandInfo.esc1Buffer != NULL) {
		free(ti->commandInfo.esc1Buffer);
	}

	if(ti->commandInfo.esc2Buffer != NULL) {
		free(ti->commandInfo.esc2Buffer);
	}

	if(ti->commandInfo.esc3Buffer != NULL) {
		free(ti->commandInfo.esc3Buffer);
	}

	if(ti->commandInfo.esc4Buffer != NULL) {
		free(ti->commandInfo.esc4Buffer);
	}

	if(ti->commandInfo.esc5Buffer != NULL) {
		free(ti->commandInfo.esc5Buffer);
	}

	free(ti->commandInfo.statBuffer);

	return(ti->dataBuffer);
}
char *fetchDevice(int getThis, int getType, struct threadInfo * ti) {
	int (*dbConnect)(struct threadStorageInfo *);
	void (*dbDisconnect)(struct threadStorageInfo *);
	void (*dbEscape)(struct threadStorageInfo *, char *, char *);
	void (*dbPush)(struct threadStorageInfo *, char *);
	char *(*dbPull)(struct threadStorageInfo *, char *);
	char *(*dbRound)(struct threadStorageInfo *, char *);
	void (*dbFree)(struct threadStorageInfo *);

	dbConnect = ti->dbInfo.connect;
	dbDisconnect = ti->dbInfo.disconnect;
	dbEscape = ti->dbInfo.escape;
	dbPush = ti->dbInfo.push;
	dbPull = ti->dbInfo.pull;
	dbRound = ti->dbInfo.round;
	dbFree = ti->dbInfo.free;

	ti->commandInfo.esc1Buffer = NULL;
	ti->commandInfo.esc2Buffer = NULL;
	ti->commandInfo.esc3Buffer = NULL;
	ti->commandInfo.esc4Buffer = NULL;
	ti->commandInfo.esc5Buffer = NULL;

	// Allocate memory for buffers
	ti->commandInfo.s =
		(ti->handlerArrays[HANDLER_ARRAY_UID].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 18) +
		CONFIG_SPACE_SIZE;

	if((ti->commandInfo.statBuffer = malloc(ti->commandInfo.s + 1)) == NULL) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

		return(ti->dataBuffer);
	}

	// Escape parameters received from the client
	if(ti->handlerArrays[HANDLER_ARRAY_UID].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_UID].buffer[0] != 0) {
		if((ti->commandInfo.esc1Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc1Buffer, ti->handlerArrays[HANDLER_ARRAY_UID].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer[0] != 0) {
		if((ti->commandInfo.esc2Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc2Buffer, ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer[0] != 0) {
		if((ti->commandInfo.esc3Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc3Buffer, ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] != 0) {
		if((ti->commandInfo.esc4Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc4Buffer, ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer[0] != 0) {
		if((ti->commandInfo.esc5Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);
			free(ti->commandInfo.esc4Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc5Buffer, ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer);
	}

	// Connect to database
	if(dbConnect(&ti->storageInfo) != 0) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_CONNECTFAILED, ERROR_MESS_DATABASE_CONNECTFAILED, ti);
	}
	else {
		// Construct SQL statement for requested machine
		switch(getThis) {
			case 1:
				// List all devices
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '0' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 2:
				// List requested devices by category
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '0' AND " TABLECOL_DEVICE_CATEGORY " = '%s' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 3:
				// Get requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_DEVICE_DATA " FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 4:
				// Search from devices
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_DEVICE_SEARCH " FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_MODEL " LIKE '%%%s%%' OR " TABLECOL_DEVICE_CATEGORY " LIKE '%%%s%%' OR " TABLECOL_DEVICE_MANUFACTURER " LIKE '%%%s%%' OR " TABLECOL_DEVICE_SIZE " LIKE '%%%s%%' OR " TABLECOL_DEVICE_IPADDRESS " LIKE '%%%s%%' OR " TABLECOL_DEVICE_SERNUM " LIKE '%%%s%%' OR " TABLECOL_DEVICE_SECNUM " LIKE '%%%s%%' OR " TABLECOL_DEVICE_LOCATION " LIKE '%%%s%%' OR " TABLECOL_DEVICE_MOSD " LIKE '%%%s%%' ORDER BY " TABLEORD_DEVICE_NAME "%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 5:
				// Create new device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE " (" TABLEKEY_DEVICE_NEW ") VALUES('%s', '%s', '%s', '%s')%c",
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 6:
				// Modify existing device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET %s = '%s' WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc5Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 7:
				// Modify existing device timestamp
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET " TABLECOL_DEVICE_MODIFIED " = NOW(), " TABLECOL_DEVICE_MODIFIER " = '%s' WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 8:
				// Delete existing device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET " TABLECOL_DEVICE_DISPOSED " = " TABLECOL_DEVICE_DISPOSED " + 1 WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 9:
				// Restore disposed device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET " TABLECOL_DEVICE_DISPOSED " = '0' WHERE " TABLECOL_DEVICE_DISPOSED " = '1' AND " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 10:
				// Attach device to requested machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_MACHINE_ID ") VALUES('%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 11:
				// Remove device from requested machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_MACHINE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 12:
				// Get devices attached in machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_DEVICE_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_MACHINE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 13:
				// Get machines attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_MACHINE_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 14:
				// List all disposed devices
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '1' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 15:
				// List requested disposed devices by type
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '1' AND " TABLECOL_DEVICE_CATEGORY " = '%s' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 16:
				// Attach device to requested domain
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_MACHINE_ID ") (SELECT '%s', " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DOMAIN " = '%s' AND " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 17:
				// Attach device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_MACHINE_ID ") (SELECT '%s', " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 18:
				// Remove device to requested domain
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_MACHINE_ID " IN (SELECT " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DOMAIN " = '%s' AND " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 19:
				// Remove device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_MACHINE_ID " IN (SELECT " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 20:
				// Attach device to requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_DEVICES_ID ") VALUES('%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 21:
				// Attach device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICES_ID ", " TABLECOL_DEVICE_MAP_DEVICE_ID ") (SELECT '%s', " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 22:
				// Get devices attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_DEVICES_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 23:
				// Get peripherals attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 24:
				// Remove device from requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_DEVICES_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 25:
				// Remove device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICES_ID " = '%s' AND " TABLECOL_DEVICE_MAP_DEVICE_ID " IN (SELECT " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 26:
				// Attach device to requested peripheral
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_PERIPHERALS_ID ") VALUES('%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 27:
				// Attach device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_PERIPHERALS_ID ", " TABLECOL_DEVICE_MAP_DEVICE_ID ") (SELECT '%s', " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 28:
				// Get devices attached in peripheral
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 29:
				// Get peripherals attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_DEVICES_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 30:
				// Remove device from requested peripheral
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 31:
				// Remove device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " = '%s' AND " TABLECOL_DEVICE_MAP_DEVICE_ID " IN (SELECT " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 32:
				// Clone device attachments for machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICES ") SELECT " TABLECOL_DEVICE_MAP_DEVICE_ID ",'%s','0','0' FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_MACHINE_ID " IN (SELECT " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_NODE " = '%s' AND " TABLECOL_MACHINE_DOMAIN " = '%s' AND " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc3Buffer,
					0
				);

				break;
			case 33:
				// Add note to requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_NOTES " (" TABLEKEY_NOTES_DEVICE ") VALUES('%s', '%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 34:
				// Pull notes from requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT " TABLEKEY_NOTES_DATA " FROM " TABLE_NOTES " WHERE " TABLECOL_NOTES_DEVICE " = '%s' ORDER BY " TABLEORD_NOTES_CREATED "%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 35:
				// Update note for requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_NOTES " SET " TABLECOL_NOTES_MODIFIED " = NOW(), " TABLECOL_NOTES_MODIFIER " = '%s', " TABLECOL_NOTES_NOTE " = '%s' WHERE " TABLECOL_NOTES_ID " = '%s' AND " TABLECOL_NOTES_CREATOR " = '%s'%c",
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 36:
				// Delete note for requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_NOTES " WHERE " TABLECOL_NOTES_ID " = '%s' AND " TABLECOL_NOTES_CREATOR " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 37:
				// Pull note from requested device by id
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT " TABLEKEY_NOTES_DATA " FROM " TABLE_NOTES " WHERE " TABLECOL_NOTES_DEVICE " = '%s' AND " TABLECOL_NOTES_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		// Pull data out from database
		switch(getType) {
			case QUERY_TYPE_PULL:
				if(dbPull(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			case QUERY_TYPE_PUSH:
				dbPush(&ti->storageInfo, ti->commandInfo.statBuffer);

				if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer == NULL || ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] == 0 || ti->handlerArrays[HANDLER_ARRAY_PARAM].size == 0) {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Device \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}
				else {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Device \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}

				logMessage(ti->logSpace);

				replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

				break;
			case QUERY_TYPE_ROUND:
				if(dbRound(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Device \"%s\" created by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);

					logMessage(ti->logSpace);

					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		dbDisconnect(&ti->storageInfo);
	}

	// Free allocated resources and reply to the client
	if(ti->commandInfo.esc1Buffer != NULL) {
		free(ti->commandInfo.esc1Buffer);
	}

	if(ti->commandInfo.esc2Buffer != NULL) {
		free(ti->commandInfo.esc2Buffer);
	}

	if(ti->commandInfo.esc3Buffer != NULL) {
		free(ti->commandInfo.esc3Buffer);
	}

	if(ti->commandInfo.esc4Buffer != NULL) {
		free(ti->commandInfo.esc4Buffer);
	}

	if(ti->commandInfo.esc5Buffer != NULL) {
		free(ti->commandInfo.esc5Buffer);
	}

	free(ti->commandInfo.statBuffer);

	return(ti->dataBuffer);
}
示例#5
0
DBHandler::~DBHandler()
{
    dbDisconnect();
}
示例#6
0
void  ltDbClose(ltDbConn *psConn){
	dbDisconnect(psConn);
}
示例#7
0
int
main(int argc, char *argv[])
{
  struct hostent *hptr;
 
  TREQUEST myRequest; /* request to send to server */ 
  struct sockaddr_in serverAddr; /* server's socket address */ 
  char replyBuf[REPLY_MSG_SIZE*100]; /* buffer for reply */ 
  char reply; /* if TRUE, expect reply back */ 
  int sockAddrSize; /* size of socket address structure */ 
  int sFd; /* socket file descriptor */ 
  int mlen; /* length of message */
  int rBytes;
  int i, portnum = 0;
  char hostname[128];
  MYSQL *dbsock;
  char tmp[1000], temp[100];
  char *ch;

  /* make sure all arguments are there */
  if (argc<3) {
    printf("Usage: tcpClient <target name> [command]\n");
    exit(1);
  }

  /* create client's socket */ 
  if((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)
  {
    perror ("socket"); 
    return (ERROR); 
  } 

  /* bind not required - port number is dynamic */ 
  /* build server socket address */ 
  sockAddrSize = sizeof (struct sockaddr_in); 
  bzero ((char *) &serverAddr, sockAddrSize); 
  serverAddr.sin_family = AF_INET; 

  /* get port and host from DB; if no record in DB for <target name> - exit */
  dbsock = dbConnect(getenv("MYSQL_HOST"), "daq");

  /* use 'port' field */
  sprintf(tmp,"SELECT tcpClient_tcp FROM Ports WHERE name='%s'",argv[1]);
  /*
  printf("DB select: >%s<\n",tmp);
  */
  if(dbGetInt(dbsock, tmp, &portnum)==ERROR) return(ERROR);

  sprintf(tmp,"SELECT Host FROM Ports WHERE name='%s'",argv[1]);
  /*
  printf("DB select: >%s<\n",tmp);
  */
  if(dbGetStr(dbsock, tmp, hostname)==ERROR) return(ERROR);

  dbDisconnect(dbsock);
  /*  
  printf("hostname=>%s< portnum=%d\n",hostname,portnum);
  */

  serverAddr.sin_port = htons(portnum/*SERVER_PORT_NUM*/); 

  hptr = gethostbyname(hostname);
  if(hptr == NULL) {
      printf("unknown hostname %s \n",hostname); 
      close(sFd);
      exit(1);
    } else {
      memcpy(&serverAddr.sin_addr,*(hptr->h_addr_list),sizeof(sizeof(struct in_addr)));
    }


  /*
    If the connection cannot be established immediately and O_NONBLOCK is set for
    the file descriptor for the socket, connect() shall fail and set errno to [EINPROGRESS],
    but the connection request shall not be aborted, and the connection shall be established
    asynchronously. Subsequent calls to connect() for the same socket, before the connection
    is established, shall fail and set errno to [EALREADY].

    When the connection has been established asynchronously, select() and poll() shall
    indicate that the file descriptor for the socket is ready for writing.
  */


goto a123;
  {
    int itmp, orig_flags;
    int on = 1; /* blocking */
    int off = 0; /* nonblocking */

    orig_flags = fcntl(sFd, F_GETFL, 0);
    printf("orig_flags=0x%08x\n",orig_flags);

	
    if(fcntl(sFd, F_SETFL, orig_flags | O_NONBLOCK) < 0)
    {
      perror("fcntl(O_NONBLOCK)");
    }
	
	/*
    if(fcntl(sFd, F_SETFL, orig_flags | O_NDELAY) < 0)
    {
      perror("fcntl(O_NDELAY)");
	}
	*/

	/*ioctl: No such file or directory (O_NDELAY or O_NONBLOCK ?)
    itmp = ioctl(sFd,O_NDELAY,(int) &on);
    if(itmp < 0) perror("ioctl");
	*/

  }
a123:

    /* see dpS/dpS.s/dplite.c how DP_cmd works !!! 
    {
      int cval = 1;
      int linger[2];

      linger[0] = 0;
      linger[1] = 0;

      setsockopt(sFd, SOL_SOCKET, SO_LINGER,
		 (char *)linger, sizeof(linger));
      
      setsockopt(sFd, IPPROTO_TCP, TCP_NODELAY, (char *)&cval, sizeof(int));
    }
	*/

  /*
  3.5.  How can I set the timeout for the connect() system call?

  From Richard Stevens ([email protected]):

  Normally you cannot change this.  Solaris does let you do this, on a
  per-kernel basis with the ndd tcp_ip_abort_cinterval parameter.

  The easiest way to shorten the connect time is with an alarm() around
  the call to connect().  A harder way is to use select(), after setting
  the socket nonblocking.  Also notice that you can only shorten the
  connect time, there's normally no way to lengthen it.

  From Andrew Gierth ([email protected]):

  First, create the socket and put it into non-blocking mode, then call
  connect(). There are three possibilities:

  o  connect succeeds: the connection has been successfully made (this
     usually only happens when connecting to the same machine)

  o  connect fails: obvious

  o  connect returns -1/EINPROGRESS. The connection attempt has begun,
     but not yet completed.

  If the connection succeeds:

  o  the socket will select() as writable (and will also select as
     readable if data arrives)

  If the connection fails:

  o  the socket will select as readable *and* writable, but either a
     read or write will return the error code from the connection
     attempt. Also, you can use getsockopt(SO_ERROR) to get the error
     status - but be careful; some systems return the error code in the
     result parameter of getsockopt, but others (incorrectly) cause the
     getsockopt call *itself* to fail with the stored value as the
     error.
*/


  signal(SIGALRM,alarmHandler);
  alarm(10); /* in 3 seconds connect call will be interrupted if did not finished */

  /* connect to server */ 
  /*printf("connecting to server ...\n");*/ 
  if (connect (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR)
  {
    /*perror ("connect");*/ 
    close (sFd); 
	return (ERROR);
  }
  /*printf("connected !!!\n");*/ 

  alarm(0); /* clear alarm so it will not interrupt us */

  /* build request, prompting user for message */ 
  myRequest.msgLen = strlen(argv[2]);
  sprintf(myRequest.message,"%s\0",argv[2]);
  myRequest.reply = TRUE;
  /*  
printf("1: 0x%08x 0x%08x\n", myRequest.msgLen, myRequest.reply);
  */
  /* convert integers to network byte order */
  myRequest.msgLen = htonl(myRequest.msgLen);
  myRequest.reply = htonl(myRequest.reply);
  /*
printf("2: 0x%08x 0x%08x\n", myRequest.msgLen, myRequest.reply);
  */
  /*
  printf(" Sending %d bytes: %s (len=%d)\n",
    myRequest.msgLen, myRequest.message, sizeof (myRequest));
  fflush(stdout);
  */

  /* send request to server */ 
  if(write(sFd, (char *) &myRequest, sizeof(TREQUEST)) == ERROR)
  {
    perror("write"); 
    close(sFd); 
    return(ERROR);
  } 

  if(myRequest.reply) /* if expecting reply, read and display it */
  { 
    int remB = 0;
	/*
    printf("MESSAGE FROM SERVER:\n");
	*/
    while( (rBytes = read(sFd, (char *)&replyBuf[remB], REPLY_MSG_SIZE)) > 0)
    {
#ifdef INSTANT_PRINT
      for(i=0;i<rBytes;i++) printf("%c",replyBuf[remB+i]);fflush(stdout);
#endif
      remB += rBytes;
    }
#ifdef INSTANT_PRINT
    printf("\n");
	fflush(stdout);
#else
    ch = strstr(replyBuf,"value = ");
    if(ch == NULL) mlen = remB;
    else           mlen = (int)ch - (int)replyBuf;
	/*
    printf("buf starts at 0x%08x, strstr returns 0x%08x, mlen=%d\n",
      replyBuf,ch,mlen);
	fflush(stdout);
	*/
    for(i=0; i<mlen; i++)
    {
      printf("%c",replyBuf[i]);
    }
    printf("\n");
#endif
  } 
  close(sFd);
 
  return(OK);
 }
示例#8
0
void
get_run_config(char *mysql_database, char *session, int *run, char **config, char **conffile, char **datafile)
{
  MYSQL *connNum;
  MYSQL_RES *result;
  MYSQL_ROW row_out;
  char query[1024];

  *run=0;
  *config=NULL;
  *conffile=NULL;
  *datafile=NULL;

  /* connect to mysql database */
  connNum = dbConnect(getenv("MYSQL_HOST"), mysql_database);

  /* form mysql query, execute, then close mysql connection */
  sprintf(query,"SELECT runNumber,config FROM sessions WHERE name='%s'",session);
  if(mysql_query(connNum, query) != 0)
  {
    printf("get_run_config: ERROR in mysql_query 1\n");
    dbDisconnect(connNum);
    return;
  }

  if(!(result = mysql_store_result(connNum) ))
  {
    printf("get_run_config: ERROR in mysql_store_result 1\n");
    dbDisconnect(connNum);
    return;
  }

  /* get 'row_out' and check it for null */
  row_out = mysql_fetch_row(result);
  if(row_out==NULL)
  {
    mysql_free_result(result);
    dbDisconnect(connNum);
 
    return;
  }

  /* run number */ 
  if(row_out[0]==NULL)
  {
    *run=0;
  }
  else
  {
    *run=atoi(row_out[0]);
  }

  /* config */
#ifdef VXWORKS
  *config=mystrdup(row_out[1]);
#else
  *config=strdup(row_out[1]);
#endif
  mysql_free_result(result);








  /* get conffile */

  sprintf(query,"SELECT value FROM %s_option WHERE name='confFile'",(char *)(*config));
  if(mysql_query(connNum, query) != 0)
  {
    printf("get_run_config: ERROR in mysql_query 2\n");
    dbDisconnect(connNum);
    return;
  }
  if(!(result = mysql_store_result(connNum) ))
  {
    printf("get_run_config: ERROR in mysql_store_result 2\n");
    dbDisconnect(connNum);
    return;
  }
  row_out = mysql_fetch_row(result);
  if(row_out==NULL)
  {
    mysql_free_result(result);
    dbDisconnect(connNum); 
    return;
  }
#ifdef VXWORKS
  *conffile = mystrdup(row_out[0]);
#else
  *conffile = strdup(row_out[0]);
#endif
  mysql_free_result(result);







  /* get datafile */

  sprintf(query,"SELECT value FROM %s_option WHERE name='dataFile'",(char *)(*config));
  if(mysql_query(connNum, query) != 0)
  {
    printf("get_run_config: ERROR in mysql_query 3\n");
    dbDisconnect(connNum);
    return;
  }
  if(!(result = mysql_store_result(connNum) ))
  {
    printf("get_run_config: ERROR in mysql_store_result 3\n");
    dbDisconnect(connNum);
    return;
  }
  row_out = mysql_fetch_row(result);
  if(row_out==NULL)
  {
    mysql_free_result(result);
    dbDisconnect(connNum); 
    return;
  }
#ifdef VXWORKS
  *datafile = mystrdup(row_out[0]);
#else
  *datafile = strdup(row_out[0]);
#endif
  mysql_free_result(result);









  dbDisconnect(connNum);

  return;
}
示例#9
0
int ldapLogin(char *thisUid, char *thisPwd, struct threadInfo * ti) {
	extern struct threadInfo *pMainThreadInfo;

	int (*dbConnect)(struct threadStorageInfo *);
	void (*dbDisconnect)(struct threadStorageInfo *);
	int (*dbCookie)(char *, int, struct threadStorageInfo *);
	int (*dbInsert)(char *, int, struct threadStorageInfo *);

	dbConnect = pMainThreadInfo->dbInfo.connect;
	dbDisconnect = pMainThreadInfo->dbInfo.disconnect;
	dbCookie = pMainThreadInfo->dbInfo.cookie;
	dbInsert = pMainThreadInfo->dbInfo.insert;

	if(thisUid == NULL) {
		return(-1);
	}

	if((ti->directoryInfo.ldapUri = configFetch("ldap_uri", &ti->directoryInfo.i)) == NULL) {
		ti->directoryInfo.ldapUri = LDAP_MYURI;
	}

	if((ti->directoryInfo.ldapUsername = configFetch("ldap_username", &ti->directoryInfo.i)) == NULL) {
		ti->directoryInfo.ldapUsername = LDAP_MYUSERNAME;
	}

	if((ti->directoryInfo.ldapPassword = configFetch("ldap_password", &ti->directoryInfo.i)) == NULL) {
		ti->directoryInfo.ldapPassword = LDAP_MYPASSWORD;
	}

	if((ti->directoryInfo.ldapAttr = configFetch("ldap_searchattr", &ti->directoryInfo.i)) == NULL) {
		ti->directoryInfo.ldapAttr = LDAP_MYATTR;
	}

	if((ti->directoryInfo.ldapBase = configFetch("ldap_searchbase", &ti->directoryInfo.i)) == NULL) {
		ti->directoryInfo.ldapBase = LDAP_MYBASE;
	}

	if((ti->directoryInfo.ldapFilter = configFetch("ldap_searchfilter", &ti->directoryInfo.i)) == NULL) {
		ti->directoryInfo.ldapFilter = LDAP_MYFILTER;
	}

	if(ldap_initialize(&ti->directoryInfo.l, ti->directoryInfo.ldapUri) != LDAP_SUCCESS) {
		ldapMessage(ti, ERROR_SLIGHT, "Error occurred while trying to initialize LDAP connection", NULL);

		return(-1);
	}

	ti->directoryInfo.v = LDAP_MYVERSION;

	if(ldap_set_option(ti->directoryInfo.l, LDAP_OPT_PROTOCOL_VERSION, (int *) &ti->directoryInfo.v) != LDAP_OPT_SUCCESS) {
		ldapMessage(ti, ERROR_SLIGHT, "Error occurred while trying to initialize LDAP connection", NULL);

		ldap_unbind(ti->directoryInfo.l);

		return(-1);
	}

	if(ti->directoryInfo.ldapUsername != NULL && ti->directoryInfo.ldapUsername[0] != 0) {
		if(ldap_bind_s(ti->directoryInfo.l, ti->directoryInfo.ldapUsername, ti->directoryInfo.ldapPassword, LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
			ldapMessage(ti, ERROR_SLIGHT, "Error occurred while trying to establish LDAP connection", ti->directoryInfo.ldapUsername);

			ldap_unbind(ti->directoryInfo.l);

			return(-1);
		}
	}

	ti->directoryInfo.timeVal.tv_sec = LDAP_MYTIMEOUT;
	ti->directoryInfo.timeVal.tv_usec = 0;

	if(ldap_search_st(ti->directoryInfo.l, ti->directoryInfo.ldapBase, LDAP_SCOPE_SUBTREE, ti->directoryInfo.ldapFilter, NULL, 0, &ti->directoryInfo.timeVal, &ti->directoryInfo.m) != LDAP_SUCCESS) {
		ldapMessage(ti, ERROR_SLIGHT, "Error occurred while trying to search from LDAP database", ti->directoryInfo.ldapFilter);

		ldap_unbind(ti->directoryInfo.l);

		return(-1);
	}

	if(ldap_count_entries(ti->directoryInfo.l, ti->directoryInfo.m) <= 0) {
		ldapMessage(ti, ERROR_SLIGHT, "Search from LDAP database returned no entries", ti->directoryInfo.ldapFilter);

		ldap_msgfree(ti->directoryInfo.m);
		ldap_unbind(ti->directoryInfo.l);

		return(-1);
	}

	if((ti->directoryInfo.e = ldap_first_entry(ti->directoryInfo.l, ti->directoryInfo.m)) == NULL) {
		ldapMessage(ti, ERROR_SLIGHT, "Search from LDAP database returned no entries", ti->directoryInfo.ldapFilter);

		ldap_msgfree(ti->directoryInfo.m);
		ldap_unbind(ti->directoryInfo.l);

		return(-1);
	}

	if((ti->directoryInfo.d = ldap_get_dn(ti->directoryInfo.l, ti->directoryInfo.e)) == NULL) {
		ldapMessage(ti, ERROR_SLIGHT, "Search from LDAP database returned no entries", ti->directoryInfo.ldapFilter);

		ldap_msgfree(ti->directoryInfo.m);
		ldap_unbind(ti->directoryInfo.l);

		return(-1);
	}

	snprintf(ti->directoryInfo.dnSpace, sizeof(ti->directoryInfo.dnSpace), "%s=%s,%s%c", ti->directoryInfo.ldapAttr, thisUid, ti->directoryInfo.d, 0);

	ldap_memfree(ti->directoryInfo.d);
	ldap_msgfree(ti->directoryInfo.m);

	if(ldap_bind_s(ti->directoryInfo.l, ti->directoryInfo.dnSpace, thisPwd, LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
		ldapMessage(ti, ERROR_SLIGHT, "Authentication against LDAP failed", ti->directoryInfo.dnSpace);

		ldap_unbind(ti->directoryInfo.l);

		return(-1);
	}

	ldap_unbind(ti->directoryInfo.l);

	ti->directoryInfo.l = NULL;

	if(dbConnect(&ti->storageInfo) == 0) {
		if(dbCookie(thisUid, strlen(thisUid), &ti->storageInfo) != 0) {
			ldapMessage(ti, ERROR_SLIGHT, "Authentication against LDAP failed", "Error occurred while trying to create session");

			dbDisconnect(&ti->storageInfo);

			return(-1);
		}

		if(dbInsert(thisUid, strlen(thisUid), &ti->storageInfo) != 0) {
			ldapMessage(ti, ERROR_SLIGHT, "Authentication against LDAP failed", "Error occurred while trying to create session");

			dbDisconnect(&ti->storageInfo);

			return(-1);
		}

		dbDisconnect(&ti->storageInfo);

		return(0);
	}
	else {
		ldapMessage(ti, ERROR_SLIGHT, "Authentication against LDAP failed", "Error occurred while trying to create session");

		return(-1);
	}
}
示例#10
0
static int
TcpServer(void)
{ 
  struct sockaddr_in serverAddr; 
  struct sockaddr_in clientAddr; 
  int sockAddrSize;              /* size of socket address structure */ 
  int sFd;                       /* socket file descriptor */ 
  /*int newFd;*/                     /* socket descriptor from accept */ 
  int ix = 0;                    /* counter for work task names */ 
  int portnum = SERVER_PORT_NUM; /* desired port number; can be changed if that number in use enc */
  char workName[16];             /* name of work task */ 
  int on = TRUE;  /* non-blocking */
  int off = FALSE; /* blocking */
  int status;
  static TWORK targ;
  MYSQL *dbsock = NULL;
  MYSQL_RES *result;
  int numRows;
  char tmp[256], *myname, *hname, *ch;
  
  printf("TcpServer reached\n");fflush(stdout);

#ifdef Linux
  prctl(PR_SET_NAME,"tcpServer");
#endif

  /* some cleanup */
  sockAddrSize = sizeof(struct sockaddr_in); 
  memset((char *)&serverAddr, 0, sockAddrSize); 
  memset((char *)&clientAddr, 0, sockAddrSize); 


  /* creates an endpoint for communication and returns a socket file descriptor */
  if((sFd = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
  {
    perror("socket"); 
    return(ERROR); 
  } 

  /* set up the local address */ 
  serverAddr.sin_family = AF_INET; 
  serverAddr.sin_port = htons(portnum); 
  serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* create a TCP-based socket */ 

  /* bind socket to local address */
  while(bind(sFd, (struct sockaddr *)&serverAddr, sockAddrSize) == ERROR)
  {
    printf("TcpServer: trying port %d\n",portnum);
    perror("TcpServer: bind");

    /* try another port (just increment on one) */
    portnum ++;
    if((portnum-SERVER_PORT_NUM) > 50)
    {
      close(sFd); 
      return(ERROR);
    }

    serverAddr.sin_port = htons(portnum);
  }
  printf("TcpServer: bind on port %d\n",portnum);

  /* create queue for client connection requests */ 
  if(listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR)
  {
    perror ("listen"); 
    close (sFd); 
    return (ERROR);
  }

  myname = localname;
  printf("TcpServer: myname >%s<\n",myname);fflush(stdout);

  /* update daq database 'Ports' table with port number and host name */
  dbsock = dbConnect(getenv("MYSQL_HOST"), "daq");

  /* trying to select our name from 'Ports' table */
  sprintf(tmp,"SELECT Name FROM Ports WHERE Name='%s'",myname);
  if(mysql_query(dbsock, tmp) != 0)
  {
	printf("mysql error (%s)\n",mysql_error(dbsock));
    return(ERROR);
  }

  /* gets results from previous query */
  /* we assume that numRows=0 if our Name does not exist,
     or numRows=1 if it does exist */
  if( !(result = mysql_store_result(dbsock)) )
  {
    printf("ERROR in mysql_store_result (%)\n",mysql_error(dbsock));
    return(ERROR);
  }
  else
  {
    numRows = mysql_num_rows(result);
    mysql_free_result(result);

	hname = getenv("HOST");
	printf("hname befor >%s<\n",hname);
    /* remove everything starting from first dot */
    ch = strstr(hname,".");
    if(ch != NULL) *ch = '\0';
    else ch = hname[strlen(hname)];
	printf("hname after >%s<\n",hname);

    /*printf("nrow=%d\n",numRows);*/
    if(numRows == 0)
    {
      sprintf(tmp,"INSERT INTO Ports (Name,Host,tcpClient_tcp) VALUES ('%s','%s',%d)",
        myname,hname,portnum);
    }
    else if(numRows == 1)
    {
      sprintf(tmp,"UPDATE Ports SET Host='%s',tcpClient_tcp=%d WHERE Name='%s'",hname,portnum,myname);
    }
    else
    {
      printf("ERROR: unknown nrow=%d",numRows);
      return(ERROR);
    }

    if(mysql_query(dbsock, tmp) != 0)
    {
	  printf("ERROR\n");
      return(ERROR);
    }
    else
    {
      printf("Query >%s< succeeded\n",tmp);
    }
  }

  dbDisconnect(dbsock);


  request_in_progress = 0;
  /* accept new connect requests and spawn tasks to process them */ 
  while(1)
  {
    /*printf("inside while loop\n");*/

    /* do not accept new request if current one is not finished yet; too
    many requests may create network buffer shortage */
    if(request_in_progress)
    {
      printf("wait: request in progress\n");
      sleep(1);
      continue;
    }

    /*printf("before accept\n");*/
    if((targ.newFd = accept (sFd, (struct sockaddr *) &clientAddr, &sockAddrSize))
          == ERROR)
    {
      perror ("accept"); 
      close (sFd); 
      return (ERROR); 
    }

    /*printf("accepted request, targ.newFd=%d\n",targ.newFd);*/
    targ.address = inet_ntoa(clientAddr.sin_addr);
    targ.port = ntohs (clientAddr.sin_port);

    sprintf (workName, "tTcpWork%d", ix++);
	/*
usrNetStackSysPoolStatus("tcpServer",1);
usrNetStackDataPoolStatus("tcpServer",1);
	*/
    request_in_progress = 1;
    /* spawn with floating point flag VX_FP_TASK, just in case if some code needs it */
    /*printf("TcpServer: start work thread\n");*/
	{
      int ret;
	  pthread_t id;
      pthread_attr_t detached_attr;

      pthread_attr_init(&detached_attr);
      pthread_attr_setdetachstate(&detached_attr, PTHREAD_CREATE_DETACHED);
      pthread_attr_setscope(&detached_attr, PTHREAD_SCOPE_SYSTEM);
	  /*
      printf("befor: socket=%d address>%s< port=%d\n",
        targ.newFd, targ.address, targ.port); fflush(stdout);
	  */
      /* block annoying IP address(es) */
      /*if(!strncmp((int) inet_ntoa (clientAddr.sin_addr),"129.57.71.",10))*/
      if(!strncmp(targ.address,"129.57.71.",10))
	  {
        printf("WARN: ignore request from %s\n",targ.address);
        close(targ.newFd);
        request_in_progress = 0;
	  }
      else
	  {
        ret = pthread_create(&id, &detached_attr, tcpServerWorkTask, &targ);
        if(ret!=0)
        {
          printf("ERROR: pthread_create(CODAtcpServerWorkTask) returned %d\n",
            ret);
          close(targ.newFd);
          request_in_progress = 0;
        }
	  }
	}
	/*
usrNetStackSysPoolStatus("tcpServer",2);
usrNetStackDataPoolStatus("tcpServer",2);
	*/

    /* sleep 100 msec before processing next request; we do not want to process
    too many requests per minute to avoid network buffers shortage */
    /*sleep(1):*/
  }

} 
示例#11
0
/* our version of 'et_start' */
int
ettStart()
{
  objClass object = localobject;
  ET_priv *etp = (ET_priv *) object->privated;

  size_t hostlen = ET_FILENAME_LENGTH - 1;
  char *ch;

  struct timespec btime = {1, 0}; /* 1 sec */

  int status, ntransferred = 0;
  MYSQL *dbsock;
  char tmp[1000], tmpp[1000];

  printf("ettStart reached\n");

  printf("\n0 etp->exit = %d\n\n",etp->exit);

  /************************************/
  /* default configuration parameters */
  /************************************/
  etp->nevents = 4000;      /* total number of events */
  etp->event_size = 500000; /* size of event in bytes */

  etp->serverPort = 11111;  /* server port number, unique for every ET system on the same machine */
                            /* will NOT use that value, will get free port and put it into database */

  etp->udpPort = 0;
  etp->deleteFile = 1;
  etp->ngroups = 1;    
  etp->noDelay = 0;
  etp->maxNumStations = 0;
  etp->sendBufSize = 0;
  etp->recvBufSize = 0;
  etp->et_verbose = ET_DEBUG_NONE;




  /***************************************************/
  /* extract all necessary information from database */

  /* connect to database */
  dbsock = dbConnect(mysql_host, expid);


  /* get 'from' info */
  sprintf(tmp,"SELECT inputs FROM %s WHERE name='%s'",configname,object->name);
  printf("MYSQL QUERY >%s<\n",tmp);
  if(dbGetStr(dbsock, tmp, tmpp)==ET_ERROR)
  {
    printf("coda_ett: ERROR in mysql query >%s<\n",tmp);
  }
  else
  {
    printf("inputs >%s<\n",tmpp);
  }

  ch = strchr(tmpp,':'); /* get pointer to the location of ':' */
  if(ch==NULL)
  {
    printf("wrong arguments in 'from' - exit\n");
    exit(0);
  }
  ch[0] = '\0'; /* replace ':' by the end of string */

  /* info from database looks like 'ET66:ctof1', so after parsing
     'tmpp' contains 'ET66' and '(ch+1)' points to 'ctof1'; we are
     not using 'ET66' names here, just 'ctof1' as hostname; 'et_from'
     will be set using session name */
  strcpy(etp->host_from,(ch+1));
  /*sprintf(etp->et_from,"/tmp/et_sys_%s",session);*/
  sprintf(etp->et_from,"/et/%s",session);
  printf("host_from >%s<, et_from >%s<\n",etp->host_from,etp->et_from);




  /* get 'to' info */
  sprintf(tmp,"SELECT outputs FROM %s WHERE name='%s'",configname,object->name);
  printf("MYSQL QUERY >%s<\n",tmp);
  if(dbGetStr(dbsock, tmp, tmpp)==ET_ERROR)
  {
    printf("cannot get 'outputs' from table>%s< for the name>%s<\n",configname,object->name);
    return(ET_ERROR);
  }
  else
  {
    printf("outputs >%s<\n",tmpp);
  }
  ch = strchr(tmpp,':'); /* get pointer to the location of ':' */
  if(ch==NULL)
  {
    printf("wrong arguments in 'to' - exit\n");
    exit(0);
  }
  ch[0] = '\0'; /* replace ':' by the end of string */
  strcpy(etp->host_to,(ch+1));
  /*sprintf(etp->et_to,"/tmp/et_sys_%s",session);*/
  sprintf(etp->et_to,"/et/%s",session);
  printf("host_to >%s<, et_to >%s<\n",etp->host_to,etp->et_to);


  /* disconnect from database */
  dbDisconnect(dbsock);


  /* station 'from' */
  strcpy(etp->station,"ETT");




  /* init ET configuration will be used for both ET systems */
  et_open_config_init(&etp->openconfig);


  /*****************/
  /* open 'from' ET */
  /*****************/

  et_open_config_sethost(etp->openconfig, etp->host_from);
  et_open_config_gethost(etp->openconfig, ch);
  printf("host_from >%s<\n",ch);
  /*et_open_config_setport(openconfig, 12345);*/


  /*sergey: increase tcp buffersize 
  {
	int rBufSize;
    int sBufSize;
    int noDelay;

	et_open_config_gettcp(openconfig, &rBufSize, &sBufSize, &noDelay);
    printf("default rBufSize=%d, sBufSize=%d, noDelay=%d\n", rBufSize, sBufSize, noDelay);

	rBufSize = 65000;
    sBufSize = 65000;
    noDelay = 0;
	et_open_config_settcp(openconfig, rBufSize, sBufSize, noDelay);

	et_open_config_gettcp(openconfig, &rBufSize, &sBufSize, &noDelay);
    printf("ett: set rBufSize=%d, sBufSize=%d, noDelay=%d\n", rBufSize, sBufSize, noDelay);
  }
*/

  if(et_open(&etp->id_from, etp->et_from, etp->openconfig) != ET_OK)
  {
    printf("et_open 'from' problems\n");
    exit(1);
  }


  /******************/
  /* open 'to' ET */
  /******************/

  /*!!!!!!!!!!!!!!!! ET_HOST_ANYWHERE works, to_node does not !!!!!!!!*/
  et_open_config_sethost(etp->openconfig, etp->host_to/*ET_HOST_ANYWHERE*/);
  et_open_config_gethost(etp->openconfig, ch);
  printf("host_to >%s<\n",ch);

  /* Always use 'direct' connection: you can connect to any subnet,
     but only one ET system allowed on remote machine */
  et_open_config_setcast(etp->openconfig, ET_DIRECT);






  /*sergey: increase tcp buffersize 
  {
	int rBufSize;
    int sBufSize;
    int noDelay;

	et_open_config_gettcp(openconfig, &rBufSize, &sBufSize, &noDelay);
    printf("default rBufSize=%d, sBufSize=%d, noDelay=%d\n", rBufSize, sBufSize, noDelay);

	rBufSize = 65000;
    sBufSize = 65000;
    noDelay = 0;
	et_open_config_settcp(openconfig, rBufSize, sBufSize, noDelay);

	et_open_config_gettcp(openconfig, &rBufSize, &sBufSize, &noDelay);
    printf("ett: set rBufSize=%d, sBufSize=%d, noDelay=%d\n", rBufSize, sBufSize, noDelay);
  }
*/


  /*
  {
    et_open_config *config = (et_open_config *) openconfig;
    printf("befor et_open: rBufSize=%d, sBufSize=%d, noDelay=%d\n",
      config->tcpSendBufSize,config->tcpRecvBufSize,config->tcpNoDelay);
  }
  */


  if(et_open(&etp->id_to, etp->et_to, etp->openconfig) != ET_OK)
  {
    printf("et_open 'to' problems\n");
    exit(1);
  }

  {
    et_id *idto = (et_id *) etp->id_to;
    printf("11111: idto->endian=0x%08x idto->systemendian=0x%08x\n",idto->endian,idto->systemendian);
  }

printf("11\n");fflush(stdout);
  /* destroy configuration */
  et_open_config_destroy(etp->openconfig);

  /* init station configuration */
  et_station_config_init(&etp->sconfig);
  et_station_config_setuser(etp->sconfig, ET_STATION_USER_MULTI);
  et_station_config_setrestore(etp->sconfig, ET_STATION_RESTORE_OUT);
  et_station_config_setprescale(etp->sconfig, 1);
  et_station_config_setcue(etp->sconfig, 150);
 
printf("12\n");fflush(stdout);

  /* ET system "all" mode */
  et_station_config_setselect(etp->sconfig, ET_STATION_SELECT_ALL);
  et_station_config_setblock(etp->sconfig, ET_STATION_BLOCKING);

  /* ET system "on req" mode 
  et_station_config_setselect(etp->sconfig, ET_STATION_SELECT_ALL);
  et_station_config_setblock(etp->sconfig, ET_STATION_NONBLOCKING);
  */
  /* ET system "condition" mode 
  et_station_config_setselect(etp->sconfig, ET_STATION_SELECT_MATCH);
  et_station_config_setblock(setp->config, ET_STATION_BLOCKING);
  et_station_config_setselectwords(etp->sconfig, selections);
  */
  /* new non-blocking "condition" mode 
  et_station_config_setselect(etp->sconfig, ET_STATION_SELECT_MATCH);
  et_station_config_setblock(etp->sconfig, ET_STATION_NONBLOCKING);
  et_station_config_setselectwords(etp->sconfig, selections);
  */
  /* user's condition, blocking  mode 
  et_station_config_setselect(etp->sconfig, ET_STATION_SELECT_USER);
  et_station_config_setblock(etp->sconfig, ET_STATION_BLOCKING);
  et_station_config_setselectwords(etp->sconfig, selections);
  if (et_station_config_setfunction(etp->sconfig, "et_carls_function") == ET_ERROR) {
	printf("%s: cannot set function\n", argv[0]);
	exit(1);
  }
  if (et_station_config_setlib(etp->sconfig, "/home/timmer/cvs/coda/source/et/src/libet_user.so") == ET_ERROR) {
    printf("%s: cannot set library\n", argv[0]);
	exit(1);
  }
  */
  /* user's condition, nonblocking mode 
  et_station_config_setselect(etp->sconfig, ET_STATION_SELECT_USER);
  et_station_config_setblock(etp->sconfig, ET_STATION_NONBLOCKING);
  et_station_config_setselectwords(etp->sconfig, selections);
  et_station_config_setfunction(etp->sconfig, "et_carls_function");
  et_station_config_setlib(etp->sconfig, "/home/timmer/cvs/coda/source/et/src/libet_user.so");
  */
  
  /* set debug level */
  et_system_setdebug(etp->id_from, ET_DEBUG_INFO);
  et_system_setdebug(etp->id_to,   ET_DEBUG_INFO);

  if ((status = et_station_create(etp->id_from, &etp->stat_from, etp->station, etp->sconfig)) < ET_OK)
  {
    if (status == ET_ERROR_EXISTS)
    {
      /* my_stat contains pointer to existing station */;
      printf("station already exists, will attach to it\n");

      /* get id and attach to existing station (must be created by 'et_start' */
      if((status = et_station_name_to_id(etp->id_from, &etp->stat_from, etp->station)) < ET_OK)
      {
        printf("error in station_name_to_id\n");
        exit(0);
      }
    }
    else
    {
      printf("error in station creation\n");
      exit(0);
    }
  }

  et_station_config_destroy(etp->sconfig);
printf("13\n");fflush(stdout);





  /* */
  if (et_station_attach(etp->id_from, etp->stat_from, &etp->att_from) < 0)
  {
    printf("error in station attach\n");
    exit(0);
  }
printf("14\n");fflush(stdout);

  if (et_station_attach(etp->id_to, ET_GRANDCENTRAL, &etp->att_to) < 0)
  {
    printf("error in station attach\n");
    exit(0);
  }
printf("15\n");fflush(stdout);

  et_bridge_config_init(&etp->bconfig);

  /*et_bridge_config_setfunc(etp->bconfig, et_bridge_CODAswap);*/


  /* set timeouts - does not do anything ???
  if (et_bridge_config_settimeoutfrom(etp->bconfig, btime) < 0)
  {
    printf("error et_bridge_config_settimeoutfrom\n");
    exit(0);
  }

  if (et_bridge_config_settimeoutto(etp->bconfig, btime) < 0)
  {
    printf("error et_bridge_config_settimeoutto\n");
    exit(0);
  }
  */

  printf("1 etp->exit = %d\n",etp->exit);
  printf("1 etp->exit = %d\n",etp->exit);
  printf("1 etp->exit = %d\n",etp->exit);

  /* infinite loop */
  status = ET_OK;
  while((status == ET_OK) && (etp->exit == 0))
  {
	printf("-> status=%d etp->exit=%d\n",status,etp->exit);
    status = et_events_bridge(etp->id_from, etp->id_to, etp->att_from, etp->att_to,
			      etp->bconfig, NUMEVENTS, &ntransferred);
  }

  printf("2 etp->exit = %d\n",etp->exit);
  printf("2 etp->exit = %d\n",etp->exit);
  printf("2 etp->exit = %d\n",etp->exit);

  /* cleanup */
  et_bridge_config_destroy(etp->bconfig);
  et_forcedclose(etp->id_from);
  et_forcedclose(etp->id_to);

  etp->exit = 0;

  return(ET_OK);
}
示例#12
0
char *fetchAccess(int getThis, int getType, struct threadInfo * ti) {
	int (*dbConnect)(struct threadStorageInfo *);
	void (*dbDisconnect)(struct threadStorageInfo *);
	void (*dbEscape)(struct threadStorageInfo *, char *, char *);
	void (*dbPush)(struct threadStorageInfo *, char *);
	char *(*dbPull)(struct threadStorageInfo *, char *);
	void (*dbFree)(struct threadStorageInfo *);

	dbConnect = ti->dbInfo.connect;
	dbDisconnect = ti->dbInfo.disconnect;
	dbEscape = ti->dbInfo.escape;
	dbPush = ti->dbInfo.push;
	dbPull = ti->dbInfo.pull;
	dbFree = ti->dbInfo.free;

	ti->commandInfo.esc1Buffer = NULL;
	ti->commandInfo.esc2Buffer = NULL;
	ti->commandInfo.esc3Buffer = NULL;
	ti->commandInfo.esc4Buffer = NULL;
	ti->commandInfo.esc5Buffer = NULL;

	// Allocate memory for buffers
	ti->commandInfo.s =
		(ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) +
		CONFIG_SPACE_SIZE;

	if((ti->commandInfo.statBuffer = malloc(ti->commandInfo.s + 1)) == NULL) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

		return(ti->dataBuffer);
	}

	// Escape parameters received from the client
	if(ti->handlerArrays[HANDLER_ARRAY_UID].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_UID].buffer[0] != 0) {
		if((ti->commandInfo.esc1Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc1Buffer, ti->handlerArrays[HANDLER_ARRAY_UID].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer[0] != 0) {
		if((ti->commandInfo.esc2Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc2Buffer, ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer[0] != 0) {
		if((ti->commandInfo.esc3Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc3Buffer, ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] != 0) {
		if((ti->commandInfo.esc4Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc4Buffer, ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer[0] != 0) {
		if((ti->commandInfo.esc5Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);
			free(ti->commandInfo.esc4Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc5Buffer, ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer);
	}

	// Connect to database
	if(dbConnect(&ti->storageInfo) != 0) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_CONNECTFAILED, ERROR_MESS_DATABASE_CONNECTFAILED, ti);
	}
	else {
		// Construct SQL statement for requested item
		switch(getThis) {
			case 1:
				// Change own password
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_USERS " SET " TABLECOL_USER_PWD " = " TABLE_PWD_HASH "('%s') WHERE " TABLECOL_USER_UID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 2:
				// Change user's password
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_USERS " SET " TABLECOL_USER_PWD " = " TABLE_PWD_HASH "('%s') WHERE " TABLECOL_USER_UID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 3:
				// Check if requestor is admin or not
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_USER_PERM " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '' AND " TABLECOL_USER_GRP " = '' AND " TABLECOL_USER_PERM " = '" PRIVILEGE_LEVEL_ADMIN_S "'%c",
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 4:
				// Get user's default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT MAX(" TABLECOL_USER_PERM ") AS " TABLECOL_USER_PERM " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '' AND " TABLECOL_USER_GRP " = ''%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 5:
				// Create new user privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_PERM_NODES " (" TABLEKEY_USER_PERM ") VALUES((SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s'), '', '%s', '', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 6:
				// Create new user default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_PERM_NODES " (" TABLEKEY_USER_PERM ") VALUES((SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s'), '', '', '', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 7:
				// Modify existing user privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_PERM_NODES " SET " TABLECOL_USER_PERM " = '%s' WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '%s'%c",
					ti->commandInfo.esc5Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 8:
				// Modify existing user default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_PERM_NODES " SET " TABLECOL_USER_PERM " = '%s' WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = ''%c",
					ti->commandInfo.esc5Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 9:
				// Check user default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_USER_USID " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_PERM " = '%s' AND " TABLECOL_USER_DOMAIN " = ''%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 10:
				// Delete existing user privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 11:
				// Get user's privilege level for domain
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT MAX(" TABLECOL_USER_PERM ") AS " TABLECOL_USER_PERM " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		// Pull data out from database
		switch(getType) {
			case QUERY_TYPE_PULL:
				if(dbPull(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			case QUERY_TYPE_PUSH:
				dbPush(&ti->storageInfo, ti->commandInfo.statBuffer);

				snprintf(
					ti->logSpace,
					sizeof(ti->logSpace),
					"User \"%s\" modified by \"%s\" using command \"%s\"%c",
					ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer,
					ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
					ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
					0
				);

				logMessage(ti->logSpace);

				replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		dbDisconnect(&ti->storageInfo);
	}

	// Free allocated resources and reply to the client
	if(ti->commandInfo.esc1Buffer != NULL) {
		free(ti->commandInfo.esc1Buffer);
	}

	if(ti->commandInfo.esc2Buffer != NULL) {
		free(ti->commandInfo.esc2Buffer);
	}

	if(ti->commandInfo.esc3Buffer != NULL) {
		free(ti->commandInfo.esc3Buffer);
	}

	if(ti->commandInfo.esc4Buffer != NULL) {
		free(ti->commandInfo.esc4Buffer);
	}

	if(ti->commandInfo.esc5Buffer != NULL) {
		free(ti->commandInfo.esc5Buffer);
	}

	free(ti->commandInfo.statBuffer);

	return(ti->dataBuffer);
}
示例#13
0
int
codaDownload(char *conf)
{
  objClass object = localobject;

  MONp monp = (MONp) object->privated;
  int deflt = 0;
  static char tmp[1000];
  static char tmp2[1000];
  int  ix;  
  int  listArgc;
  char listArgv[LISTARGV1][LISTARGV2];

  MYSQL *dbsock;
  char tmpp[1000];

  
  monp->object = object;


  /***************************************************/
  /* extract all necessary information from database */


  /*****************************/
  /*****************************/
  /* FROM CLASS (former conf1) */

  strcpy(configname,conf); /* Sergey: save CODA configuration name */

  UDP_start();

  /* connect to database */
  dbsock = dbConnect(getenv("MYSQL_HOST"), expid);

  sprintf(tmp,"SELECT value FROM %s_option WHERE name='SPLITMB'",
    configname);
  if(dbGetInt(dbsock, tmp, &monp->split)==MON_ERROR)
  {
    monp->split = 2047 << 20;
    printf("cannot get SPLITMB, set monp->split=%d\n",monp->split);
  }
  else
  {
    printf("get monp->split = %d\n",monp->split);
    monp->split = monp->split<<20;
    printf("set monp->split = %d\n",monp->split);
  }

  sprintf(tmp,"SELECT value FROM %s_option WHERE name='RECL'",
    configname);
  if(dbGetInt(dbsock, tmp, &monp->record_length)==MON_ERROR)
  {
    monp->record_length = 32768;
    printf("cannot get RECL, set monp->record_length=%d\n",monp->record_length);
  }
  else
  {
    printf("get monp->record_length = %d\n",monp->record_length);
  }


  sprintf(tmp,"SELECT value FROM %s_option WHERE name='EvDumpLevel'",
    configname);
  if(dbGetInt(dbsock, tmp, &monp->log_EvDumpLevel)==MON_ERROR)
  {
    monp->log_EvDumpLevel = 0;
    printf("cannot get EvDumpLevel, set monp->log_EvDumpLevel=%d\n",monp->log_EvDumpLevel);
  }
  else
  {
    printf("get monp->log_EvDumpLevel = %d\n",monp->log_EvDumpLevel);
  }


  /* do not nned that !!!???
  sprintf(tmp,"SELECT inputs FROM %s WHERE name='%s'",configname,object->name);
  if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
  {
    printf("cannot get 'inputs' from table>%s< for the name>%s<\n",configname,object->name);
  }
  else
  {
    printf("inputs >%s<\n",tmpp);
  }
  */

  sprintf(tmp,"SELECT outputs FROM %s WHERE name='%s'",configname,object->name);
  if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
  {
    printf("cannot get 'outputs' from table>%s< for the name>%s<\n",configname,object->name);
    return(MON_ERROR);
  }
  else
  {
    strcpy(monp->output_type,tmpp);
    printf("get monp->output_type >%s<\n",monp->output_type);
  }

  /* default output to none */
  monp->output_switch = 3;

  if( !strncmp(monp->output_type,"file",4) ) /* output to binary file */
  {
    sprintf(tmp,"SELECT value FROM %s_option WHERE name='dataFile'",configname);
    if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
    {
      printf("cannot get 'dataFile' from table >%s_option<\n",configname);
      return(MON_ERROR);
    }
    else
    {
      monp->filename = strdup(tmpp); /* Sergey: change it to strcpy(monp->filename,tmpp);*/
      printf("get monp->filename >%s<\n",monp->filename);
    }
    monp->output_switch = 1;
  }
  else if( !strncmp(monp->output_type,"debug",5) ) /* debug dump */
  {
    monp->output_switch = 2;
  }
  else if( !strncmp(monp->output_type,"none",4) ) /* output to /dev/null */
  {
    monp->output_switch = 3;
  }
  else if( !strncmp(monp->output_type,"coda",4) ) /* output in CODA format */
  {
    sprintf(tmp,"SELECT value FROM %s_option WHERE name='dataFile'",configname);
    if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
    {
      printf("cannot get 'dataFile' from table >%s_option<\n",configname);
      return(MON_ERROR);
    }
    else
    {
      monp->filename = strdup(tmpp); /* Sergey: change it to strcpy(monp->filename,tmpp);*/
      printf("get monp->filename >%s<\n",monp->filename);
    }
  	printf("coda format will be used\n");
    monp->output_switch = 4;
  }
  else
  {
    printf("invalid monp->output_type >%s<\n",monp->output_type);
    return(MON_ERROR);
  }

  /*****************************/
  /*****************************/
  /*****************************/

  monp->fd = -1;

  if (monp->mod_id) {
    printf("INFO: Unloading module %x\n", monp->mod_id);

#if defined __sun||LINUX
    if (dlclose ((void *) monp->mod_id) != 0)
    {
      printf("ERROR: failed to unload module to decode >%s<\n",monp->mod_name);
      return MON_ERROR;
    }
#else
    printf("WARN: dynamic loading not yet supported on this platform\n");
#endif
  }
  
  printf("INFO: Downloading configuration '%s'\n", configname);

  strcpy(monp->mod_name,"CODA");


  /* Get the list of readout-lists from the database */
  sprintf(tmpp,"SELECT code FROM %s WHERE name='%s'",configname,object->name);
  if(dbGetStr(dbsock, tmpp, tmp)==MON_ERROR) return(MON_ERROR);
printf("++++++======>%s<\n",tmp);


  /* disconnect from database */
  dbDisconnect(dbsock);


  /* Decode configuration string */
  listArgc = 0;
  if(!((strcmp (tmp, "{}") == 0)||(strcmp (tmp, "") == 0)))
  {
    if(listSplit1(tmp, 1, &listArgc, listArgv)) return(MON_ERROR);
    for(ix=0; ix<listArgc; ix++) printf("nrols [%1d] >%s<\n",ix,listArgv[ix]);
  }
  else
  {
    printf("download: do not split list >%s<\n",tmp);
  }


  /* Get object filename in order to find the ROLs __init routine */
  if(listArgc)
  {
    strcpy(monp->mod_name,listArgv[0]);

    /* Load the decode module */
    sprintf(tmp,"%s/%s_file.so",getenv("CODA_LIB"),listArgv[0]);
#if defined __sun||LINUX
    if (monp->mod_id) dlclose(monp->mod_id);
    monp->mod_id = dlopen ((const char *) tmp, RTLD_NOW | RTLD_GLOBAL);
    if (monp->mod_id == 0) {
      printf("WARN: dlopen failed to open >%s<\n",tmp);
      printf("WARN: >%s<\n",dlerror());
      deflt = 1;
    }
#else
    printf("WARN: dynamic loading not supported\n");
    deflt = 1;
#endif
    
  }
  else
  {
    deflt = 1;
    printf("WARN: row %s table %s no code entry, use CODA file fmt.\n",
      object->name,configname);
  }


  /******************************************************************/
  /* Now look up the routines in the library and fill in the tables */
  if(deflt)
  {
    /* default to CODA format */
    printf("INFO: Using inbuilt (CODA) format\n");
	/*
    monp->open_proc = CODA_open_file;
    monp->close_proc = CODA_close_file;
    monp->write_proc = CODA_write_event;
    */
  }
  else
  {
    IFUNCPTR proc;
    /* find input formatting procs */
    sprintf(tmp,"%s_open_file",monp->mod_name);
    proc = (IFUNCPTR) dlsym (monp->mod_id, tmp);
    monp->open_proc = proc;
    sprintf(tmp,"%s_close_file",monp->mod_name);
    proc = (IFUNCPTR) dlsym (monp->mod_id, tmp);
    monp->close_proc = proc;
    sprintf(tmp,"%s_write_event",monp->mod_name);
    proc = (IFUNCPTR) dlsym (monp->mod_id, tmp);
    monp->write_proc = proc;

    printf("INFO: Loaded module for format %s\n",monp->mod_name);
  }

  /* If we need to initialize, reinitialize, or
   * if et_alive fails on Linux, then initialize.
   */
  if( (et_init == 0)   ||
      (et_reinit == 1) ||
      ((!et_alive(et_sys)) && (et_locality == ET_LOCAL_NOSHARE))
     )
  {
    if(mon_et_initialize() != MON_OK)
    {
      printf("ERROR: mon download: cannot initalize ET system\n");
      return(MON_ERROR);
    }
  }

  codaUpdateStatus("downloaded");

  return(MON_OK);
}