int smbc_ftruncate(int fd, off_t size) { SMBCFILE * file = find_fd(fd); return smbc_getFunctionFtruncate(statcont)(statcont, file, size); }
void conf_fd( int fd, int and_events, int or_events ) { int n = find_fd( fd ); assert( n >= 0 ); pollfds[n].events = (pollfds[n].events & and_events) | or_events; }
/* write to a file */ static NTSTATUS svfs_write(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, union smb_write *wr) { struct svfs_private *p = ntvfs->private_data; struct svfs_file *f; ssize_t ret; if (wr->generic.level != RAW_WRITE_WRITEX) { return ntvfs_map_write(ntvfs, req, wr); } CHECK_READ_ONLY(req); f = find_fd(p, wr->writex.in.file.ntvfs); if (!f) { return NT_STATUS_INVALID_HANDLE; } ret = pwrite(f->fd, wr->writex.in.data, wr->writex.in.count, wr->writex.in.offset); if (ret == -1) { return map_nt_error_from_unix(errno); } wr->writex.out.nwritten = ret; wr->writex.out.remaining = 0; /* should fill this in? */ return NT_STATUS_OK; }
/* flush a file */ static NTSTATUS svfs_flush(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, union smb_flush *io) { struct svfs_private *p = ntvfs->private_data; struct svfs_file *f; switch (io->generic.level) { case RAW_FLUSH_FLUSH: case RAW_FLUSH_SMB2: /* ignore the additional unknown option in SMB2 */ f = find_fd(p, io->generic.in.file.ntvfs); if (!f) { return NT_STATUS_INVALID_HANDLE; } fsync(f->fd); return NT_STATUS_OK; case RAW_FLUSH_ALL: for (f=p->open_files;f;f=f->next) { fsync(f->fd); } return NT_STATUS_OK; } return NT_STATUS_INVALID_LEVEL; }
extern int Seek(int fd, int offset, int whence) { int index = find_fd(fd); struct seek_struct seek_msg; struct my_msg *msg; /* No match for provided file descriptor number.*/ if(index == -1) { TtyPrintf(0,"\nERROR: Invalid Fd %d",fd); return ERROR; } msg= (struct my_msg *)malloc(sizeof(struct my_msg)); msg->type=SEEK; msg->data2=index; msg->ptr=&seek_msg; seek_msg.curr_offset=files[index]->offset; seek_msg.new_offset=offset; seek_msg.whence=whence; Send((void *)msg, -FILE_SERVER); int result=msg->type; if(result==ERROR) { TtyPrintf(0,"\nSeek Unsuccessful"); } else { files[index]->offset=result; } free(msg); return result; }
int smbc_fstatvfs(int fd, struct statvfs *st) { SMBCFILE * file = find_fd(fd); return smbc_getFunctionFstatVFS(statcont)(statcont, file, st); }
extern int Open(char *pathname) { /* Check if the length of pathname is legal.*/ if(strlen(pathname) > (MAXPATHNAMELEN - 1)) return ERROR; /* Create a fixed-length buffer to contain pathname for server's using. */ char *path = (char *)malloc(MAXPATHNAMELEN); memcpy(path, pathname, strlen(pathname)+1); /* Tell there's available file descriptor number.*/ int index = -1; index=find_fd(0); /* No available file descriptor number.*/ if(index == -1) { TtyPrintf(0,"\nERROR:Cannot Open the file! Already Opened %d Files",MAX_OPEN_FILES); return ERROR; } /* Process multiple slashes first */ //reduce_slash(path); printf("\npath passed is %s",path); /* Create msg to be sent to server.*/ struct my_msg *msg_open; msg_open = (struct my_msg *)malloc(sizeof(struct my_msg)); msg_open->type = OPEN; msg_open->data2 = current_inode_num; msg_open->ptr = (void *)path; /* Get result from file system. */ /* if pid is positive, normal.*/ /* if pid is negative, it is interpreted as the process ID of the process to which to send the message.*/ int result; Send((void *)msg_open, -FILE_SERVER); printf("\nValue returned is %d",msg_open->type); if(msg_open->type ==0) { struct file_info *file; file = (struct file_info *)malloc(sizeof(struct file_info)); file->inode_num = (msg_open->data2); // inode number is from server. file->offset = 0; // be initialized as 0 when a file is open. files[index] = file; track_fd[index]=fd_counter++; printf("\nvalue of fd is %d with inode_num %d",track_fd[index],files[index]->inode_num); result= track_fd[index]; } else { TtyPrintf(0,"\nERROR:File %s does not exist!",path); result= ERROR; } free(path); free(msg_open); return result; }
void fake_fd( int fd, int events ) { int n = find_fd( fd ); assert( n >= 0 ); fdparms[n].faked |= events; }
int smbc_closedir(int dh) { SMBCFILE * file = find_fd(dh); del_fd(dh); return smbc_getFunctionClosedir(statcont)(statcont, file); }
int smbc_lseekdir(int fd, off_t offset) { SMBCFILE * file = find_fd(fd); return smbc_getFunctionLseekdir(statcont)(statcont, file, offset); }
int smbc_close(int fd) { SMBCFILE * file = find_fd(fd); del_fd(fd); return smbc_getFunctionClose(statcont)(statcont, file); }
/* read from a file */ static NTSTATUS svfs_read(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, union smb_read *rd) { struct svfs_private *p = ntvfs->private_data; struct svfs_file *f; ssize_t ret; if (rd->generic.level != RAW_READ_READX) { return NT_STATUS_NOT_SUPPORTED; } f = find_fd(p, rd->readx.in.file.ntvfs); if (!f) { return NT_STATUS_INVALID_HANDLE; } ret = pread(f->fd, rd->readx.out.data, rd->readx.in.maxcnt, rd->readx.in.offset); if (ret == -1) { return map_nt_error_from_unix(errno); } rd->readx.out.nread = ret; rd->readx.out.remaining = 0; /* should fill this in? */ rd->readx.out.compaction_mode = 0; return NT_STATUS_OK; }
/* close a file */ static NTSTATUS svfs_close(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, union smb_close *io) { struct svfs_private *p = ntvfs->private_data; struct svfs_file *f; if (io->generic.level != RAW_CLOSE_CLOSE) { /* we need a mapping function */ return NT_STATUS_INVALID_LEVEL; } f = find_fd(p, io->close.in.file.ntvfs); if (!f) { return NT_STATUS_INVALID_HANDLE; } if (close(f->fd) == -1) { return map_nt_error_from_unix(errno); } DLIST_REMOVE(p->open_files, f); talloc_free(f->name); talloc_free(f); return NT_STATUS_OK; }
ssize_t smbc_write(int fd, const void *buf, size_t bufsize) { SMBCFILE * file = find_fd(fd); return smbc_getFunctionWrite(statcont)(statcont, file, buf, bufsize); }
int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count) { SMBCFILE * file = find_fd(dh); return smbc_getFunctionGetdents(statcont)(statcont, file, dirp, count); }
off_t smbc_lseek(int fd, off_t offset, int whence) { SMBCFILE * file = find_fd(fd); return smbc_getFunctionLseek(statcont)(statcont, file, offset, whence); }
ssize_t smbc_read(int fd, void *buf, size_t bufsize) { SMBCFILE * file = find_fd(fd); return smbc_getFunctionRead(statcont)(statcont, file, buf, bufsize); }
fsal_status_t vfs_getattr2(struct fsal_obj_handle *obj_hdl, struct attrlist *attrs) { struct vfs_fsal_obj_handle *myself; fsal_status_t status = {0, 0}; bool has_lock = false; bool need_fsync = false; bool closefd = false; int my_fd; myself = container_of(obj_hdl, struct vfs_fsal_obj_handle, obj_handle); if (obj_hdl->fsal != obj_hdl->fs->fsal) { LogDebug(COMPONENT_FSAL, "FSAL %s getattr for handle belonging to FSAL %s, ignoring", obj_hdl->fsal->name, obj_hdl->fs->fsal != NULL ? obj_hdl->fs->fsal->name : "(none)"); goto out; } /* Get a usable file descriptor (don't need to bypass - FSAL_O_ANY * won't conflict with any share reservation). */ status = find_fd(&my_fd, obj_hdl, false, NULL, FSAL_O_ANY, &has_lock, &need_fsync, &closefd, false); if (FSAL_IS_ERROR(status)) { if (obj_hdl->type == SYMBOLIC_LINK && status.major == ERR_FSAL_PERM) { /* You cannot open_by_handle (XFS on linux) a symlink * and it throws an EPERM error for it. * open_by_handle_at does not throw that error for * symlinks so we play a game here. Since there is * not much we can do with symlinks anyway, * say that we did it but don't actually * do anything. In this case, return the stat we got * at lookup time. If you *really* want to tweek things * like owners, get a modern linux kernel... */ status = fsalstat(ERR_FSAL_NO_ERROR, 0); } goto out; } status = fetch_attrs(myself, my_fd, attrs); out: if (closefd) close(my_fd); if (has_lock) PTHREAD_RWLOCK_unlock(&obj_hdl->lock); return status; }
int /* O - 1 on success, 0 on error */ cupsdAddSelect(int fd, /* I - File descriptor */ cupsd_selfunc_t read_cb, /* I - Read callback */ cupsd_selfunc_t write_cb,/* I - Write callback */ void *data) /* I - Data to pass to callback */ { _cupsd_fd_t *fdptr; /* File descriptor record */ #ifdef HAVE_EPOLL int added; /* 1 if added, 0 if modified */ #endif /* HAVE_EPOLL */ /* * Range check input... */ printf("[select.c::cupsdAddSelect()]: fd=%d, read_cb=%p, write_cb=%p, data=%p\n", fd, read_cb, write_cb, data); cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddSelect: fd=%d, read_cb=%p, write_cb=%p, data=%p", fd, read_cb, write_cb, data); if (fd < 0) return (0); /* * See if this FD has already been added... */ if ((fdptr = find_fd(fd)) == NULL) { /* * No, add a new entry... */ if ((fdptr = calloc(1, sizeof(_cupsd_fd_t))) == NULL) return (0); fdptr->fd = fd; fdptr->use = 1; if (!cupsArrayAdd(cupsd_fds, fdptr)) { cupsdLogMessage(CUPSD_LOG_EMERG, "Unable to add fd %d to array!", fd); free(fdptr); return (0); } #ifdef HAVE_EPOLL added = 1; } else added = 0; #else }
int smbc_fremovexattr(int fd, const char *name) { SMBCFILE * file = find_fd(fd); if (file == NULL) { errno = EBADF; return -1; } return smbc_getFunctionRemovexattr(statcont)(statcont, file->fname, name); }
static void _handle_syscall_ioctl(sandbox_t *sandbox, pid_t pid) { int fd = ptrace_get_syscall_arg(pid, 0); sandbox->binder_fd = find_fd(pid, DEV_BINDER); int binder_fd = sandbox->binder_fd; if (fd == sandbox->binder_fd) { LOGD("[%d] binder ioctl %d; binder_fd=%d\n", pid, fd, binder_fd); handle_binder(sandbox, pid); } else { LOGD("[%d] ioctl; binder_fd=%d\n", pid, binder_fd); } }
void del_fd( int fd ) { int n = find_fd( fd ); assert( n >= 0 ); npolls--; #ifdef HAVE_SYS_POLL_H memmove(pollfds + n, pollfds + n + 1, (npolls - n) * sizeof(*pollfds)); #endif memmove(fdparms + n, fdparms + n + 1, (npolls - n) * sizeof(*fdparms)); changed = 1; }
extern int Create(char *pathname) { int i; /* Check if the length of pathname is legal.*/ if(strlen(pathname) > (MAXPATHNAMELEN - 1)) return ERROR; /* Create a fixed-length buffer to contain pathname for server's using. */ char *path = (char *)malloc(MAXPATHNAMELEN); memcpy(path, pathname, strlen(pathname)+1); /* Tell there's available file descriptor number.*/ int index = find_fd(0); printf("\nval of index is %d\n",index); /* No available file descriptor number.*/ if(index == -1) { return ERROR; } /* Process multiple slashes first */ //reduce_slash(path); /* Create msg to be sent to server.*/ struct my_msg *msg_create; msg_create = (struct my_msg *)malloc(sizeof(struct my_msg)); msg_create->type = CREATE; msg_create->data2 = current_inode_num; msg_create->ptr = (void *)path; /* What the server should do is to check if the named file exsits. if yes, truncate it to 0 and open an empty file. If not, create a new file with the given name and assign it a descriptor number.*/ int res=Send((void *)msg_create,FILE_SERVER); if(msg_create->type == 0) { struct file_info *file; file = (struct file_info *)malloc(sizeof(struct file_info)); file->inode_num = msg_create->data2; // inode number is from server. file->offset = 0; // be initialized as 0 when a file is created. files[index] = file; track_fd[index] = ++fd_counter; // make this file desciptor number valid. return track_fd[index]; } else { TtyPrintf(0,"\nERROR: File %s could not be created!",pathname); return ERROR; } }
void SendToAll(struct Package package) { int i; int conn_fd; for(i = 0; i < user_num; i++) { conn_fd = find_fd(OnlineUser[i].username); send(conn_fd, &package, sizeof(package),0); } }
int smbc_flistxattr(int fd, char *list, size_t size) { SMBCFILE * file = find_fd(fd); if (file == NULL) { errno = EBADF; return -1; } return smbc_getFunctionListxattr(statcont)(statcont, file->fname, list, size); }
/* removes fd from register, returning its filename and status via filename and status arguments. */ void deregister_fd(int fd) { struct pid_reg **pid_iter = find_pid(getpid()); if (*pid_iter) { struct fd_reg **fd_iter = find_fd(*pid_iter, fd); if (*fd_iter) { remove_fd(fd_iter); if ((*pid_iter)->fd_head == 0) remove_pid(pid_iter); } } }
int smbc_fgetxattr(int fd, const char *name, const void *value, size_t size) { SMBCFILE * file = find_fd(fd); if (file == NULL) { errno = EBADF; return -1; } return smbc_getFunctionGetxattr(statcont)(statcont, file->fname, name, value, size); }
/* set info on a open file */ static NTSTATUS svfs_setfileinfo(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, union smb_setfileinfo *info) { struct svfs_private *p = ntvfs->private_data; struct svfs_file *f; struct utimbuf unix_times; CHECK_READ_ONLY(req); f = find_fd(p, info->generic.in.file.ntvfs); if (!f) { return NT_STATUS_INVALID_HANDLE; } switch (info->generic.level) { case RAW_SFILEINFO_END_OF_FILE_INFO: case RAW_SFILEINFO_END_OF_FILE_INFORMATION: if (ftruncate(f->fd, info->end_of_file_info.in.size) == -1) { return map_nt_error_from_unix(errno); } break; case RAW_SFILEINFO_SETATTRE: unix_times.actime = info->setattre.in.access_time; unix_times.modtime = info->setattre.in.write_time; if (unix_times.actime == 0 && unix_times.modtime == 0) { break; } /* set modify time = to access time if modify time was 0 */ if (unix_times.actime != 0 && unix_times.modtime == 0) { unix_times.modtime = unix_times.actime; } /* Set the date on this file */ if (svfs_file_utime(f->fd, &unix_times) != 0) { return NT_STATUS_ACCESS_DENIED; } break; default: DEBUG(2,("svfs_setfileinfo: level %d not implemented\n", info->generic.level)); return NT_STATUS_NOT_IMPLEMENTED; } return NT_STATUS_OK; }
extern int Read(int fd, void *buf, int size) { struct read_struct read_m; /* Create msg to be sent to server.*/ struct my_msg *msg_read; char *temp_buf; int index = find_fd(fd); /* No match for provided file descriptor number.*/ if(index == -1) { TtyPrintf(0,"\nERROR: Invalid Fd %d",fd); return ERROR; } if(buf==NULL) { TtyPrintf(0,"\nERROR: Invalid memory pointer!"); return ERROR; } msg_read = (struct my_msg *)malloc(sizeof(struct my_msg)); msg_read->type = READ; msg_read->data2 = files[index]->inode_num; printf("\nassigned value is %d\n",msg_read->data2); msg_read->ptr = &read_m; temp_buf=(char *)malloc(size); read_m.offset=files[index]->offset; read_m.len=size; read_m.buff=temp_buf; Send(msg_read,-FILE_SERVER); int result=msg_read->type; if(result==ERROR) { TtyPrintf(0,"\nERROR: Read Unsuccessful!"); } else { memcpy(buf,temp_buf,result); files[index]->offset+=msg_read->type; } free(temp_buf); free(msg_read); return result; }
extern int Write(int fd, void *buf, int size) { /* Create msg to be sent to server. */ struct my_msg *msg_write; struct read_struct write_m; int index = find_fd(fd); /* No match for provided file descriptor number.*/ if(index == -1) { TtyPrintf(0,"\nERROR: Invalid Fd %d",fd); return ERROR; } if(buf==NULL) { TtyPrintf(0,"\nERROR: Invalid memory pointer!"); return ERROR; } char *msg_buf=(char *)malloc(size*sizeof(char)); msg_write = (struct my_msg *)malloc(sizeof(struct my_msg)); memcpy(msg_buf,buf,strlen(buf)); msg_write->type = WRITE; msg_write->data2 = files[index]->inode_num; //msg_write->ptr = (void *)files[index]; msg_write->ptr = &write_m; write_m.offset=files[index]->offset; write_m.len=size; write_m.buff=msg_buf; Send((void *)msg_write,-FILE_SERVER); int result=msg_write->type; if(result==ERROR) { TtyPrintf(0,"\nWrite Unsuccessful"); } else { files[index]->offset+=msg_write->type; TtyPrintf(0,"\nWrite Successfully Completed!"); } free(msg_buf); free(msg_write); return result; }