int initbbsinfo(struct bbsinfo *bbsinfo) { bzero(bbsinfo, sizeof (*bbsinfo)); binfo = bbsinfo; if (chdir(MY_BBS_HOME) == -1) goto ERROR; bbsinfo->bcacheshm = get_old_shm(getBBSKey(BCACHE_SHM), sizeof (struct BCACHE)); if (bbsinfo->bcacheshm == NULL) goto ERROR; bbsinfo->bcache = bbsinfo->bcacheshm->bcache; bbsinfo->ucachehashshm = get_old_shm(getBBSKey(UCACHE_HASH_SHM), sizeof (struct UCACHEHASH)); if (bbsinfo->ucachehashshm == NULL) goto ERROR; bbsinfo->utmpshm = get_old_shm(getBBSKey(UTMP_SHM), sizeof (struct UTMPFILE)); if (bbsinfo->utmpshm == NULL) goto ERROR; bbsinfo->uindexshm = get_old_shm(getBBSKey(UINDEX_SHM), sizeof (struct UINDEX)); if (bbsinfo->uindexshm == NULL) goto ERROR; bbsinfo->wwwcache = get_old_shm(getBBSKey(WWWCACHE_SHM), sizeof (struct WWWCACHE)); if (bbsinfo->wwwcache == NULL) goto ERROR; if (mmapfile(".PASSWDS", &passwd_mf) < 0) goto ERROR; passwdptr = (struct userec *) passwd_mf.ptr; return 0; ERROR: mmapfile(NULL, &passwd_mf); if (bbsinfo->bcacheshm) shmdt(bbsinfo->bcacheshm); if (bbsinfo->ucachehashshm) shmdt(bbsinfo->ucachehashshm); if (bbsinfo->utmpshm) shmdt(bbsinfo->utmpshm); if (bbsinfo->uindexshm) shmdt(bbsinfo->uindexshm); if (bbsinfo->wwwcache) shmdt(bbsinfo->wwwcache); return -1; }
int main(int ac, char **av) { char *patch; size_t patchsize; int infd; size_t offset; if (ac != 5 && ac != 4) usage(); offset = get_num(av[2]); patch = mmapfile(av[3], &patchsize); if (av[4]) { size_t newsize = get_num(av[4]); if (newsize > patchsize) fprintf(stderr, "kallsyms: warning, size larger than patch\n"); if (newsize < patchsize) patchsize = newsize; } infd = open(av[1], O_RDWR); if (infd < 0) { fprintf(stderr, "Cannot open %s\n", av[1]); exit(1); } if (pwrite(infd, patch, patchsize, offset) != patchsize) { fprintf(stderr, "Cannot write patch to %s\n", av[1]); exit(1); } close(infd); return 0; }
int main (void) { struct mmapfile mf = { ptr:NULL, size:0 }; int i = 0; char *ptr, title[64]; FILE *fp; while (sina[i].title) { getfile(sina[i].url); if (!file_exist(TMPFILE)) { i++; continue; } if (NULL == (fp = fopen(POSTFILE, "w"))) return 0; if (mmapfile(TMPFILE, &mf) < 0) { i++; continue; } if (mf.size <= 0) { i++; continue; } ptr = mf.ptr; MMAP_TRY { if (NULL == (ptr = weather_date(fp, ptr, sina[i].title, sina[i].type))) { MMAP_UNTRY; mmapfile(NULL, &mf); continue; } weather_item(fp, ptr, sina[i].type); } MMAP_CATCH { } MMAP_END mmapfile(NULL, &mf); fclose(fp); sprintf(title, "%s城市24小时天气预报(%s)", sina[i].type ? "国际" : "国内", sina[i].title); postfile(POSTFILE, "deliver", BOARD, title); unlink(TMPFILE); unlink(POSTFILE); i++; } return 1; }
HI_RET himd(int argc , char* argv[]) { U32 ulAddr = 0; VOID* pMem = NULL; LENGTH_T len; if (argc < 2) { printf("usage: %s <address>. sample: %s 0x80040000\n", argv[0], argv[0]); EXIT("", -1); } if (argc == 3) { if ( StrToNumber(argv[2], &len) != HI_SUCCESS) { len = DEFAULT_MD_LEN; } } else { len = DEFAULT_MD_LEN; } if( StrToNumber(argv[1], &ulAddr) == HI_SUCCESS) { printf("====dump memory %#010lX====\n", ulAddr); #ifdef PC_EMULATOR #define SHAREFILE "../shm" printf("**** is Emulator, use share file : %s ****\n", SHAREFILE); pMem = mmapfile(SHAREFILE , len); if (NULL == pMem) { EXIT("Memory Map error.", -1); } pMem += ulAddr; #else pMem = memmap(ulAddr, len); if (NULL == pMem) { EXIT("Memory Map error.", -1); } #endif hi_hexdump(STDOUT, pMem, len, 16); } else { printf("Please input address like 0x12345\n"); } return 0; }
static conn_t * rmconn(conn_t * c) { mmapfile(NULL, &c->mf); close(c->sd); if (c->cache_header) free(c->cache_header); free(c); num_connections--; return NULL; }
static SPWAW_ERROR spwoob_load_raw_file_core (SPWOOB *oob, BYTE id, const char *file, ULONG rsize, load_raw_data_cb load) { SPWAW_ERROR rc = SPWERR_OK; SPWOOB_DATA *dst; char path[MAX_PATH+1]; int fd = -1; HANDLE maph; void *raw = NULL; CNULLARG (oob); CNULLARG (file); if (oob->data[id]) return (SPWERR_OK); dst = safe_malloc (SPWOOB_DATA); COOMGOTO (dst, "SPWOOB_DATA", handle_error); dst->id = id; dst->spwoob = oob; dst->rdata = safe_smalloc (void, rsize); COOMGOTO (dst, "storage for raw OOB data", handle_error); dst->rsize = rsize; memset (path, 0, sizeof (path)); snprintf (path, sizeof (path) - 1, "%s\\%s", oob->srcdir, file); fd = open (path, O_RDONLY|O_BINARY); if (fd < 0) FAILGOTO (SPWERR_FOFAILED, "open(oob file) failed", handle_error); raw = mmapfile (fd, &maph); if (!raw) FAILGOTO (SPWERR_FRFAILED, "mmapfile(oob file) failed", handle_error); memcpy (dst->rdata, raw, dst->rsize); unmmapfile ((void **)&raw, &maph); raw = NULL; close (fd); fd = -1; rc = load (dst); ERRORGOTO ("load_raw_data_cb()", handle_error); oob->data[id] = dst; oob->count++; return (SPWERR_OK); handle_error: if (dst) { if (dst->rdata) safe_free (dst->rdata); safe_free (dst); } if (raw) unmmapfile ((void **)&raw, &maph); if (fd != -1) close (fd); return (rc); }
/*memory dump bin*/ HI_RET himdb(int argc , char* argv[]) { U32 ulAddr = 0; VOID* pMem = NULL; VOID* pBase = NULL; LENGTH_T len = DEFAULT_MD_LEN; FILENAME_T fn; if (argc >= 3) { if ( StrToNumber(argv[2], &len) != HI_SUCCESS) { len = DEFAULT_MD_LEN; } if (argc == 4) { strcpy(fn, argv[3]); } else { sprintf(fn, "md_%s-%s", argv[1], argv[2]); } } else { printf("usage: %s <address> <len> [filename]. sample: %s 0x80040000 \n", argv[0], argv[0]); EXIT("", -1); } if( StrToNumber(argv[1], &ulAddr) == HI_SUCCESS) { printf("====dump memory:<%#010lX><%lu> to file:<%s>====\n", ulAddr, len, fn); #ifdef PC_EMULATOR #define SHAREFILE "../shm" printf("**** is Emulator, use share file : %s ****\n", SHAREFILE); pBase = mmapfile(SHAREFILE , len); pMem = pBase ; if (NULL == pMem) { EXIT("Memory Map error.", -1); } pMem = pBase + ulAddr; #else pBase = memmap(ulAddr, len); pMem = pBase ; if (NULL == pMem) { EXIT("Memory Map error.", -1); } #endif (void)hi_md2file(pMem, len, NULL, fn); } else { printf("Please input address like 0x12345\n"); } (void)munmap(pBase, len); return 0; }
/** * Maps a file into memory in either read or write mode. * * @param filename Name of the file to map. * @param len_ptr The number of bytes the returned pointer points to. * @param mode Can be either "r", "w", "rb" or "wb". If mode is "r", * len_ptr is taken as an input parameter (*len_ptr bytes * are allocated). * {NB I copied this from existing notes but surely shouldn't * the comment about len_ptr apply if mode is "w" not "r"? -- AH} * @return The contents of file in filename as a pointer to a * memory area. */ caddr_t mmapfile(char *filename, size_t *len_ptr, char *mode) { struct stat stat_buf; int fd; int binflag = 0; /* set to O_BINARY if we want to use the binary flag with open() */ caddr_t space; size_t map_len; /* should probably be off_t (for file sizes) rather than size_t (for size of objects), according to SUS */ /* allow for: r+b, w+b, rb+, wb+ */ binflag = (mode[1] == 'b' || (mode[1] == '+' && mode[2] == 'b') ) ? O_BINARY : 0; space = NULL; switch(mode[0]) { case 'r': fd = open(filename, O_RDONLY|binflag); if (fd == EOF) { Rprintf( "mmapfile()<storage.c>: Can't open file %s ... \n\tReason: ", filename); perror(NULL); } else if(fstat(fd, &stat_buf) == EOF) { Rprintf( "mmapfile()<storage.c>: Can't fstat() file %s ... \n\tReason: ", filename); perror(NULL); } else { *len_ptr = stat_buf.st_size; map_len = (stat_buf.st_size > 0) ? stat_buf.st_size : MMAP_EMPTY_LEN; /* if file is empty, mmap() beyond end of file, but report empty size to CL */ space = mmap(NULL, map_len, PROT_READ, MMAPFLAGS, fd, 0); } if (fd != EOF) close(fd); break; case 'w': if ((fd = open(filename, O_RDWR|O_CREAT|binflag, 0666)) == EOF) fd = creat(filename, 0666); if (fd == EOF) { Rprintf( "mmapfile()<storage.c>: Can't create file %s ...\n\tReason: ", filename); perror(NULL); } else { lseek(fd, *len_ptr - sizeof(int), SEEK_SET); write(fd, &fd, sizeof(int)); lseek(fd, 0, SEEK_SET); space = mmap(NULL, *len_ptr, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); } if (fd != EOF) close(fd); break; default: Rprintf( "mmapfile()<storage.c>: Mode '%s' is not supported ...\n", mode); } #if defined(__svr4__) if (space == MAP_FAILED) { #else if (space == (caddr_t)-1) { /* do we need this fallback ? */ #endif Rprintf( "mmapfile()<storage.c>: Can't mmap() file %s ...\n" "\tYou have probably run out of memory / address space!\n" "\tError Message: ", filename); perror(NULL); space = NULL; } return space; } /** * Maps a file into memory. * * This function does virtually the same as mmapfile (same parameters, same return * value), but the memory is taken with malloc(3), not with mmap(2). * * @see mmapfile */ caddr_t mallocfile(char *filename, size_t *len_ptr, char *mode) { struct stat stat_buf; int fd; int binflag = 0; /* set to O_BINARY if we want to use the binary flag with open() */ caddr_t space; /* allow for: r+b, w+b, rb+, wb+, rb, wb */ binflag = (mode[1] == 'b' || (mode[1] == '+' && mode[2] == 'b') ) ? O_BINARY : 0; space = NULL; switch(mode[0]) { case 'r': fd = open(filename, O_RDONLY|binflag); if (fd == EOF) { Rprintf( "storage:mallocfile():\n can't open %s -- ", filename); perror(NULL); } else if(fstat(fd, &stat_buf) == EOF) { Rprintf( "storage:mallocfile():\n can't stat %s -- ", filename); perror(NULL); } else { *len_ptr = stat_buf.st_size; space = (caddr_t)cl_malloc(*len_ptr); if (read(fd, space, *len_ptr) != *len_ptr) { Rprintf( "storage:mallocfile():\n couldn't read file contents -- "); perror(NULL); free(space); space = NULL; } } if (fd != EOF) close(fd); break; case 'w': if ((fd = open(filename, O_RDWR|O_CREAT|binflag, 0666)) == EOF) fd = creat(filename, 0666); if(fd == EOF) { Rprintf( "storage:mallocfile():\n can't open/create %s for writing -- ", filename); perror(NULL); } else { space = (caddr_t)cl_malloc(*len_ptr); if (write(fd, space, *len_ptr) != *len_ptr) { Rprintf( "storage:mallocfile():\n couldn't write file -- "); perror(NULL); free(space); space = NULL; } } if (fd != EOF) close(fd); break; default: Rprintf( "storage:mallocfile():\n mode %s is not supported\n", mode); } return space; } /** * Reads the contents of a file into memory represented by blob. * * You can choose the memory allocation method - MMAPPED is faster, but * writeable areas of memory should be taken with care. MALLOCED is * slower (and far more space consuming), but writing data into malloced * memory is no problem. * * In Windows, the read is always binary-mode. * * @param filename The file to read in. * @param allocation_method MMAPPED or MALLOCED (see function description) * @param item_size This is used for MemBlob access methods, it * is simply copied into the MemBlob data * structure. * @param blob The MemBlob to read the file into. It must not * be in use -- the fields are overwritten. * @return 0 on failure, 1 if everything went fine. */ int read_file_into_blob(char *filename, int allocation_method, int item_size, MemBlob *blob) { int result; assert("You must not pass a NULL blob!" && (blob != NULL)); blob->item_size = item_size; blob->allocation_method = allocation_method; blob->writeable = 0; blob->changed = 0; if (allocation_method == MMAPPED) blob->data = (int *)mmapfile(filename, &(blob->size), "rb"); else if (allocation_method == MALLOCED) blob->data = (int *)mallocfile(filename, &(blob->size), "rb"); else { Rprintf( "storage:read_file_into_blob():\n" " allocation method %d is not supported\n", allocation_method); return 0; } if (blob->data == NULL) { result = 0; blob->nr_items = 0; blob->allocation_method = UNALLOCATED; } else { result = 1; blob->nr_items = (item_size == SIZE_BIT) ? blob->size * 8 : blob->size / item_size; } return result; }