Exemple #1
0
static void inotify_callback(char* path, int event) {
  if (event & IN_CREATE || event & IN_MOVED_TO) {
    output("CREATE\n%s\nCHANGE\n%s\n", path, path);
    userlog(LOG_DEBUG, "CREATE: %s", path);
    return;
  }

  if (event & IN_MODIFY) {
    output("CHANGE\n%s\n", path);
    userlog(LOG_DEBUG, "CHANGE: %s", path);
    return;
  }

  if (event & IN_ATTRIB) {
    output("STATS\n%s\n", path);
    userlog(LOG_DEBUG, "STATS: %s", path);
    return;
  }

  if (event & IN_DELETE || event & IN_MOVED_FROM) {
    output("DELETE\n%s\n", path);
    userlog(LOG_DEBUG, "DELETE: %s", path);
    return;
  }

  if (event & IN_UNMOUNT) {
    output("RESET\n");
    userlog(LOG_DEBUG, "RESET");
    return;
  }
}
Exemple #2
0
static void rm_watch(int wd, bool update_parent) {
  watch_node* node = table_get(watches, wd);
  if (node == NULL) {
    return;
  }

  userlog(LOG_DEBUG, "unwatching %s: %d (%p)", node->path, node->wd, node);

  if (inotify_rm_watch(inotify_fd, node->wd) < 0) {
    userlog(LOG_DEBUG, "inotify_rm_watch(%d:%s): %s", node->wd, node->path, strerror(errno));
  }

  for (int i=0; i<array_size(node->kids); i++) {
    watch_node* kid = array_get(node->kids, i);
    if (kid != NULL) {
      rm_watch(kid->wd, false);
    }
  }

  if (update_parent && node->parent != NULL) {
    for (int i=0; i<array_size(node->parent->kids); i++) {
      if (array_get(node->parent->kids, i) == node) {
        array_put(node->parent->kids, i, NULL);
        break;
      }
    }
  }

  array_delete(node->kids);
  free(node);
  table_put(watches, wd, NULL);
}
Exemple #3
0
static bool register_roots(array* new_roots, array* unwatchable) {
  for (int i=0; i<array_size(new_roots); i++) {
    char* new_root = array_get(new_roots, i);
    userlog(LOG_INFO, "registering root: %s", new_root);
    int id = watch(new_root, unwatchable);
    if (id == ERR_ABORT) {
      return false;
    }
    else if (id >= 0) {
      watch_root* root = malloc(sizeof(watch_root));
      CHECK_NULL(root, false);
      root->id = id;
      root->name = new_root;
      CHECK_NULL(array_push(roots, root), false);
    }
    else {
      if (show_warning && watch_limit_reached()) {
        int limit = get_watch_count();
        userlog(LOG_WARNING, "watch limit (%d) reached", limit);
        output("MESSAGE\n" INOTIFY_LIMIT_MSG, limit);
        show_warning = false;  // warn only once
      }
      CHECK_NULL(array_push(unwatchable, new_root), false);
    }
  }

  return true;
}
Exemple #4
0
static bool unwatchable_mounts(array* mounts) {
  FILE* mtab = fopen("/etc/mtab", "r");
  if (mtab == NULL) {
    mtab = fopen("/proc/mounts", "r");
  }
  if (mtab == NULL) {
    userlog(LOG_ERR, "neither /etc/mtab nor /proc/mounts can be read");
    return false;
  }

  char* line;
  while ((line = read_line(mtab)) != NULL) {
    userlog(LOG_DEBUG, "mtab: %s", line);
    char* dev = strtok(line, MTAB_DELIMS);
    char* point = strtok(NULL, MTAB_DELIMS);
    char* fs = strtok(NULL, MTAB_DELIMS);

    if (dev == NULL || point == NULL || fs == NULL) {
      userlog(LOG_ERR, "can't parse mount line");
      return false;
    }

    if (!is_watchable(dev, point, fs)) {
      CHECK_NULL(array_push(mounts, strdup(point)), false);
    }
  }

  fclose(mtab);
  return true;
}
Exemple #5
0
static bool read_input() {
  char* line = read_line(stdin);
  userlog(LOG_DEBUG, "input: %s", (line ? line : "<null>"));

  if (line == NULL || strcmp(line, "EXIT") == 0) {
    userlog(LOG_INFO, "exiting: %s", line);
    return false;
  }

  if (strcmp(line, "ROOTS") == 0) {
    array* new_roots = array_create(20);
    CHECK_NULL(new_roots, false);

    while (1) {
      line = read_line(stdin);
      userlog(LOG_DEBUG, "input: %s", (line ? line : "<null>"));
      if (line == NULL || strlen(line) == 0) {
        return false;
      }
      else if (strcmp(line, "#") == 0) {
        break;
      }
      else {
        int l = strlen(line);
        if (l > 1 && line[l-1] == '/')  line[l-1] = '\0';
        CHECK_NULL(array_push(new_roots, strdup(line)), false);
      }
    }

    return update_roots(new_roots);
  }

  userlog(LOG_INFO, "unrecognised command: %s", line);
  return true;
}
Exemple #6
0
bool process_inotify_input() {
  ssize_t len = read(inotify_fd, event_buf, EVENT_BUF_LEN);
  if (len < 0) {
    userlog(LOG_ERR, "read: %s", strerror(errno));
    return false;
  }

  int i = 0;
  while (i < len) {
    struct inotify_event* event = (struct inotify_event*) &event_buf[i];
    i += EVENT_SIZE + event->len;

    if (event->mask & IN_IGNORED) {
      continue;
    }
    if (event->mask & IN_Q_OVERFLOW) {
      userlog(LOG_INFO, "event queue overflow");
      continue;
    }

    if (!process_inotify_event(event)) {
      return false;
    }
  }

  return true;
}
Exemple #7
0
bool init_inotify() {
  inotify_fd = inotify_init();
  if (inotify_fd < 0) {
    int e = errno;
    userlog(LOG_ERR, "inotify_init: %s", strerror(e));
    if (e == EMFILE) {
      message(MSG_INSTANCE_LIMIT);
    }
    return false;
  }
  userlog(LOG_DEBUG, "inotify fd: %d", get_inotify_fd());

  read_watch_descriptors_count();
  if (watch_count <= 0) {
    close(inotify_fd);
    inotify_fd = -1;
    return false;
  }
  userlog(LOG_INFO, "inotify watch descriptors: %d", watch_count);

  watches = table_create(watch_count);
  if (watches == NULL) {
    userlog(LOG_ERR, "out of memory");
    close(inotify_fd);
    inotify_fd = -1;
    return false;
  }

  return true;
}
static int add_watch(const char* path, watch_node* parent) {
  int wd = inotify_add_watch(inotify_fd, path, IN_MODIFY | IN_ATTRIB | IN_CREATE | IN_DELETE | IN_MOVE | IN_DELETE_SELF);
  if (wd < 0) {
    if (errno == ENOSPC) {
      limit_reached = true;
    }
    userlog(LOG_ERR, "inotify_add_watch(%s): %s", path, strerror(errno));
    return ERR_CONTINUE;
  }
  else {
    userlog(LOG_DEBUG, "watching %s: %d", path, wd);
  }

  watch_node* node = table_get(watches, wd);
  if (node != NULL) {
    if (node->wd != wd || strcmp(node->name, path) != 0) {
      char buf1[PATH_MAX], buf2[PATH_MAX];
      const char* normalized1 = realpath(node->name, buf1);
      const char* normalized2 = realpath(path, buf2);
      if (normalized1 == NULL || normalized2 == NULL || strcmp(normalized1, normalized2) != 0) {
        userlog(LOG_ERR, "table error: collision at %d (new %s, existing %s)", wd, path, node->name);
        return ERR_ABORT;
      }
      else {
        userlog(LOG_WARNING, "intersection at %d: (new %s, existing %s, real %s)", wd, path, node->name, normalized1);
        return ERR_IGNORE;
      }
    }

    return wd;
  }

  node = malloc(sizeof(watch_node));

  CHECK_NULL(node);
  node->name = strdup(path);
  CHECK_NULL(node->name);
  node->wd = wd;
  node->parent = parent;
  node->kids = NULL;

  if (parent != NULL) {
    if (parent->kids == NULL) {
      parent->kids = array_create(DEFAULT_SUBDIR_COUNT);
      CHECK_NULL(parent->kids);
    }
    CHECK_NULL(array_push(parent->kids, node));
  }

  if (table_put(watches, wd, node) == NULL) {
    userlog(LOG_ERR, "table error: unable to put (%d:%s)", wd, path);
    return ERR_ABORT;
  }

  return wd;
}
Exemple #9
0
static int walk_tree(int path_len, watch_node* parent, bool recursive, array* mounts) {
  for (int j=0; j<array_size(mounts); j++) {
    char* mount = array_get(mounts, j);
    if (strncmp(path_buf, mount, strlen(mount)) == 0) {
      userlog(LOG_DEBUG, "watch path '%s' crossed mount point '%s' - skipping", path_buf, mount);
      return ERR_IGNORE;
    }
  }

  DIR* dir = NULL;
  if (recursive) {
    if ((dir = opendir(path_buf)) == NULL) {
      if (errno == EACCES || errno == ENOENT || errno == ENOTDIR) {
        userlog(LOG_DEBUG, "opendir(%s): %d", path_buf, errno);
        return ERR_IGNORE;
      }
      else {
        userlog(LOG_ERR, "opendir(%s): %s", path_buf, strerror(errno));
        return ERR_CONTINUE;
      }
    }
  }

  int id = add_watch(path_len, parent);

  if (dir == NULL) {
    return id;
  }
  else if (id < 0) {
    closedir(dir);
    return id;
  }

  path_buf[path_len] = '/';

  struct dirent* entry;
  while ((entry = readdir(dir)) != NULL) {
    if (entry->d_type != DT_DIR ||
        strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
      continue;
    }

    int name_len = strlen(entry->d_name);
    memcpy(path_buf + path_len + 1, entry->d_name, name_len + 1);

    int subdir_id = walk_tree(path_len + 1 + name_len, table_get(watches, id), recursive, mounts);
    if (subdir_id < 0 && subdir_id != ERR_IGNORE) {
      rm_watch(id, true);
      id = subdir_id;
      break;
    }
  }

  closedir(dir);
  return id;
}
Exemple #10
0
int main(int argc, char** argv) {
  if (argc > 1) {
    if (strcmp(argv[1], "--help") == 0) {
      printf(USAGE_MSG);
      return 0;
    }
    else if (strcmp(argv[1], "--version") == 0) {
      printf(VERSION_MSG);
      return 0;
    }
    else if (strcmp(argv[1], "--selftest") == 0) {
      self_test = true;
    }
    else {
      printf("unrecognized option: %s\n", argv[1]);
      printf(HELP_MSG);
      return 1;
    }
  }

  init_log();
  if (!self_test) {
    userlog(LOG_INFO, "started (v." VERSION ")");
  }
  else {
    userlog(LOG_INFO, "started (self-test mode) (v." VERSION ")");
  }

  setvbuf(stdin, NULL, _IONBF, 0);
  setvbuf(stdout, NULL, _IONBF, 0);

  roots = array_create(20);
  if (init_inotify() && roots != NULL) {
    set_inotify_callback(&inotify_callback);

    if (!self_test) {
      main_loop();
    }
    else {
      run_self_test();
    }

    unregister_roots();
  }
  else {
    printf("GIVEUP\n");
  }
  close_inotify();
  array_delete(roots);

  userlog(LOG_INFO, "finished");
  closelog();

  return 0;
}
Exemple #11
0
/*
 * Do the test call to the server
 */
int main(int argc, char** argv) {
    int ret=SUCCEED;

    if (SUCCEED!=(ret=userlog("HELLO WORLD FROM USER LOG!!!  %d-%s", 123, "PARAM")))
    {
        goto out;
    }
    if (SUCCEED!=(ret=userlog("This is log file line 2")))
    {
        goto out;
    }

out:
    return ret;
}
Exemple #12
0
static void read_watch_descriptors_count() {
  FILE* f = fopen(WATCH_COUNT_NAME, "r");
  if (f == NULL) {
    userlog(LOG_ERR, "can't open %s: %s", WATCH_COUNT_NAME, strerror(errno));
    return;
  }

  char* str = read_line(f);
  if (str == NULL) {
    userlog(LOG_ERR, "can't read from %s", WATCH_COUNT_NAME);
  }
  else {
    watch_count = atoi(str);
  }

  fclose(f);
}
Exemple #13
0
static void unregister_roots() {
  watch_root* root;
  while ((root = array_pop(roots)) != NULL) {
    userlog(LOG_INFO, "unregistering root: %s", root->name);
    unwatch(root->id);
    free(root->name);
    free(root);
  };
}
Exemple #14
0
static bool update_roots(array* new_roots) {
  userlog(LOG_INFO, "updating roots (curr:%d, new:%d)", array_size(roots), array_size(new_roots));

  unregister_roots();

  if (array_size(new_roots) == 0) {
    output("UNWATCHEABLE\n#\n");
    array_delete(new_roots);
    return true;
  }
  else if (array_size(new_roots) == 1 && strcmp(array_get(new_roots, 0), "/") == 0) {  // refuse to watch entire tree
    output("UNWATCHEABLE\n/\n#\n");
    userlog(LOG_INFO, "unwatchable: /");
    array_delete_vs_data(new_roots);
    return true;
  }

  array* mounts = unwatchable_mounts();
  if (mounts == NULL) {
    return false;
  }

  array* unwatchable = array_create(20);
  if (!register_roots(new_roots, unwatchable, mounts)) {
    return false;
  }

  output("UNWATCHEABLE\n");
  for (int i=0; i<array_size(unwatchable); i++) {
    char* s = array_get(unwatchable, i);
    output("%s\n", s);
    userlog(LOG_INFO, "unwatchable: %s", s);
  }
  output("#\n");

  array_delete_vs_data(unwatchable);
  array_delete_vs_data(mounts);
  array_delete_vs_data(new_roots);

  return true;
}
Exemple #15
0
static array* unwatchable_mounts() {
  FILE* mtab = setmntent(_PATH_MOUNTED, "r");
  if (mtab == NULL) {
    userlog(LOG_ERR, "cannot open " _PATH_MOUNTED);
    return NULL;
  }

  array* mounts = array_create(20);
  CHECK_NULL(mounts, NULL);

  struct mntent* ent;
  while ((ent = getmntent(mtab)) != NULL) {
    userlog(LOG_DEBUG, "mtab: %s : %s", ent->mnt_dir, ent->mnt_type);
    if (strcmp(ent->mnt_type, MNTTYPE_IGNORE) != 0 && !is_watchable(ent->mnt_type)) {
      CHECK_NULL(array_push(mounts, strdup(ent->mnt_dir)), NULL);
    }
  }

  endmntent(mtab);
  return mounts;
}
Exemple #16
0
extern "C" int LIBTUX_EXPORT _tmstartserver(int argc, char **argv, TMSVRARGS_T* tmsvrargs)
{
	int nRet = 0;

	// 初始化Tuxedo系统
	TPINIT tpinfo;
	memset(&tpinfo, 0, sizeof(tpinfo));

	// 试图创建AppContext对象
	if(!g_pAppContext)
	{
		g_pAppContext = new AppContext();
	}

	// 初始化Tuxedo系统
	nRet = g_pAppContext->tpinit(&tpinfo);
	if(nRet != 0)
	{
		logger->error(LTRACE, "tpinit error");
		return -1;
	}

	// 作为Server,注册服务
	nRet = g_pAppContext->tpregister(tmsvrargs->tmdsptchtbl, tmsvrargs->tmdsptchtbllen, 0);
	if(nRet != 0)
	{
		logger->error(LTRACE, "Register service error");
		return -1;
	}

	// 初始化用户应用
	nRet = tpsvrinit(argc, argv);
	if(nRet != 0)
	{
		userlog("tpsvrinit failed");
		return -1;
	}

	// 运行Server
	g_pAppContext->tprun(0);

	// 结束Tuxedo系统
	g_pAppContext->tpterm();

	// 结束用户应用
	tpsvrdone();

	// 销毁Tuxedo系统
	delete g_pAppContext;

	return 0;
}
Exemple #17
0
static int walk_tree(const char* path, watch_node* parent, array* ignores) {
  if (is_ignored(path, ignores)) {
    return ERR_IGNORE;
  }

  DIR* dir = opendir(path);
  if (dir == NULL) {
    if (errno == EACCES) {
      return ERR_IGNORE;
    }
    else if (errno == ENOTDIR) {  // flat root
      return add_watch(path, parent);
    }
    userlog(LOG_ERR, "opendir(%s): %s", path, strerror(errno));
    return ERR_CONTINUE;
  }

  int id = add_watch(path, parent);
  if (id < 0) {
    closedir(dir);
    return id;
  }

  struct dirent* entry;
  char subdir[PATH_MAX];
  strcpy(subdir, path);
  if (subdir[strlen(subdir) - 1] != '/') {
    strcat(subdir, "/");
  }
  char* p = subdir + strlen(subdir);

  while ((entry = readdir(dir)) != NULL) {
    if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
      continue;
    }

    strcpy(p, entry->d_name);
    if (!is_directory(entry, subdir)) {
      continue;
    }

    int subdir_id = walk_tree(subdir, table_get(watches, id), ignores);
    if (subdir_id < 0 && subdir_id != ERR_IGNORE) {
      rm_watch(id, true);
      id = subdir_id;
      break;
    }
  }

  closedir(dir);
  return id;
}
Exemple #18
0
int watch(const char* root, array* mounts) {
  bool recursive = true;
  if (root[0] == '|') {
    root++;
    recursive = false;
  }

  int path_len = strlen(root);
  if (root[path_len - 1] == '/') {
    --path_len;
  }

  struct stat st;
  if (stat(root, &st) != 0) {
    if (errno == ENOENT) {
      return ERR_MISSING;
    }
    else if (errno == EACCES || errno == ELOOP || errno == ENAMETOOLONG || errno == ENOTDIR) {
      userlog(LOG_INFO, "stat(%s): %s", root, strerror(errno));
      return ERR_CONTINUE;
    }
    else {
      userlog(LOG_ERR, "stat(%s): %s", root, strerror(errno));
      return ERR_ABORT;
    }
  }

  if (S_ISREG(st.st_mode)) {
    recursive = false;
  }
  else if (!S_ISDIR(st.st_mode)) {
    userlog(LOG_WARNING, "unexpected node type: %s, %d", root, st.st_mode);
    return ERR_IGNORE;
  }

  memcpy(path_buf, root, path_len);
  path_buf[path_len] = '\0';
  return walk_tree(path_len, NULL, recursive, mounts);
}
Exemple #19
0
static bool is_ignored(const char* path, array* ignores) {
  if (ignores != NULL) {
    int pl = strlen(path);
    for (int i=0; i<array_size(ignores); i++) {
      const char* ignore = array_get(ignores, i);
      int il = strlen(ignore);
      if (pl >= il && strncmp(path, ignore, il) == 0) {
        userlog(LOG_DEBUG, "path %s is under unwatchable %s - ignoring", path, ignore);
        return true;
      }
    }
  }
  return false;
}
Exemple #20
0
int tpsvrinit(int argc, char **argv)
{
    userlog("\n    server_client starting...");
    
    long lShmKey = IpcKeyMgr::getIpcKey (-1,"SHM_WorkFlowKey");
		Environment::getDBConn()->commit();    	
    if (lShmKey < 0) {
        userlog("\n    server_client start error when tpsvrinit, getshm err SHM_WorkFlowKey\n"
                "    获取共享内存KEY失败! 可能表B_IPC_CFG未定义, 退出!");
        exit(0);
    }
    
    //获取共享缓冲地址
    if (GetShmAddress(&g_pShmTable,lShmKey)<0 ){
        userlog("\n    server_client start error when tpsvrinit, getshm err shmkey:%d\n"
                "    获取共享缓冲失败,可能主监控守护进程未启动! 服务不能加载!",lShmKey);
        g_pShmTable = NULL;
        exit(0);
    }
    userlog("\n    server_client started.");
    
    return 0;
}
Exemple #21
0
static void report_event(char* event, char* path) {
  userlog(LOG_DEBUG, "%s: %s", event, path);

  int len = strlen(path);
  for (char* p = path; *p != '\0'; p++){
    if (*p == '\n') {
      *p = '\0';
    }
  }

  fputs(event, stdout);
  fputc('\n', stdout);
  fwrite(path, len, 1, stdout);
  fputc('\n', stdout);
}
Exemple #22
0
static bool process_inotify_event(struct inotify_event* event) {
  watch_node* node = table_get(watches, event->wd);
  if (node == NULL) {
    return true;
  }

  bool is_dir = (event->mask & IN_ISDIR) == IN_ISDIR;
  userlog(LOG_DEBUG, "inotify: wd=%d mask=%d dir=%d name=%s", event->wd, event->mask & (~IN_ISDIR), is_dir, node->path);

  memcpy(path_buf, node->path, node->path_len + 1);
  int path_len = node->path_len;
  if (event->len > 0) {
    path_buf[node->path_len] = '/';
    int name_len = strlen(event->name);
    memcpy(path_buf + path_len + 1, event->name, name_len + 1);
    path_len += name_len + 1;
  }

  if (is_dir && event->mask & (IN_CREATE | IN_MOVED_TO)) {
    int result = walk_tree(path_len, node, true, NULL);
    if (result < 0 && result != ERR_IGNORE && result != ERR_CONTINUE) {
      return false;
    }
  }

  if (is_dir && event->mask & (IN_DELETE | IN_MOVED_FROM)) {
    for (int i=0; i<array_size(node->kids); i++) {
      watch_node* kid = array_get(node->kids, i);
      if (kid != NULL && strncmp(path_buf, kid->path, kid->path_len) == 0) {
        rm_watch(kid->wd, false);
        array_put(node->kids, i, NULL);
        break;
      }
    }
  }

  if (callback != NULL) {
    (*callback)(path_buf, event->mask);
  }

  return true;
}
Exemple #23
0
static bool process_inotify_event(struct inotify_event* event) {
  watch_node* node = table_get(watches, event->wd);
  if (node == NULL) {
    return true;
  }

  userlog(LOG_DEBUG, "inotify: wd=%d mask=%d dir=%d name=%s",
      event->wd, event->mask & (~IN_ISDIR), (event->mask & IN_ISDIR) != 0, node->name);

  char path[PATH_MAX];
  strcpy(path, node->name);
  if (event->len > 0) {
    if (path[strlen(path) - 1] != '/') {
      strcat(path, "/");
    }
    strcat(path, event->name);
  }

  if ((event->mask & IN_CREATE || event->mask & IN_MOVED_TO) && event->mask & IN_ISDIR) {
    int result = walk_tree(path, node, NULL);
    if (result < 0 && result != ERR_IGNORE) {
      return false;
    }
  }

  if ((event->mask & IN_DELETE || event->mask & IN_MOVED_FROM) && event->mask & IN_ISDIR) {
    for (int i=0; i<array_size(node->kids); i++) {
      watch_node* kid = array_get(node->kids, i);
      if (kid != NULL && strcmp(kid->name, path) == 0) {
        rm_watch(kid->wd, false);
        array_put(node->kids, i, NULL);
        break;
      }
    }
  }

  if (callback != NULL) {
    (*callback)(path, event->mask);
  }
  return true;
}
Exemple #24
0
static void main_loop() {
  int input_fd = fileno(stdin), inotify_fd = get_inotify_fd();
  int nfds = (inotify_fd > input_fd ? inotify_fd : input_fd) + 1;
  fd_set rfds;
  bool go_on = true;

  while (go_on) {
    FD_ZERO(&rfds);
    FD_SET(input_fd, &rfds);
    FD_SET(inotify_fd, &rfds);
    if (select(nfds, &rfds, NULL, NULL, NULL) < 0) {
      userlog(LOG_ERR, "select: %s", strerror(errno));
      go_on = false;
    }
    else if (FD_ISSET(input_fd, &rfds)) {
      go_on = read_input();
    }
    else if (FD_ISSET(inotify_fd, &rfds)) {
      go_on = process_inotify_input();
    }
  }
}
Exemple #25
0
/* Write the given data into the given database. */
static int writedb(DB * dbp, void *buf, u_int32_t size){
	DBT key, data;
	size_t len;
	int ch, ret;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.data = buf;
	data.data = buf;
	data.size = key.size = size;
	ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
	switch (ret) {
	case 0:
		return (EXIT_SUCCESS);
	case DB_KEYEXIST:
	        return (EXIT_SUCCESS);
	case DB_LOCK_DEADLOCK:
	        return (EXIT_SUCCESS);
	default:
		userlog("put: %s", db_strerror(ret));
		return (-1);
	}
}
Exemple #26
0
char *exe_cmd(char *in)
{
    static char	obuf[SS_BUFSIZE];
    static char	ibuf[SS_BUFSIZE];
    static char	cmd[4];
    static char	token[SS_BUFSIZE];
    static char	ebuf[19];
    static char	var1[16];
    int		result;
    char	*buf;

    strncpy(ibuf, in, SS_BUFSIZE);
    strncpy(cmd, ibuf, 4);
    strncpy(ebuf, "200:1,Syntax error;", 19);

    /*
     * Split the commandline after the colon so we can give the
     * options directly to the actual functions. Also set a default
     * and most used answer.
     */
    strncpy(token, &ibuf[5], SS_BUFSIZE);
    strncpy(obuf, "100:0;", SS_BUFSIZE);


    /*
     * The A(counting) commands.
     *
     *  AINI:5,pid,tty,user,program,city;
     *  100:1,linenr;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd, "AINI", 4) == 0) {
        if ((result = reg_newcon(token)) != -1) {
            snprintf(obuf, SS_BUFSIZE, "100:1,%d;", result);
            return obuf;
        } else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ACLO:1,pid;
     *  107:0;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd ,"ACLO", 4) == 0) {
        if (reg_closecon(token) == 0) {
            strcpy(obuf, "107:0;");
            return obuf;
        } else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ADOI:2,pid,doing;
     *  100:0;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd, "ADOI", 4) == 0) {
        if (reg_doing(token) == 0)
            return obuf;
        else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ATCP:1,pid;
     *  100:0;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd, "ATCP", 4) == 0) {
        if (reg_ip(token) == 0)
            return obuf;
        else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ATTY:2,pid,tty;
     *  100:0;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd, "ATTY", 4) == 0) {
        if (reg_tty(token) == 0)
            return obuf;
        else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ALOG:5,file,program,pid,grade,text;
     *  100:0;
     *  201:1,errno;
     */
    if (strncmp(cmd, "ALOG", 4) == 0) {
        if (userlog(token) != 0)
            snprintf(obuf, SS_BUFSIZE, "201:1,%d;", oserr);
        return obuf;
    }

    /*
     *  AUSR:3,pid,user,city;
     *  100:0;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd, "AUSR", 4) == 0) {
        if (reg_user(token) == 0)
            return obuf;
        else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ADIS:2,pid,flag; (set Do Not Disturb).
     *  100:0;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd, "ADIS", 4) == 0) {
        if (reg_silent(token) == 0)
            return obuf;
        else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ATIM:2,pid,seconds;
     *  100:0;
     *  200:1,Syntax Error;
     */
    if (strncmp(cmd, "ATIM", 4) == 0) {
        if (reg_timer(TRUE, token) == 0)
            return obuf;
        else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  ADEF:1,pid;
     *  100:0;
     */
    if (strncmp(cmd, "ADEF", 4) == 0) {
        if (reg_timer(FALSE, token) == 0)
            return obuf;
        else {
            stat_inc_serr();
            return ebuf;
        }
    }

    /*
     *  Check for personal message
     *
     *  CIPM:1,pid;  (Is personal message present)
     *  100:2,fromname,message;
     *  100:0;
     */
    if (strncmp(cmd, "CIPM", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        reg_ipm_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * CSPM:3,fromuser,touser,text; (Send personal message).
     * 100:1,n;  n: 1=donotdisturb 2=buffer full 3=error
     * 100:0;
     */
    if (strncmp(cmd, "CSPM", 4) == 0) {
        if ((result = reg_spm(token)))
            snprintf(obuf, SS_BUFSIZE, "100:1,%d;", result);
        return obuf;
    }

    /*
     * CSYS:2,pid,1;    Sysop available for chat (from mbmon)
     * CSYS:2,pid,0;    Sysop goes away (from mbmon)
     * 100:0;		Allways Ok.
     */
    if (strncmp(cmd, "CSYS", 4) == 0) {
        reg_sysop(token);
        return obuf;
    }

    /*
     * CPAG:2,pid,reason;   Page sysop for a chat
     * 100:1,n;		    1=busy, 2=sysop not available, 3=error
     * 100:0;		    Ok
     */
    if (strncmp(cmd, "CPAG", 4) == 0) {
        if ((result = reg_page(token))) {
            snprintf(obuf, SS_BUFSIZE, "100:1,%d;", result);
        }
        return obuf;
    }

    /*
     * CCAN:1,pid;	    Cancel sysop page
     * 100:0;		    Always Ok
     */
    if (strncmp(cmd, "CCAN", 4) == 0) {
        reg_cancel(token);
        return obuf;
    }

    /*
     * Check for sysop page (from mbmon)
     *
     * CCKP:0;
     * 100:3,pid,1,reason;  Page is active
     * 100:3,pid,0,reason;  Page is canceled, but user still online
     * 100:0;		    No page active
     */
    if (strncmp(cmd, "CCKP", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        reg_checkpage_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * Check for sysop in chatmode for forced sysop chat
     *
     * CISC:1,pid;
     * 100:1,1;		    Yes (and drop into chatmode)
     * 100:1,0;		    No
     */
    if (strncmp(cmd, "CISC", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        chat_checksysop_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * Connect to chatserver
     *
     * CCON:4,pid,username,unixname,n;	Connect to chatserver with username, n=1 user is the sysop
     * 100:1,error;		If error
     * 100:0;			Ok
     */
    if (strncmp(cmd, "CCON", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        chat_connect_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * Close chat session
     *
     * CCLO:1,pid;	    Leave chatserver
     * 100:1,error;	    Error
     * 100:0;		    Ok
     */
    if (strncmp(cmd, "CCLO", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        chat_close_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * Put message on server
     *
     * CPUT:2,pid,message;  Put message on server
     * 100:2,0,error;	    Error, not fatal and continue chat
     * 100:2,1,error;	    Error, fatal and disconnect
     * 100:0;		    Ok
     */
    if (strncmp(cmd, "CPUT", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        chat_put_r(token, buf);
        strncpy(obuf, buf, SS_BUFSIZE);
        free(buf);
        return obuf;
    }

    /*
     * Get message from server
     *
     * CGET:1,pid;	    Get message from server
     * 100:2,0,message;	    If message present
     * 100:2,1,error;	    Error and disconnect
     * 100:0;		    No message
     */
    if (strncmp(cmd, "CGET", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        chat_get_r(token, buf);
        strncpy(obuf, buf, SS_BUFSIZE);
        free(buf);
        return obuf;
    }

    /*
     * The D(isk) commands.
     */

    /*
     *  DRES:0;             Reset and reread disk tables.
     *  100:0;		    Always Ok.
     */
    if (strncmp(cmd, "DRES", 4) == 0) {
        return disk_reset();
    }

    /*
     *  DSPC:1,n;           Check free space in MBytes
     *  100:2,0,n;          No, n = lowest size in MBytes
     *  100:2,1,n;          Yes, n = lowest size in MBytes
     *  100:1,2;            Unknown
     *  100:1,3;            Error
     */
    if (strncmp(cmd, "DSPC", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        disk_check_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  DGFS:0;		    Get filesystem status.
     *  100:n,data1,..,data10;
     */
    if (strncmp(cmd, "DGFS", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        disk_getfs_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * The G(lobal) commands.
     *
     *  GNOP:1,pid;
     *  100:0;
     */
    if (strncmp(cmd ,"GNOP", 4) == 0) {
        reg_nop(token);
        return obuf;
    }

    /*
     *  GPNG:n,data;
     *  100:n,data;
     */
    if (strncmp(cmd, "GPNG", 4) == 0) {
        snprintf(obuf, SS_BUFSIZE, "100:%s", token);
        return obuf;
    }

    /*
     *  GVER:0;
     *  100:1,Version ...;
     */
    if (strncmp(cmd, "GVER", 4) == 0) {
        snprintf(obuf, SS_BUFSIZE, "100:1,Version %s;", VERSION);
        return obuf;
    }

    /*
     *  GSTA:0;
     *  100:19,start,laststart,daily,startups,clients,tot_clients,tot_peak,tot_syntax,tot_comerr,
     *         today_clients,today_peak,today_syntax,today_comerr,!BBSopen,ZMH,internet,Processing,Load,sequence;
     *  201:1,16;
     */
    if (strncmp(cmd, "GSTA", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        stat_status_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  GMON:1,n;  n=1 First time
     *  100:7,pid,tty,user,program,city,isdoing,starttime;
     *  100:0;
     */
    if (strncmp(cmd, "GMON", 4) == 0) {
        strtok(token, ",");
        strcpy(var1, strtok(NULL, ";"));
        buf = calloc(SS_BUFSIZE, sizeof(char));
        get_reginfo_r(atoi(var1), buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  GSYS:0;
     *  100:7,calls,pots_calls,isdn_calls,network_calls,local_calls,startdate,last_caller;
     *  201:1,16;
     */
    if (strncmp(cmd, "GSYS", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        get_sysinfo_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  GLCC:0;
     *  100:1,n;
     */
    if (strncmp(cmd, "GLCC", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        get_lastcallercount_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  GLCR:1,recno;
     *  100:9,user,location,level,device,time,mins,calls,speed,actions;
     *  201:1,16;
     */
    if (strncmp(cmd, "GLCR", 4) == 0) {
        strtok(token, ",");
        strcpy(var1, strtok(NULL, ";"));
        buf = calloc(SS_BUFSIZE, sizeof(char));
        get_lastcallerrec_r(atoi(var1), buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }


    /*
     * The (M)IB commands, SNMP counters.
     */

    /*
     * MIB Set Mailer Session
     *
     * MSMS:6,kbrcvd,kbsent,direction,state,freqs;
     * 100:0;
     *
     * kbrcvd	  Kbytes received
     * kbsent	  KBytes sent
     * direction  0=inbount, 1=outbound
     * state	  0=secure, 1=unsecure, 2=bad session
     * type	  0=unknown, 1=ftsc, 2=yoohoo, 3=emsi, 4=binkp
     * freqs	  nr of file requests
     */
    if (strncmp(cmd, "MSMS", 4) == 0) {
        mib_set_mailer(token);
        return obuf;
    }

    /*
     * MIB Get Mailer Session
     *
     * MGMS:0;
     * 100:12,kbrcvd,kbsent,sessin,sessout,sess_sec,sess_unseq,sess_bad,ftsc,yoohoo,emsi,binkp,freqs;
     */
    if (strncmp(cmd, "MGMS", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_mailer_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Set Tosser Netmail
     *
     * MSTN:3,in,out,bad;
     * 100:0;
     */
    if (strncmp(cmd, "MSTN", 4) == 0) {
        mib_set_netmail(token);
        return obuf;
    }

    /*
     * MIB Get Tosser Netmail
     *
     * MGTN:0;
     * 100:3,in,out,bad;
     */
    if (strncmp(cmd, "MGTN", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_netmail_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Set Tosser Internet-email
     *
     * MSTI:3,in,out,bad;
     * 100:0;
     */
    if (strncmp(cmd, "MSTI", 4) == 0) {
        mib_set_email(token);
        return obuf;
    }

    /*
     * MIB Get Tosser Internet-email
     *
     * MGTI:0;
     * 100:3,in,out,bad;
     */
    if (strncmp(cmd, "MGTI", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_email_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Set Tosser Echomail
     *
     * MSTE:4,in,out,bad,dupe;
     * 100:0;
     */
    if (strncmp(cmd, "MSTE", 4) == 0) {
        mib_set_echo(token);
        return obuf;
    }

    /*
     * MIB Get Tosser Echomail
     *
     * MGTE:0;
     * 100:4,in,out,bad,dupe;
     */
    if (strncmp(cmd, "MGTE", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_echo_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Set Tosser RFC-news
     *
     * MSTR:4,in,out,bad,dupe;
     * 100:0;
     */
    if (strncmp(cmd, "MSTR", 4) == 0) {
        mib_set_news(token);
        return obuf;
    }

    /*
     * MIB Get Tosser RFC-news
     *
     * MGTR:0;
     * 100:4,in,out,bad,dupe;
     */
    if (strncmp(cmd, "MGTR", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_news_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Get Tosser Totals
     *
     * MGTT:0;
     * 100:4,in,out,bad,dupe;
     */
    if (strncmp(cmd, "MGTT", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_tosser_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Set Tosser Files
     *
     * MSFF:6,in,out,bad,dupe,magics,hatched;
     * 100:0;
     */
    if (strncmp(cmd, "MSTF", 4) == 0) {
        mib_set_files(token);
        return obuf;
    }

    /*
     * MIB Get Tosser Files
     *
     * MGFF:0;
     * 100:6,in,out,bad,dupe,magics,hatched;
     */
    if (strncmp(cmd, "MGTF", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_files_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Set BBS
     *
     * MSBB:9,sessions,minutes,posted,uploads,kbupload,downloads,kbdownload,chats,chatminutes;
     * 100:0;
     */
    if (strncmp(cmd, "MSBB", 4) == 0) {
        mib_set_bbs(token);
        return obuf;
    }

    /*
     * MIB Get BBS
     *
     * MGBB:0;
     * 100:9,sessions,minutes,posted,uploads,kbupload,downloads,kbdownload,chats,chatminutes;
     */
    if (strncmp(cmd, "MGBB", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_bbs_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * MIB Get Outbound Size
     *
     * MGOB:0;
     * 100:1,size;
     */
    if (strncmp(cmd, "MGOB", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        mib_get_outsize_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     * The (S)tatus commands.
     *
     *  SBBS:0;
     *  100:2,n,status message;
     */
    if (strncmp(cmd, "SBBS", 4) == 0) {
        switch(stat_bbs_stat()) {
        case 0:
            snprintf(obuf, SS_BUFSIZE, "100:2,0,The system is open for use;");
            break;
        case 1:
            snprintf(obuf, SS_BUFSIZE, "100:2,1,The system is closed right now!;");
            break;
        case 2:
            snprintf(obuf, SS_BUFSIZE, "100:2,2,The system is closed for Zone Mail Hour!;");
            break;
        }
        return obuf;
    }

    /*
     *  SOPE:0;
     *  100:0;
     */
    if (strncmp(cmd, "SOPE", 4) == 0) {
        stat_set_open(1);
        return obuf;
    }

    /*
     *  SCLO:0;
     *  100:0;
     */
    if (strncmp(cmd, "SCLO", 4) == 0) {
        stat_set_open(0);
        return obuf;
    }

    /*
     *  SFRE:0;
     *  100:1,Running utilities: n  Active users: n;
     *  100:0;
     *  201:1,16;
     */
    if (strncmp(cmd, "SFRE", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        reg_fre_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  SSEQ:0;
     *  100:1,number;
     *  200:1,16;
     */
    if (strncmp(cmd, "SSEQ", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        getseq_r(buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  SEST:1,semafore;   Get status of semafore
     *  100:1,n;           1 = set, 0 = not set
     *  200:1,16;
     */
    if (strncmp(cmd, "SEST", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        sem_status_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  SECR:1,semafore;   Set semafore
     *  100:0;
     *  200:1,16;
     */
    if (strncmp(cmd, "SECR", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        sem_create_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }

    /*
     *  SERM:1,semafore;   Remove semafore
     *  100:0;
     *  200:1,16;
     */
    if (strncmp(cmd, "SERM", 4) == 0) {
        buf = calloc(SS_BUFSIZE, sizeof(char));
        sem_remove_r(token, buf);
        snprintf(obuf, SS_BUFSIZE, "%s", buf);
        free(buf);
        return obuf;
    }


    /*
     * If we got this far, there must be an error.
     */
    stat_inc_serr();
    Syslog('!', "Comm systax error: \"%s:%s\"", cmd, printable(token, 0));
    return ebuf;
}
Exemple #27
0
int tpsvrinit(int argc, char **argv)
{
	userlog("%s Raise\n",argv[0]);
  return 0;
}
Exemple #28
0
void P_GET_TRANSINFO(TPSVCINFO *rqst)
{
	long drawID = 0;
	int iRet = 0;
	char swsnaddr[30];
	char *allrcvbuf = NULL;
	char *rcvbuf = NULL;
	FBFR32 *sendbuf;
	FBFR32 *recvbuf;
	
	char *sendstring;
	char *sendbuf2=NULL;
	int iLen = rqst->len;
	long  i;
	sendstring = new char[iLen];
	memset(sendstring,'\0',iLen);
	sendbuf2 = (char *) tpalloc("STRING", NULL, MAX_CMAMSG_LEN);

	//char *sendbuf = NULL;
	long lRecLen = 0;
	string s_rcvbuff;
	char cRouteKey[30];
	char tmpBuff[30];

	
	memset(swsnaddr,0,sizeof(swsnaddr));
	allrcvbuf = tpalloc("STRING",NULL,MAX_CMAMSG_LEN);
	//rcvbuf = tpalloc("STRING",NULL,MAX_CMAMSG_LEN);
	
	if ((sendbuf = (FBFR32 *)tpalloc("FML32",NULL,MAX_CMAMSG_LEN)) == NULL)
	{
		sprintf(sendbuf2,"<iResult=1><sMsg=sendbuf err>");
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	
	if ((recvbuf = (FBFR32 *)tpalloc("FML32",NULL,MAX_CMAMSG_LEN)) == NULL)
	{
		sprintf(sendbuf2,"<iResult=1><sMsg=recvbuf err>");
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}

	//sendbuf = tpalloc("STRING",NULL,MAX_CMAMSG_LEN);
	memset(sendstring,0,MAX_CMAMSG_LEN);
	s_rcvbuff.clear();
	
	memcpy(sendstring, rqst->data, iLen);
	sendstring[iLen] = 0;
	TrimHeadNull(sendstring,iLen);
	::PETRI::P_RECVBUFF.reset();
	::PETRI::P_RECVBUFF.parseBuff(sendstring);
	drawID = (long) ::PETRI::P_RECVBUFF.getDrawID();
	
	vector<long> transList;
	transList.clear();
	TOCIQuery query (::PETRI::P_DB.getDB());

	try {
		string sql = "SELECT TRANSITION_ID FROM P_TRANSITION_ATTR WHERE DRAW_ID= :DRAWID AND EXE_STATE = 'R'";
		query.close();
		query.setSQL(sql.c_str());
		query.setParameter("DRAWID",drawID);
		query.open();
		while(query.next()) {
			transList.push_back(query.field(0).asLong());
		}
		query.close();
		query.commit();
	} catch (TOCIException& toe) {
		query.close();
		query.commit();
		sprintf(sendbuf2,"<iResult=1><sMsg=Get Transition of The Draw Error! %s>",toe.getErrMsg());
		tpfree(rcvbuf);
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	} catch (...) {
		query.close();
		query.commit();
		sprintf(sendbuf2,"<iResult=1><sMsg=Get Transition of The Draw Error!> other error");
		tpfree(rcvbuf);
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	};

	if (0 == transList.size()){
		memset(sendbuf2,0,MAX_CMAMSG_LEN);
		tpfree(rcvbuf);
		tpfree((char *)sendbuf);
		query.commit();
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	
	for (int idx = 0;idx <transList.size();idx ++ ) {

	try {
		string sql = "select b.wsnaddr from P_TRANSITION_HOST_INFO a, WF_HOST_WSNADDR b where a.wsnaddr_id = b.wsnaddr_id and a.transition_id = :TRANSID";			
		query.close();
		query.setSQL(sql.c_str());
		query.setParameter("TRANSID",transList[idx]);
		query.open();
		while(query.next()) {
			strcpy( swsnaddr,query.field(0).asString() );
		}
		query.close();
		
		string sql2 = "select HOST_AREA||HOST_DOMAIN from P_TRANSITION_HOST_INFO where transition_id = :TRANSID";
		query.close();
		query.setSQL(sql2.c_str());
		query.setParameter("TRANSID",transList[idx]);
		query.open();
		while(query.next()) {
			strcpy( tmpBuff,query.field(0).asString());
		}
		query.close();
		query.commit();
	}catch (TOCIException & toe){
		sprintf(sendbuf2,"<iResult=1><lTransitionID=%ld><sMsg=get WSNADDR err:%s>",transList[idx],toe.getErrMsg());
		query.close();
		query.commit();
	} catch(...) {
		sprintf(sendbuf2,"<iResult=1><lTransitionID=%ld><sMsg=get WSNADDR other err>",transList[idx]);
		query.close();
		query.commit();
	}
	if(0 == swsnaddr[0]){
		sprintf(sendbuf2,"<iResult=1><lTransitionID=%ld><sMsg=Transition have no WSNADDR >",transList[idx]);
	}
/*
	iRet = setenv("WSNADDR",swsnaddr,1);
	if( -1 == iRet){
		sprintf(rcvbuf,"<iResult=1><lTransitionID=%ld><sMsg=set WSNADDR fail>",transList[idx]);
	};
*/
	sprintf(sendbuf2,"<lTransitionID=%ld>%s\0",transList[idx],sendstring);
	
	strcpy(cRouteKey, tmpBuff);		
	//iRet = cslfput(&sendbuf,ROUTE_KEY,0,cRouteKey);
	//iRet = Fchg32(&sendbuf,ROUTE_KEY,0,(char*)cRouteKey,(FLDLEN32)10);
	strcpy(cRouteKey, tmpBuff);		
	iRet = Fchg32(sendbuf,ROUTE_KEY,0,(char *)cRouteKey,0);
	//iRet = Fchg32((FBFR32*)(&sendbuf),ROUTE_KEY,0,(char*)cRouteKey,(FLDLEN32)20);
	if(iRet < 0)
	{
		userlog("cRouteKey=[%s],iRet=[%d]",cRouteKey,iRet);
		sprintf(sendbuf2,"<iResult=1><sMsg=cslfput to ROUTE_KEY is error>\0");
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
		
	iRet = Fchg32(sendbuf,SEND_STRING,0,(char *)sendbuf2,0);
	if(iRet < 0)
	{
		userlog("sendstring=[%s],iRet=[%d]",sendstring,iRet);

		sprintf(sendbuf2,"<iResult=1><sMsg=cslfput to SEND_STRING is error>\0");
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
		

	
	iRet = tpcall("P_G_T_LOCAL",(char *)sendbuf,0,(char **)&recvbuf,&lRecLen,0);  


    if (iRet < 0){
		sprintf(sendbuf2,"<iResult=1><lTransitionID=%ld><sMsg=call localsvc err,tperrno:%d>\0",transList[idx],tperrno);
	}
	userlog(sendbuf2);
	s_rcvbuff = s_rcvbuff + "$" + sendbuf2;
	}
	
	sprintf (allrcvbuf,"%s\0",s_rcvbuff.c_str());
	tpfree((char *)sendbuf);
	tpfree((char *)rcvbuf);
	
	tpreturn(TPSUCCESS,0L,allrcvbuf,MAX_CMAMSG_LEN,0L);
	
}
Exemple #29
0
void P_CHANGE_STATUS(TPSVCINFO *rqst)
{
	long ltransID=0;
	int iRet=0;
	char swsnaddr[30];
	FBFR32 *sendbuf;
	FBFR32 *recvbuf;
	
	char *sendstring;
	char *sendbuf2=NULL;
	int iLen = rqst->len;
	long  i;
	sendstring = new char[iLen];
	memset(sendstring,'\0',iLen);
	sendbuf2 = (char *) tpalloc("STRING", NULL, MAX_CMAMSG_LEN);
	
	//char *rcvbuf = NULL;
	//char *sendbuf = NULL;
	long lRecLen = 0;
	char cRouteKey[30];
	char tmpBuff[30];
	memset(swsnaddr,0,sizeof(swsnaddr));
	//rcvbuf = tpalloc("STRING",NULL,MAX_CMAMSG_LEN);
	//sendbuf = tpalloc("STRING",NULL,MAX_CMAMSG_LEN);
	if ((sendbuf = (FBFR32 *)tpalloc("FML32",NULL,MAX_CMAMSG_LEN)) == NULL)
	{
		sprintf(sendbuf2,"<iResult=1><sMsg=sendbuf err>");
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	
	if ((recvbuf = (FBFR32 *)tpalloc("FML32",NULL,MAX_CMAMSG_LEN)) == NULL)
	{
		sprintf(sendbuf2,"<iResult=1><sMsg=recvbuf err>");
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	
	//int iLen = rqst->len;
	memcpy(sendstring, rqst->data, iLen);
	sendstring[iLen] = 0;
	TrimHeadNull(sendstring,iLen);
	::PETRI::P_RECVBUFF.reset();
	::PETRI::P_RECVBUFF.parseBuff(sendstring);
	ltransID = ::PETRI::P_RECVBUFF.getTransitionID();
	
	TOCIQuery query (::PETRI::P_DB.getDB());
	try {
		string sql = "select b.wsnaddr from P_TRANSITION_HOST_INFO a, WF_HOST_WSNADDR b where a.wsnaddr_id = b.wsnaddr_id and a.transition_id = :TRANSID";					
		query.close();
		query.setSQL(sql.c_str());
		query.setParameter("TRANSID",ltransID);
		query.open();
		while(query.next()) {
			strcpy( swsnaddr,query.field(0).asString() );
		}
		query.close();

		string sql2 = "select HOST_AREA||HOST_DOMAIN from P_TRANSITION_HOST_INFO where transition_id = :TRANSID";
		query.close();
		query.setSQL(sql2.c_str());
		query.setParameter("TRANSID",ltransID);
		query.open();
		while(query.next()) {
			strcpy( tmpBuff,query.field(0).asString());
		}
		query.close();
		query.commit();
		
	}catch (TOCIException & toe){
		sprintf(sendbuf2,"<iResult=1><sMsg=get WSNADDR err:%s>",toe.getErrMsg());
		query.close();
		query.commit();
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	} catch(...) {
		sprintf(sendbuf2,"<iResult=1><sMsg=get WSNADDR other err>");
		query.close();
		query.commit();
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	if(0 == swsnaddr[0]){
		sprintf(sendbuf2,"<iResult=1><sMsg=Transition have no WSNADDR >");
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
/*
	iRet = setenv("WSNADDR",swsnaddr,1);
	if( -1 == iRet){
		sprintf(rcvbuf,"<iResult=1><sMsg=set WSNADDR fail>");
		tpfree(sendbuf);
		tpreturn(TPSUCCESS,0L,rcvbuf,strlen(rcvbuf),0L);
	};
	*/
	strcpy(cRouteKey, tmpBuff);		
	//iRet = Fchg32(&sendbuf,ROUTE_KEY,0,(char*)cRouteKey,(FLDLEN32)10);
	iRet = Fchg32(sendbuf,ROUTE_KEY,0,(char *)cRouteKey,0);
	if(iRet < 0)
	{
		userlog("cRouteKey=[%s],iRet=[%d]",cRouteKey,iRet);
		sprintf(sendbuf2,"<iResult=1><sMsg=cslfput to ROUTE_KEY is error>");
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	
	iRet = Fchg32(sendbuf,SEND_STRING,0,(char *)sendstring,0);
	if(iRet < 0)
	{
		userlog("sendstring=[%s],iRet=[%d]",sendstring,iRet);

		sprintf(sendbuf2,"<iResult=1><sMsg=cslfput to SEND_STRING is error>");
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	
	
	iRet = tpcall("P_C_S_LOCAL",(char *)sendbuf,0,(char **)&recvbuf,&lRecLen,0);   
    if (iRet < 0){
		sprintf(sendbuf2,"<iResult=1><sMsg=call localsvc err,tperrno:%d>",tperrno);
		tpfree((char *)sendbuf);
		tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	}
	
	tpfree((char *)sendbuf);
	sprintf(sendbuf2,"<iResult=0><sMsg=you tpcall  success>");
	tpreturn(TPSUCCESS,0L,sendbuf2,MAX_CMAMSG_LEN,0L);
	
}
tpsvrinit(int argc,char **argv)
{
	userlog("Bank-Login-Server has started");
	return(0);
}