/** * closes a flow described by fd * if send_reply is non-zero, a response is sent to client's mapi stub, * (send_reply=0 is used for local clean-ups) */ static void cmd_close_flow(int fd, int pid, int sock, int send_reply) { struct flow *f; struct client *cl; struct mapiipcbuf buf; long file_size; f=(struct flow*)flist_get(flowlist, fd); if (f) { /* to avoid reading memory after it's freed */ int tmpfd = f->fd; /* prevent closing flows of other processes */ if (pid != f->id) { DEBUG_CMD(Debug_Message( "Proc %d tried to close flow %d, which belongs to proc %d", pid, f->fd, f->id)); report_error(MAPI_INVALID_FLOW, pid, sock); return; } cleanup_flow(f); while(__sync_lock_test_and_set(&clientlist_lock,1)); cl = flist_get(clientlist, pid); f = (struct flow *) flist_remove(cl->flowlist, fd); cl->numflows--; clientlist_lock = 0; //send an ACK that flow closed if (send_reply) { buf.mtype = pid; buf.cmd = CLOSE_FLOW_ACK; buf.fd = tmpfd; mapiipc_daemon_write((struct mapiipcbuf *) &buf, sock); if (log_to_file) { file_size = acquire_write_lock(log_fd_info); write_to_file(log_fd_info, "MAPID: flow %d was closed at ", buf.fd); write_date(log_fd_info); write_newline(log_fd_info, "\n"); release_write_lock(log_fd_info, file_size); } if (log_to_syslog) log_message("flow %d was closed", buf.fd); } } else { report_error(MAPI_INVALID_FLOW, pid, sock); } }
static void cmd_delete_offline_device(char *dev, int pid, int sock) { mapidrv *drv; struct mapiipcbuf buf; long file_size; for (drv = drvlist; drv != NULL; drv = drv->next) { if (drv->device != NULL) if (strcmp(dev, drv->device) == 0) { break; } } if (drv == NULL) { DEBUG_CMD(Debug_Message("No device found for %s", dev)); report_error(MAPID_NO_DEVICE, pid, sock); return; } if (drv->offline != 0) { mapidrv_delete_device = get_drv_funct(drv->handle, "mapidrv_delete_device"); mapidrv_delete_device(drv->devid); drv->active=0; } buf.mtype = pid; buf.cmd = DELETE_OFFLINE_DEVICE_ACK; buf.fd = -1; if (log_to_file) { file_size = acquire_write_lock(log_fd_info); write_to_file(log_fd_info, "MAPID: offline device %s was deleted at ", dev); write_date(log_fd_info); write_newline(log_fd_info, "\n"); release_write_lock(log_fd_info, file_size); } if (log_to_syslog) log_message("offline device %s was deleted", dev); DEBUG_CMD(Debug_Message("Deleted offline device %s", dev)); mapiipc_daemon_write((struct mapiipcbuf *) &buf, sock); }
static void cmd_connect(int fd, int pid, int sock) //Connect to flow //fd = flow descriptor { struct mapiipcbuf buf; int err = 0; mapidrv *drv = get_drv(fd); long file_size; if (drv == NULL) { /* driver not found(should be handled in create_flow), or invalid flow id */ DEBUG_CMD(Debug_Message("cmd_connect: no driver found")); report_error(MAPI_INVALID_FLOW, pid, sock); return; } if (err == 0) { mapidrv_connect = get_drv_funct(drv->handle, "mapidrv_connect"); err = mapidrv_connect(drv->devid, fd); } if (err != 0) { report_error(err, pid, sock); return; } buf.cmd = CONNECT_ACK; buf.mtype = get_id(fd); /* should be == pid */ buf.fd = fd; if (log_to_file) { file_size = acquire_write_lock(log_fd_info); write_to_file(log_fd_info, "MAPID: connect to flow %d at ", fd); write_date(log_fd_info); write_newline(log_fd_info, "\n"); release_write_lock(log_fd_info, file_size); } if (log_to_syslog) log_message("connect to flow %d", fd); mapiipc_daemon_write((struct mapiipcbuf *) &buf, sock); }
static void cmd_get_libs(int pid, int sock) { struct mapiipcbuf buf; static char path[2048] = { '\0' }; char *str, *path_; long file_size; conf_category_t *conf; if (path[0] == '\0') { if ((conf = pc_load(mapid_conf)) != NULL) { str = pc_get_param(pc_get_category(conf, ""), "libs"); strncpy(path, str, sizeof(char) * 2048); strncpy((char *)buf.data, path, sizeof(char) * 2048); buf.cmd = GET_LIBS_ACK; if (log_to_file) { path_ = strdup(path); file_size = acquire_write_lock(log_fd_info); write_libraries(log_fd_info, path_); release_write_lock(log_fd_info, file_size); free(path_); } if (log_to_syslog) { path_ = strdup(path); syslog_libraries(path_); free(path_); } pc_close(conf); } else { buf.cmd = GET_LIBS_NACK; } } else { strncpy((char *)buf.data, path, sizeof(char) * 2048); buf.cmd = GET_LIBS_ACK; } buf.mtype = pid; mapiipc_daemon_write(&buf, sock); }
static void acquire_packages_write_lock(YogEnv* env, YogVM* vm) { acquire_write_lock(env, &vm->pkgs_lock); }
static void acquire_symbols_write_lock(YogEnv* env, YogVM* vm) { acquire_write_lock(env, &vm->sym_lock); }
static void cmd_create_offline_device(char *dev, int format, int pid, int sock) //Create a new flow //dev = device //if = IPC id used to send ack message back to client { struct mapiipcbuf buf; mapidrv *drv, *drv2, *lok; int err; int file; struct client *cl; char *format_; long file_size; //Get file descriptor buf.mtype = pid; buf.cmd = SEND_FD; mapiipc_daemon_write((struct mapiipcbuf *) &buf, sock); file = mapiipc_read_fd(sock); //Decide which driver to use for (drv = drvlist; drv != NULL; drv = drv->next) { if (drv->format == format) { DEBUG_CMD(Debug_Message("Using driver %s for %s", drv->name, dev)); break; } } if (drv == NULL) { DEBUG_CMD(Debug_Message("ERROR: No driver found for %s", dev)); report_error(MAPID_NO_DRIVER, pid, sock); return; } //Calls driver //First create new "device" for the file drv2 = malloc(sizeof(mapidrv)); drv2->device = malloc(strlen(dev)+7); sprintf(drv2->device, "%s@%d", dev, deviceid); lok=drvlist; while (lok->next!=NULL) lok = lok->next; lok->next = drv2; drv2->next=NULL; drv2->handle = drv->handle; drv2->name = strdup(drv->name); drv2->format = drv->format; drv2->devid = -deviceid++; drv2->offline = 1; drv2->active = 1; drv2->description = strdup(drv->description); drv2->offline_status = DEVICE_SETUP; mapidrv_add_device = get_drv_funct(drv->handle, "mapidrv_add_device"); err = mapidrv_add_device(dev, file, drv2->devid, gflist, &drv2->offline_status); if (err != 0) { report_error(err, pid, sock); return; } // save a reference to the newly created flow to client's flow list cl = flist_get(clientlist, pid); if (cl == NULL) { cl = (struct client *) malloc(sizeof(struct client)); cl->pid = pid; cl->sock = sock; // init the list that holds references to the flows of this client if ((cl->flowlist = malloc(sizeof(flist_t))) == NULL) { DEBUG_CMD(Debug_Message( "ERROR: cmd_create_flow - malloc new client struct: %s", strerror(errno))); exit(EXIT_FAILURE); } if ((cl->devicelist = malloc(sizeof(flist_t))) == NULL) { DEBUG_CMD(Debug_Message( "ERROR: cmd_create_flow - malloc new client struct: %s", strerror(errno))); exit(EXIT_FAILURE); } flist_init(cl->flowlist); flist_init(cl->devicelist); cl->numflows = 0; flist_append(clientlist, pid, cl); } flist_append(cl->devicelist, drv2->devid, drv->handle); cl->numdevs++; //Send ack back to user buf.mtype = pid; buf.cmd = CREATE_OFFLINE_DEVICE_ACK; strcpy((char *)buf.data, drv2->device); buf.fd = -1; if (format == 0) format_ = strdup("MFF_PCAP"); else if (format == 1) format_ = strdup("MFF_RAW"); else if (format == 2) format_ = strdup("MFF_DAG_ERF"); else if (format == 4) format_ = strdup("MFF_NAPATECH"); if (log_to_file) { file_size = acquire_write_lock(log_fd_info); write_to_file( log_fd_info, "MAPID: new offline device was created ( tracefile: %s, format: %s, device name returned: %s ) at ", dev, format_, buf.data); write_date(log_fd_info); write_newline(log_fd_info, "\n"); release_write_lock(log_fd_info, file_size); } if (log_to_syslog) log_message( "new offline device was created ( tracefile: %s, format: %s, device name returned: %s )", dev, format_, buf.data); free(format_); mapiipc_daemon_write((struct mapiipcbuf *) &buf, sock); }
static void cmd_create_flow(char *device, int pid, uid_t uid, int sock) /*removed id, id==pid here */ //Create a new flow //dev = device //if = IPC id used to send ack message back to client { struct flow *fl = (struct flow *) malloc(sizeof(struct flow)); struct client *cl; struct mapiipcbuf buf; char *devtype; mapidrv *drv; int err = 0; char* dev=device; long file_size; fl->id = pid; fl->fd = ++fdseed; fl->drv = NULL; fl->uid = uid; fl->offline = 0; if (running_shutdown) err = MAPI_SHUTTING_DOWN; //Decide which driver to use for (drv = drvlist; drv != NULL; drv = drv->next) { if (drv->device != NULL) if (strcmp(dev, drv->device) == 0) { fl->drv = drv; DEBUG_CMD(Debug_Message("Using driver %s for %s", drv->name, dev)); break; } } if (fl->drv == NULL) { DEBUG_CMD(Debug_Message("No driver found for %s", dev)); report_error(MAPID_NO_DRIVER, pid, sock); free(fl); return; } ++flows; //total number of currently registered flows //Calls driver if (err == 0) { mapidrv_create_flow = get_drv_funct(fl->drv->handle, "mapidrv_create_flow"); err = mapidrv_create_flow(drv->devid, fl->fd, &devtype); } if (err != 0) { /* flow wasn't created */ /* we can't leave the flow in place, but we need it for errno... */ /* cleanup? */ flows--; report_error(err, pid, sock); free(fl); return; } else { flist_append(flowlist, fl->fd, fl); //check if this is the first time we hear from this client cl = flist_get(clientlist, pid); if (cl == NULL) { cl = (struct client *) malloc(sizeof(struct client)); cl->pid = pid; cl->sock = sock; // init the list that holds references to the flows of this client if ((cl->flowlist = malloc(sizeof(flist_t))) == NULL) { DEBUG_CMD(Debug_Message( "ERROR: cmd_create_flow - malloc new client struct: %s", strerror(errno))); exit(EXIT_FAILURE); } flist_init(cl->flowlist); if ((cl->devicelist = malloc(sizeof(flist_t))) == NULL) { DEBUG_CMD(Debug_Message( "ERROR: cmd_create_flow - malloc new client struct: %s", strerror(errno))); exit(EXIT_FAILURE); } flist_init(cl->devicelist); cl->numflows = 0; cl->numdevs = 0; flist_append(clientlist, pid, cl); } // save a reference to the newly created flow to client's flow list cl->numflows++; flist_append(cl->flowlist, fl->fd, fl); //Send ack back to user buf.mtype = pid; strcpy((char *)buf.data, devtype); buf.cmd = CREATE_FLOW_ACK; buf.fd = fl->fd; if (log_to_file) { file_size = acquire_write_lock(log_fd_info); write_to_file(log_fd_info, "MAPID: new flow was created ( device: %s, fd: %d ) at ", device, fl->fd); write_date(log_fd_info); write_newline(log_fd_info, "\n"); release_write_lock(log_fd_info, file_size); } if (log_to_syslog) log_message("new flow was created ( device: %s, fd: %d )", device, fl->fd); } mapiipc_daemon_write(&buf, sock); }
static void cmd_apply_function(struct mapiipcbuf *qbuf, int pid, int sock) //Apply function to flow //qbuf = IPC buffer //(this function should be changed so that it becomes IPC independent) { int functionid; int fd; mapidrv *drv; mapiFunctArg *args = qbuf->data; char *argdescr = (char *)qbuf->argdescr; char *function; long file_size; while (strlen(argdescr) > 0) { switch (*argdescr) { case 's': args += strlen((char *)args) + 1; break; case 'S': // reference to flows and functions (e.g RES2FILE) args += strlen((char *)args) + 1; break; case 'i': args += sizeof(int); break; case 'r': // reference to a flow args += sizeof(int); break; case 'f': // reference to a fuction args += sizeof(int); break; case 'c': args += sizeof(char); break; case 'l': args += sizeof(unsigned long long); break; case 'u': args += sizeof(int); break; case 'p': args += strlen((char *)args) + 1; break; case 'w': qbuf->mtype = get_id(qbuf->fd); qbuf->cmd = SEND_FD; mapiipc_daemon_write((struct mapiipcbuf *) qbuf, sock); fd = mapiipc_read_fd(sock); addarg(&args, &fd, INT); break; default: break; } argdescr++; // move to the next arg } drv = get_drv(qbuf->fd); if (drv == NULL) { DEBUG_CMD(Debug_Message( "cmd_apply_function: no driver found for fd=%d", qbuf->fd)); report_error(MAPI_INVALID_FLOW, pid, sock); return; } mapidrv_apply_function = get_drv_funct(drv->handle, "mapidrv_apply_function"); function = strdup(qbuf->function); functionid = mapidrv_apply_function(drv->devid, qbuf->fd, APPLY_NORMAL, qbuf->function, qbuf->data); if (functionid == -1) { /* error in mapid */ report_error(mapid_get_errno(qbuf->fd), pid, sock); return; } qbuf->mtype = get_id(qbuf->fd); qbuf->cmd = APPLY_FUNCTION_ACK; qbuf->fid = functionid; if (log_to_file) { file_size = acquire_write_lock(log_fd_info); write_to_file(log_fd_info, "MAPID: function %s was applyed to flow %d ( fid: %d ) at ", function, qbuf->fd, qbuf->fid); write_date(log_fd_info); write_newline(log_fd_info, "\n"); release_write_lock(log_fd_info, file_size); } if (log_to_syslog) log_message("function %s was applyed to flow %d ( fid: %d )", function, qbuf->fd, qbuf->fid); free(function); mapiipc_daemon_write((struct mapiipcbuf *) qbuf, sock); }