标签:
/*
* sysutil.c
*
* Routines to make the libc/syscall API more pleasant to use. Long term,
* more libc/syscalls will go in here to reduce the number of .c files with
* dependencies on libc or syscalls.
*/
#include "config.h"
#include <sys/mman.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/vfs.h>
#include <ustat.h>
#include <openssl/sha.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/wait.h>
#include <dirent.h>
#include <libaio.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/syscall.h>
#define DBG_SUBSYS S_YLIB
#include "sysy_conf.h"
#include "sysutil.h"
#include "configure.h"
#include "adt.h"
#include "yid.h"
#include "sysy_lib.h"
#include "dbg.h"
int srv_running = 1;
#undef MAXSIZE_LOGFILE
int sy_peek(int sd, char *buf, uint32_t buflen)
{
int ret;
uint32_t toread;
ret = ioctl(sd, FIONREAD, &toread);
if (ret == -1) {
ret = errno;
GOTO(err_ret, ret);
}
toread = toread < buflen ? toread : buflen;
ret = recv(sd, buf, toread, MSG_PEEK);
if (ret == -1) {
ret = errno;
DERROR("peek errno %d\n", ret);
GOTO(err_ret, ret);
}
if (ret == 0) {
ret = ECONNRESET;
goto err_ret;
}
return ret;
err_ret:
return -ret;
}
int sy_shutdown(int sd)
{
int ret;
while (1) {
ret = shutdown(sd, SHUT_RDWR);
if (ret == -1) {
ret = errno;
if (ret != ECONNRESET)
GOTO(err_ret, ret);
else
break;
} else
break;
}
return 0;
err_ret:
return ret;
}
int sy_close(int fd)
{
int ret;
while (1) {
ret = close(fd);
if (ret == -1) {
ret = errno;
if(ret == EINTR)
continue;
GOTO(err_ret, ret);
} else
break;
}
return 0;
err_ret:
return ret;
}
void sy_close_failok(int fd)
{
(void) close(fd);
}
void sy_msleep(uint32_t milisec)
{
int ret;
time_t sec;
struct timespec req = {0, 0};
sec = (int) (milisec/1000);
milisec = milisec - (sec * 1000);
req.tv_sec = sec;
req.tv_nsec = milisec * 1000000L;
while ((ret = nanosleep(&req, &req)) == -1) {
ret = errno;
DWARN("(%d) %s\n", ret, strerror(ret));
continue;
}
}
int sy_isidle()
{
int ret;
double one, five, fifteen;
int active, total, last;
FILE *fp;
fp = fopen("/proc/loadavg", "r");
if (fp == NULL) {
DERROR("/proc/loadavg not existed.\n");
return 0;
}
ret = fscanf(fp, "%lf %lf %lf %d/%d %d", &one, &five, &fifteen,
&active, &total, &last);
YASSERT( ret == 6);
fclose(fp);
if (one > 1.0)
return 0;
return 1;
}
int sy_is_mountpoint(const char *path, long f_type)
{
int ret, ismp = 0;
char parent[MAX_PATH_LEN];
struct stat st1, st2;
struct statfs vfs;
ret = statfs(path, &vfs);
if (ret == -1) {
ret = errno;
GOTO(out, ret);
}
if (vfs.f_type != f_type) {
DBUG("File system type dismatched: %s [%llu/%llu].\n",
path, (LLU)vfs.f_type, (LLU)f_type);
ret = EINVAL;
goto out;
}
ret = stat(path, &st1);
if (ret == -1) {
ret = errno;
GOTO(out, ret);
}
snprintf(parent, MAX_PATH_LEN, "%s/..", path);
ret = stat(parent, &st2);
if (ret == -1) {
ret = errno;
GOTO(out, ret);
}
// DINFO("%lu %lu\n", st1.st_dev, st2.st_dev);
if (st1.st_dev != st2.st_dev)
ismp = 1;
else
DWARN("%s and %s have the same st_dev.\n", path, parent);
out:
if (!ismp)
DBUG("%s is not a mountpoint.\n", path);
return ismp;
}
inline int sy_vec_update(uint32_t newoff, uint32_t newlen, uint32_t *oldoff, uint32_t *oldlen)
{
uint32_t newend, oldend;
YASSERT(oldoff != NULL);
YASSERT(oldlen != NULL);
newend = newoff + newlen;
oldend = *oldoff + *oldlen;
if (newoff < *oldoff)
*oldoff = newoff;
if (newend > oldend)
oldend = newend;
*oldlen = oldend - *oldoff;
return 0;
}
inline void *_memset(void *s, int c, size_t n)
{
return memset(s, c, n);
}
inline void *_memmove(void *dest, const void *src, size_t n)
{
return memmove(dest, src, n);
}
inline void *_memcpy(void *dest, const void *src, size_t n)
{
return memcpy(dest, src, n);
}
inline ssize_t _splice(int fd_in, loff_t *off_in, int fd_out,
loff_t *off_out, size_t len, unsigned int flags)
{
int ret;
while (1) {
ret = splice(fd_in, off_in, fd_out, off_out, len, flags);
if (ret == -1) {
ret = errno;
if (ret == EINTR) {
DERROR("interrupted");
continue;
} else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline int _epoll_wait(int epfd, struct epoll_event *events,
int maxevents, int timeout)
{
return epoll_wait(epfd, events, maxevents, timeout);
}
inline ssize_t _read(int fd, void *buf, size_t count)
{
int ret;
while (1) {
ret = read(fd, buf, count);
if (ret == -1) {
ret = errno;
if (ret == EINTR)
continue;
else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline ssize_t _write(int fd, const void *buf, size_t count)
{
int ret;
while (1) {
ret = write(fd, buf, count);
if (ret == -1) {
ret = errno;
if (ret == EINTR)
continue;
else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline ssize_t _pread(int fd, void *buf, size_t count, off_t offset)
{
int ret;
while (1) {
ret = pread(fd, buf, count, offset);
if (ret == -1) {
ret = errno;
if (ret == EINTR)
continue;
else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline ssize_t _pwrite(int fd, const void *buf, size_t count, off_t offset)
{
int ret;
while (1) {
ret = pwrite(fd, buf, count, offset);
if (ret == -1) {
ret = errno;
if (ret == EINTR)
continue;
else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline ssize_t _send(int sockfd, const void *buf, size_t len, int flags)
{
int ret;
ANALYSIS_BEGIN(0);
while (1) {
ret = send(sockfd, buf, len, flags);
if (ret == -1) {
ret = errno;
if (ret == EINTR) {
DERROR("interrupted");
continue;
} else if (ret == EAGAIN || ret == ECONNREFUSED
|| ret == EHOSTUNREACH)
goto err_ret;
else
GOTO(err_ret, ret);
}
break;
}
ANALYSIS_END(0, 5000, NULL);
return ret;
err_ret:
return -ret;
}
inline ssize_t _recv(int sockfd, void *buf, size_t len, int flags)
{
int ret;
while (1) {
ret = recv(sockfd, buf, len, flags);
if (ret == -1) {
ret = errno;
if (ret == EINTR) {
DERROR("interrupted");
continue;
} else
goto err_ret;
}
break;
}
if (ret == 0) {
ret = ECONNRESET;
goto err_ret;
}
return ret;
err_ret:
return -ret;
}
inline ssize_t _recvmsg(int sockfd, struct msghdr *msg, int flags)
{
int ret;
while (1) {
ret = recvmsg(sockfd, msg, flags);
if (ret == -1) {
ret = errno;
if (ret == EINTR) {
DERROR("interrupted");
continue;
} else
goto err_ret;
}
break;
}
if (ret == 0) {
ret = ECONNRESET;
goto err_ret;
}
return ret;
err_ret:
return -ret;
}
inline size_t _strlen(const char *s)
{
if (s == NULL)
return 0;
return strlen(s);
}
inline char *_strcpy(char *dest, const char *src)
{
return strcpy(dest, src);
}
inline char *_strncpy(char *dest, const char *src, size_t n)
{
return strncpy(dest, src, n);
}
inline int _gettimeofday(struct timeval *tv, struct timezone *tz)
{
return gettimeofday(tv, tz);
}
inline int _strcmp(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}
inline int _epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
return epoll_ctl(epfd, op, fd, event);
}
inline int _posix_memalign(void **memptr, size_t alignment, size_t size)
{
return posix_memalign(memptr, alignment, size);
}
inline ssize_t _writev(int fd, const struct iovec *iov, int iovcnt)
{
int ret;
while (1) {
ret = writev(fd, iov, iovcnt);
if (ret == -1) {
ret = errno;
if (ret == EINTR)
continue;
else if (ret == EAGAIN)
goto err_ret;
else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline ssize_t _readv(int fd, const struct iovec *iov, int iovcnt)
{
int ret;
while (1) {
ret = readv(fd, iov, iovcnt);
if (ret == -1) {
ret = errno;
if (ret == EINTR)
continue;
else if (ret == EAGAIN)
goto err_ret;
else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline ssize_t _sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
{
int ret;
while (1) {
ret = sendfile(out_fd, in_fd, offset, count);
if (ret == -1) {
ret = errno;
if (ret == EINTR)
continue;
else if (ret == EAGAIN)
goto err_ret;
else
GOTO(err_ret, ret);
}
break;
}
return ret;
err_ret:
return -ret;
}
inline int _sem_wait(sem_t *sem)
{
int ret;
while (1) {
ret = sem_wait(sem);
if (ret) {
ret = errno;
if (ret == EINTR)
continue;
else
GOTO(err_ret, ret);
}
break;
}
return 0;
err_ret:
return ret;
}
inline int _sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
{
int ret;
while (1) {
ret = sem_timedwait(sem, abs_timeout);
if (ret) {
ret = errno;
if (ret == ETIMEDOUT)
goto err_ret;
else if (ret == EINTR)
continue;
else
GOTO(err_ret, ret);
}
break;
}
return 0;
err_ret:
return ret;
}
inline int _sem_timedwait1(sem_t *sem, int tmo)
{
int ret;
struct timespec ts;
ts.tv_sec = time(NULL) + tmo;
ts.tv_nsec = 0;
while (1) {
ret = sem_timedwait(sem, &ts);
if (ret) {
ret = errno;
if (ret == ETIMEDOUT)
goto err_ret;
else if (ret == EINTR)
continue;
else
GOTO(err_ret, ret);
}
break;
}
return 0;
err_ret:
return ret;
}
inline void exit_handler(int sig)
{
DWARN("got signal %d, exiting\n", sig);
srv_running = 0;
}
/*
*para path should as /xxx/xxx
*there should only has file type DT_REG
*or DT_DIR in path, or will crash
* */
int _delete_path(const char *path)
{
char tmp[MAX_PATH_LEN] = {0};
DIR *dir;
struct dirent *ptr;
int ret;
struct stat stbuf;
/*fstat(path,&stat)
*S_ISDIR(stat.st_mode);
* */
dir = opendir(path);
if (!dir) {
if (errno == ENOTDIR)
unlink(path);
return 0;
}
while((ptr = readdir(dir))!=NULL) {
if (strcmp(ptr->d_name,".")==0
||strcmp(ptr->d_name,"..")==0)
continue;
sprintf(tmp,"%s/%s",path, ptr->d_name);
ret = stat(tmp, &stbuf);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
if (S_ISREG(stbuf.st_mode)) {
ret = unlink(tmp);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
} else if (S_ISDIR(stbuf.st_mode)) {
_delete_path(tmp);
}
}
closedir(dir);
ret = rmdir(path);
if (ret < 0){
ret = errno;
GOTO(err_ret, ret);
}
return 0;
err_ret:
return ret;
}
int path_action(char *path, fn_t fn, void *arg)
{
char tmp[MAX_PATH_LEN] = {0};
DIR *dir;
struct dirent *ptr;
/*fstat(path,&stat)
*S_ISDIR(stat.st_mode);
* */
dir = opendir(path);
if (!dir) {
if (errno == ENOTDIR)
fn(tmp, arg);
return 0;
}
while((ptr = readdir(dir))!=NULL) {
if (strcmp(ptr->d_name,".")==0
||strcmp(ptr->d_name,"..")==0)
continue;
sprintf(tmp,"%s/%s",path,ptr->d_name);
if (ptr->d_type == DT_REG)
fn(tmp, arg);
else if (ptr->d_type == DT_DIR)
path_action(tmp, fn, arg);
}
closedir(dir);
return 0;
}
int verid64_write(const char *path, const verid64_t *id)
{
int ret, fd;
char name[MAX_NAME_LEN];
ret = path_validate(path, 0, 1);
if (ret)
GOTO(err_ret, ret);
fd = creat(path, 0644);
if (fd == -1) {
if (ret == ENOENT)
goto err_ret;
else {
DERROR("path %s\n", path);
GOTO(err_ret, ret);
}
}
UNIMPLEMENTED(__NULL__); /*crc*/
sprintf(name, "%llu_v%u\n", (LLU)id->id, id->version);
ret = _pwrite(fd, name, strlen(name), 0);
if (ret < 0) {
ret = errno;
GOTO(err_fd, ret);
}
return 0;
err_fd:
close(fd);
unlink(path);
err_ret:
return ret;
}
int verid64_load(const char *path, verid64_t *id)
{
int ret, fd;
char name[MAX_NAME_LEN];
struct stat stbuf;
ret = path_validate(path, 0, 1);
if (ret)
GOTO(err_ret, ret);
fd = open(path, O_RDONLY);
if (fd == -1) {
ret = errno;
if (ret == ENOENT)
goto err_ret;
else {
DERROR("path %s\n", path);
GOTO(err_ret, ret);
}
}
UNIMPLEMENTED(__NULL__); /*crc*/
ret = fstat(fd, &stbuf);
if (ret < 0) {
ret = errno;
DERROR("path %s\n", path);
GOTO(err_ret, ret);
}
ret = _pread(fd, name, stbuf.st_size, 0);
if (ret < 0) {
ret = errno;
GOTO(err_fd, ret);
}
memset(id, 0x0, sizeof(*id));
#ifdef __x86_64__
ret = sscanf(name, "%lu_v%u", &id->id, &id->version);
#else
ret = sscanf(name, "%llu_v%u", &id->id, &id->version);
#endif
if (ret != 2) {
DERROR("path %s\n", path);
ret = EBADF;
GOTO(err_fd, ret);
}
return 0;
err_fd:
close(fd);
unlink(path);
err_ret:
return ret;
}
int _set_file(const char* path, const char *key, const char* value)
{
int fd, ret, done;
FILE *fp = NULL;
char buf[2048], tvalue[1024], tkey[1024];
long begin = 0, left = 0;
char *ptr, *str __attribute__((unused));
(void)fd;
fp = fopen(path,"r+");
if (fp == 0){
ret = errno;
GOTO(err_fd, ret);
}
fseek(fp,0,SEEK_END);
left = ftell(fp);
rewind(fp);
done = 0;
begin = 0;
while(feof(fp)==0){
begin = ftell(fp);
memset(buf,0x00,2048);
memset(tvalue,0x00,1024);
memset(tkey,0x00,1024);
str = fgets(buf,2048,fp);
sscanf(buf,"%s %s",tkey, tvalue);
if(strcmp(tkey,key)==0){
left = left - ftell(fp);
done = 1;
break;
}
}
if (!done){
left = left - ftell(fp);
};
ret = ymalloc((void**)&ptr,left+strlen(key)+strlen(value)+2);
if(ret )
GOTO(err_malloc,ret);
sprintf(ptr,"%s %s\n",key,value);
if (left){
ret = fread(ptr+strlen(value)+strlen(key)+2, 1, left,fp);
if (ret != left){
ret = errno;
GOTO(err_fread,ret);
}
}
ret = fseek(fp,begin,SEEK_SET);
ret = fwrite(ptr,1, left+strlen(value)+strlen(key)+2,fp);
if (ret != (int)(left+strlen(value)+strlen(key)+2)){
ret = errno;
GOTO(err_fread,ret);
}
ret = ftruncate(fileno(fp),begin + left + strlen(value)+strlen(key)+2);
fclose(fp);
yfree((void**)&ptr);
return 0;
err_fread:
fclose(fp);
yfree((void**)&ptr);
return ret;
err_malloc:
fclose(fp);
return ret;
err_fd:
return ret;
}
/*This is a fucked interface, but it is prety
*/
int _get_file2(const char *path, const char *key, char *value1, char *value2)
{
int ret, num = 0;
FILE *fp;
fp = fopen(path, "r+");
if (fp == NULL) {
ret = errno;
GOTO(err_ret, ret);
}
char _key[MAX_PATH_LEN];
char _value1[MAX_PATH_LEN];
char _value2[MAX_PATH_LEN];
char *str __attribute__((unused));
char _buf[3*MAX_PATH_LEN];
ret = ENOENT;
while(feof(fp) == 0) {
memset(_key, 0x0, MAX_PATH_LEN);
memset(_value1, 0x0, MAX_PATH_LEN);
memset(_value2, 0x0, MAX_PATH_LEN);
str = fgets(_buf, 3*MAX_PATH_LEN, fp);
num = sscanf(_buf, "%s %s %s", _key, _value1, _value2);
if (num != 3) {
fclose(fp);
return ENOENT;
}
if (strcmp(key, _key) == 0) {
memcpy(value1, _value1, strlen(_value1));
memcpy(value2, _value2, strlen(_value2));
return 0;
}
}
fclose(fp);
return ENOENT;
err_ret:
return ret;
}
/*another fucked version
*/
int _get_file3(const char *path, const char *key, char *value1, char *value2, char *value3)
{
int ret, num = 0;
FILE *fp;
char _key[MAX_PATH_LEN];
char _value1[MAX_PATH_LEN];
char _value2[MAX_PATH_LEN];
char _value3[MAX_PATH_LEN];
char *str __attribute__((unused));
char _buf[3*MAX_PATH_LEN];
fp = fopen(path, "r+");
if (fp == NULL) {
ret = errno;
GOTO(err_ret, ret);
}
ret = ENOENT;
while(feof(fp) == 0) {
memset(_key, 0x0, MAX_PATH_LEN);
memset(_value1, 0x0, MAX_PATH_LEN);
memset(_value2, 0x0, MAX_PATH_LEN);
str = fgets(_buf, 3*MAX_PATH_LEN, fp);
num = sscanf(_buf, "%s %s %s %s", _key, _value1, _value2, _value3);
if (num != 4) {
fclose(fp);
return ENOENT;
}
if (strcmp(key, _key) == 0) {
memcpy(value1, _value1, strlen(_value1));
memcpy(value2, _value2, strlen(_value2));
memcpy(value3, _value3, strlen(_value3));
return 0;
}
}
fclose(fp);
return ENOENT;
err_ret:
return ret;
}
/*there should be a len of value, but we still use
*this interface, most of case, you know what you are doing
*/
int _get_file0(const char *path, char *value)
{
int fd,ret;
struct stat stbuf;
fd = open(path, O_RDONLY, 0644);
if (fd < 0) {
ret = errno;
GOTO(err_fd, ret);
}
ret = fstat(fd, &stbuf);
if (ret < 0) {
ret = errno;
GOTO(err_fd, ret);
}
ret = read(fd, value, stbuf.st_size);
if (ret < 0) {
ret = errno;
GOTO(err_io, ret);
}
close(fd);
return 0;
err_io:
close(fd);
err_fd:
return ret;
}
int _get_file(const char* path, const char *key, char* value)
{
int ret, done;
FILE *fp = NULL;
char buf[2048];
char tvalue[1024];
char tkey[1024];
char *str __attribute__((unused));
fp = fopen(path,"r+");
if (fp == 0){
ret = errno;
GOTO(err_ret, ret);
}
rewind(fp);
done = 0;
while(feof(fp)==0){
memset(buf,0x00,2048);
memset(tkey,0x00,1024);
memset(tvalue,0x00,1024);
str = fgets(buf,2048,fp);
sscanf(buf,"%s %s",tkey, tvalue);
if(strcmp(tkey,key)==0){
done = 1;
memcpy(value,tvalue,strlen(tvalue));
break;
}
//begin = ftell(fp);
}
fclose(fp);
if (!done){
return ENOENT;
}
return 0;
err_ret:
return ret;
}
int _del_file(const char* path, const char *key, const char* value)
{
int fd, ret, done;
FILE *fp = NULL;
char buf[2048], tvalue[1024], tkey[1024];
long begin = 0, left = 0;
char *ptr=NULL, *str __attribute__((unused)) = NULL;
(void)fd;
(void)value;
fp = fopen(path,"r+");
if (fp == 0){
ret = errno;
GOTO(err_fd, ret);
}
fseek(fp,0,SEEK_END);
left = ftell(fp);
rewind(fp);
done = 0;
begin = 0;
while(feof(fp)==0){
begin = ftell(fp);
memset(buf,0x00,2048);
memset(tvalue,0x00,1024);
memset(tkey,0x00,1024);
str = fgets(buf,2048,fp);
sscanf(buf,"%s %s",tkey, tvalue);
if(strcmp(tkey,key)==0){
left = left - ftell(fp);
done = 1;
break;
}
}
if (!done){
/*no entry*/
return 0;
};
if (left){
ret = ymalloc((void**)&ptr,left);
if(ret )
GOTO(err_malloc,ret);
ret = fread(ptr, 1, left,fp);
if (ret != left){
ret = errno;
GOTO(err_fread,ret);
}
}else{
ret = ftruncate(fileno(fp),begin);
}
ret = fseek(fp,begin,SEEK_SET);
if (ret)
GOTO(err_fread,ret);
ret = fwrite(ptr,1, left,fp);
if (ret != (int)(left)){
ret = errno;
GOTO(err_fread,ret);
}
ret = ftruncate(fileno(fp),begin + left);
if (ret)
GOTO(err_fread,ret);
fclose(fp);
if (ptr)
yfree((void**)&ptr);
return 0;
err_fread:
fclose(fp);
if (ptr)
yfree((void**)&ptr);
return ret;
err_malloc:
fclose(fp);
return ret;
err_fd:
return ret;
}
int _del_file1(const char *path)
{
return remove(path);
}
#if 1
int _sha1_file(const char *path, uint32_t offset, unsigned char *md)
{
int ret, fd;
SHA_CTX c;
struct stat stbuf;
char buf[MAX_BUF_LEN];
int64_t off, left, cp;
fd = open(path, O_RDONLY);
if (fd < 0) {
ret = errno;
GOTO(err_ret, ret);
}
ret = SHA1_Init(&c);
if (ret == 0) {
ret = EINVAL;
GOTO(err_fd, ret);
}
ret = fstat(fd, &stbuf);
if (ret < 0) {
ret = errno;
GOTO(err_fd, ret);
}
if (offset >= stbuf.st_size)
goto out;
for (off = offset; off < stbuf.st_size; off += MAX_BUF_LEN) {
left = stbuf.st_size - off;
cp = left < MAX_BUF_LEN ? left : MAX_BUF_LEN;
ret = _pread(fd, buf, cp, off);
if (ret < 0) {
ret = -ret;
GOTO(err_fd, ret);
}
ret = SHA1_Update(&c, buf, cp);
if (ret == 0) {
ret = EINVAL;
GOTO(err_fd, ret);
}
}
out:
close(fd);
ret = SHA1_Final(md, &c);
if (ret == 0) {
ret = EINVAL;
GOTO(err_ret, ret);
}
return 0;
err_fd:
close(fd);
err_ret:
return ret;
}
#else
int _sha1_file(const char *path, uint32_t offset, unsigned char *md)
{
(void) path;
(void) offset;
(void) md;
UNIMPLEMENTED(__NULL__);
return EINVAL;
}
#endif
void _sha1_print(char *str, const unsigned char *md)
{
const uint32_t *x = (void *)md;
sprintf(str, "%x%x%x%x%x", x[0], x[1], x[2], x[3], x[4]);
}
int _set_degree(int m, int n, degree_type_t type, ...)
{
int ret, x, fd;
va_list ap;
char *name, buf[MAX_NAME_LEN];
x = (LLU)m * 100 / n;
if (type == DEGREE_BG) {
va_start(ap, type);
name = va_arg(ap, char *);
fd = open(name, O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
ret = errno;
va_end(ap);
GOTO(err_ret, ret);
}
va_end(ap);
sprintf(buf, "%u", x);
ret = _pwrite(fd, buf, strlen(buf), 0);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
} else if (type == DEGREE_FG) {
printf("%u\n", x);
} else
YASSERT(0);
return 0;
err_ret:
return ret;
}
void _stolower(char *s)
{
unsigned int i;
for(i = 0; i < strlen(s); i++) {
s[i]= (char)(tolower(s[i]));
}
}
void _stoupper(char *s)
{
unsigned int i;
for(i = 0; i < strlen(s); i++) {
s[i]= (char)(toupper(s[i]));
}
}
int _tail(int size)
{
int tail;
tail = size % 4;
tail = tail ? (4 - tail) : 0;
DINFO("tail %u\n", tail);
return tail;
}
int64_t _time_used(const struct timeval *prev, const struct timeval *now)
{
return ((LLU)now->tv_sec - (LLU)prev->tv_sec) * 1000 * 1000
+ (now->tv_usec - prev->tv_usec);
}
int64_t _time_used1(const struct timespec *prev, const struct timespec *now)
{
return ((LLU)now->tv_sec - (LLU)prev->tv_sec) * 1000 * 1000 * 1000
+ (now->tv_nsec - prev->tv_nsec);
}
int _run(int nonblock, const char *path, ...)
{
int ret, i, son, status;
va_list ap;
char *argv[128];
struct stat stbuf;
ret = stat(path, &stbuf);
if (ret < 0) {
ret = errno;
DWARN("stat %s not ret %d\n", path, ret);
GOTO(err_ret, ret);
}
son = fork();
switch (son) {
case -1:
ret = errno;
GOTO(err_ret, ret);
case 0:
va_start(ap, path);
for (i = 0; i < 128; i++) {
argv[i] = va_arg(ap, char *);
if (argv[i] == NULL)
break;
}
va_end(ap);
for (i = getdtablesize() - 1; i >= 0; --i) {
sy_close_failok(i);
}
ret = execv(path, argv);
if (ret) {
ret = errno;
GOTO(err_ret, ret);
}
break;
default:
if (nonblock)
return 0;
ret = wait(&status);
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
if (ret) {
if (ret == ENOENT || ret == ENONET)
goto err_ret;
else
GOTO(err_ret, ret);
}
} else {
}
}
return 0;
err_ret:
return ret;
}
int _set_value(const char *path, const char *value, int size, int flag)
{
int ret, fd;
YASSERT(memcmp("/tmp/lich/netinfo", path, strlen("/tmp/lich/netinfo")));
retry:
fd = open(path, O_WRONLY | flag, 0644);
if (fd < 0) {
ret = errno;
if ((flag & O_CREAT) && ret == ENOENT) {
ret = path_validate(path, 0, YLIB_DIRCREATE);
if (ret)
GOTO(err_ret, ret);
goto retry;
}
if (ret == EEXIST)
goto err_ret;
else
GOTO(err_ret, ret);
}
if (value) {
ret = _write(fd, value, size);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
if (value[size - 1] != ‘\n‘) {
ret = _write(fd, "\n", 1);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
}
if (flag & O_SYNC)
fsync(fd);
}
close(fd);
return 0;
err_ret:
return ret;
}
int _get_value(const char *path, char *value, int buflen)
{
int ret, fd;
struct stat stbuf;
ret = stat(path, &stbuf);
if (ret) {
ret = errno;
if (ret == ENOENT)
goto err_ret;
else
GOTO(err_ret, ret);
}
if (buflen < stbuf.st_size) {
ret = EINVAL;
GOTO(err_ret, ret);
}
fd = open(path, O_RDONLY);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
ret = _read(fd, value, buflen);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
if (value[ret - 1] == ‘\n‘) {
value[ret - 1] = ‘\0‘;
ret--;
} else {
value[ret] = ‘\0‘;
}
close(fd);
return ret;
err_ret:
return -ret;
}
int __disk_alloc1(int fd, int size)
{
int ret;
char *buf;
int left, offset, count;
ret = posix_memalign((void **)&buf, BLOCK_SIZE, BIG_BUF_LEN);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
memset(buf, 0x0, BIG_BUF_LEN);
left = size;
offset = 0;
while(left) {
count = left < BIG_BUF_LEN ? left : BIG_BUF_LEN;
ret = _pwrite(fd, buf, count, offset);
if (ret < 0) {
ret = errno;
YASSERT(0);
GOTO(err_free, ret);
}
left -= ret;
offset += ret;
}
yfree((void**)&buf);
return 0;
err_free:
yfree((void**)&buf);
err_ret:
return ret;
}
int _disk_alloc(int fd, int size)
{
int ret, msize;
void *addr;
msize = (size / SYS_PAGE_SIZE) * SYS_PAGE_SIZE;
if (size % SYS_PAGE_SIZE) {
msize += SYS_PAGE_SIZE;
}
ret = ftruncate(fd, msize);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
addr = mmap(0, msize, PROT_WRITE, MAP_SHARED | MAP_NONBLOCK,
fd, 0);
if (addr == MAP_FAILED) {
ret = errno;
if (ret == EACCES) {
DWARN("access fail\n");
return __disk_alloc1(fd, msize);
} else
GOTO(err_ret, ret);
}
memset(addr, 0x0, msize);
ret = msync(addr, msize, MS_ASYNC);
if (ret < 0) {
ret = errno;
GOTO(err_map, ret);
}
munmap(addr, msize);
ret = fsync(fd);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
return 0;
err_map:
munmap(addr, msize);
err_ret:
return ret;
}
int _fallocate(int fd, size_t size)
{
int ret;
ret = fallocate(fd, 0, 0, size);
if (ret < 0) {
ret = errno;
if (ret == ENOSYS || ret == EOPNOTSUPP) {
ret = _disk_alloc(fd, size);
if (ret) {
ret = errno;
GOTO(err_ret, ret);
}
} else
GOTO(err_ret, ret);
}
return 0;
err_ret:
return ret;
}
#if 0
int _disk_mset(int fd, int c, size_t count, off_t offset)
{
int ret, msize;
void *addr;
msize = (size / SYS_PAGE_SIZE) * SYS_PAGE_SIZE;
if (size % SYS_PAGE_SIZE) {
msize += SYS_PAGE_SIZE;
}
ret = ftruncate(fd, msize);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
addr = mmap(0, msize, PROT_WRITE, MAP_SHARED | MAP_NONBLOCK,
fd, 0);
if (addr == MAP_FAILED) {
ret = errno;
GOTO(err_ret, ret);
}
memset(addr, 0x0, msize);
munmap(addr, msize);
return 0;
err_ret:
return ret;
}
#endif
void __ppath(char *ppath, const char *path)
{
char *c;
strcpy(ppath, path);
c = strrchr(ppath, ‘/‘);
if (c)
*c = ‘\0‘;
else
strcpy(ppath, "/");
}
int _mkdir(const char *path, mode_t mode)
{
int ret;
char ppath[MAX_NAME_LEN];
retry:
ret = mkdir(path, mode);
if (ret < 0) {
ret = errno;
if (ret == ENOENT) {
__ppath(ppath, path);
ret = _mkdir(ppath, mode);
if (ret) {
if (ret == EEXIST)
goto retry;
else
GOTO(err_ret, ret);
}
goto retry;
} else if (ret == EEXIST)
goto err_ret;
else
GOTO(err_ret, ret);
}
DBUG("mkdir %s\n", path);
return 0;
err_ret:
return ret;
}
int _open(const char *path, int flag, mode_t mode)
{
int ret, fd;
char ppath[MAX_NAME_LEN];
YASSERT(strlen(path));
retry:
fd = open(path, flag, mode);
if (fd < 0) {
ret = errno;
if (ret == ENOENT) {
__ppath(ppath, path);
ret = _mkdir(ppath, 0755);
if (ret) {
if (ret == EEXIST)
goto retry;
else
GOTO(err_ret, ret);
}
goto retry;
} else if (ret == EEXIST || ret == EINVAL)
goto err_ret;
else
GOTO(err_ret, ret);
}
return fd;
err_ret:
return -ret;
}
int _dir_iterator(const char *path, int (*callback)(const char *parent,
const char *name, void *opaque), void *opaque)
{
int ret;
DIR *dir;
struct dirent debuf, *de;
dir = opendir(path);
if (dir == NULL) {
ret = errno;
if (ret == ENOENT)
goto err_ret;
else {
DWARN("path %s\n", path);
GOTO(err_ret, ret);
}
}
while (1) {
ret = readdir_r(dir, &debuf, &de);
if (ret < 0) {
ret = errno;
GOTO(err_dir, ret);
}
if (de == NULL)
break;
if (strcmp(de->d_name, ".") == 0
|| strcmp(de->d_name, "..") == 0)
continue;
ret = callback(path, de->d_name, opaque);
if (ret)
GOTO(err_dir, ret);
}
closedir(dir);
return 0;
err_dir:
closedir(dir);
err_ret:
return ret;
}
const char *_inet_ntoa(uint32_t addr)
{
struct in_addr sin;
sin.s_addr = addr;
return inet_ntoa(sin);
}
int _inet_addr(struct sockaddr_in *sin, const char *host)
{
int ret, retry = 0, herrno = 0;
struct hostent hostbuf, *result;
char buf[MAX_BUF_LEN];
retry:
ret = gethostbyname_r(host, &hostbuf, buf, sizeof(buf), &result, &herrno);
if (ret) {
ret = errno;
if (ret == EALREADY || ret == EAGAIN) {
DWARN("connect addr %s\n", host);
ret = EAGAIN;
SLEEP_RETRY(err_ret, ret, retry, retry);
} else
GOTO(err_ret, ret);
}
if (result)
_memcpy(&sin->sin_addr, result->h_addr, result->h_length);
else {
ret = ENONET;
DWARN("connect addr %s ret (%u) %s\n", host, ret, strerror(ret));
goto err_ret;
}
return 0;
err_ret:
return ret;
}
int _disk_dfree(const char *path, uint64_t *size, uint64_t *free)
{
int ret;
struct statvfs fsbuf;
ret = statvfs(path, &fsbuf);
if (ret == -1) {
ret = errno;
DERROR("statvfs(%s, ...) ret (%d) %s\n", path, ret,
strerror(ret));
GOTO(err_ret, ret);
}
*free = (LLU)fsbuf.f_bsize * fsbuf.f_bavail;
*size = (LLU)fsbuf.f_bsize * fsbuf.f_blocks;
return 0;
err_ret:
return ret;
}
void _splithost(char *_addr, char *_port, const char *buf, int default_port)
{
int len, i;
char *port;
port = strchr(buf, ‘/‘);
YASSERT(port);
memcpy(_addr, buf, port - buf);
_addr[port - buf] = ‘\0‘;
snprintf(_port, MAX_LINE_LEN, "%d", default_port + atoi(port + 1));
len = strlen(port) - 1;
for (i = 0; i < len; i++) {
YASSERT(isdigit(port[i + 1]));
}
DBUG("addr %s port %s\n", _addr, _port);
}
long int _random()
{
struct timeval tv;
gettimeofday(&tv, NULL);
srandom(tv.tv_usec + getpid());
return random();
}
long int _random_range(int from, int to)
{
int s;
YASSERT(to > from);
s = _random();
return s % (to - from) + from;
}
void *_opaque_encode(void *buf, uint32_t *len, ...)
{
void *pos, *value;
va_list ap;
uint32_t valuelen, total;
va_start(ap, len);
pos = buf;
total = 0;
while (1) {
value = va_arg(ap, char *);
valuelen = va_arg(ap, uint32_t);
DBUG("encode %s len %u\n", (char *)value, valuelen);
if (value == NULL)
break;
memcpy(pos, &valuelen, sizeof(valuelen));
pos += sizeof(valuelen);
memcpy(pos, value, valuelen);
pos += valuelen;
total += (sizeof(valuelen) + valuelen);
}
va_end(ap);
*len = total;
return buf + total;
}
const void *_opaque_decode(const void *buf, uint32_t len, ...)
{
const void *pos;
const void **value;
va_list ap;
uint32_t *_valuelen, valuelen;
va_start(ap, len);
pos = buf;
while (pos < buf + len) {
value = va_arg(ap, const void **);
_valuelen = va_arg(ap, uint32_t *);
if (value == NULL)
break;
memcpy(&valuelen, pos, sizeof(valuelen));
pos += sizeof(valuelen);
//memcpy(value, pos, valuelen);
*value = pos;
pos += valuelen;
if (_valuelen)
*_valuelen = valuelen;
DBUG("decode %s len %u\n", (char *)value, valuelen);
}
va_end(ap);
return pos;
}
int _getkey(const char *key, char *value, const char *buf)
{
int ret;
char *ptr;
ptr = (void *)buf;
while (1) {
ret = sscanf(ptr, key, value);
if (ret == 1)
break;
ptr = strchr(ptr, ‘\n‘);
if (ptr && *(ptr + 1) != ‘\0‘)
ptr++;
else {
ret = ENOENT;
goto err_ret;
}
}
return 0;
err_ret:
return ret;
}
pid_t _gettid()
{
return syscall(SYS_gettid);
}
int _path_split(const char *path, char *_namespace, char *_bucket, char *_object, char *_chunk)
{
int ret;
char tmp[MAX_BUF_LEN];
char namespace[MAX_NAME_LEN], bucket[MAX_NAME_LEN],
object[MAX_NAME_LEN], chunk[MAX_NAME_LEN];
namespace[0] = ‘\0‘;
bucket[0] = ‘\0‘;
object[0] = ‘\0‘;
chunk[0] = ‘\0‘;
tmp[0] = ‘\0‘;
if (strcmp(path, "/") == 0) {
return 0;
}
ret = sscanf(path, "/%[^/]/%[^/]/%[^/]/%[^/]/%[^/]", namespace, bucket, object, chunk, tmp);
DBUG("path ‘%s‘ ret %d namespace ‘%s‘ bucket ‘%s‘ object ‘%s‘ tmp ‘%s‘\n",
path, ret, namespace, bucket, object, tmp);
if (ret > 4) {
ret = EINVAL;
goto err_ret;
}
if (_namespace)
strcpy(_namespace, namespace);
if (_bucket)
strcpy(_bucket, bucket);
if (_object)
strcpy(_object, object);
if (_chunk)
strcpy(_chunk, chunk);
return ret;
err_ret:
return -ret;
}
int _path_split1(const char *path, char *parent, char *name)
{
int ret;
char namespace[MAX_NAME_LEN], bucket[MAX_NAME_LEN],
object[MAX_NAME_LEN], chunk[MAX_NAME_LEN];
ret = _path_split(path, namespace, bucket, object, chunk);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
if (ret == 0) {
strcpy(parent, "/");
name[0] = ‘\0‘;
} else if (ret == 1) {
strcpy(parent, "/");
strcpy(name, namespace);
} else if (ret == 2) {
snprintf(parent, MAX_PATH_LEN, "/%s", namespace);
strcpy(name, bucket);
} else if (ret == 3) {
snprintf(parent, MAX_PATH_LEN, "/%s/%s", namespace, bucket);
strcpy(name, object);
} else if (ret == 4) {
snprintf(parent, MAX_PATH_LEN, "/%s/%s/%s", namespace, bucket, object);
strcpy(name, chunk);
}
return 0;
err_ret:
return ret;
}
int _test_aio()
{
int ret, fd;
io_context_t ctx;
struct iocb *ioarray[1], iocb;
struct io_event events;
char path[512], *buf;
ctx = 0;
ret = io_setup(2, &ctx);
if (ret < 0) {
ret = -ret;
printf("setup error %u\n", ret);
goto err_ret;
}
sprintf(path, "/tmp/l-XXXXXX");
fd = mkstemp(path);
if (fd < 0) {
ret = errno;
printf("setup error %u\n", ret);
goto err_ret;
}
unlink(path);
ret = posix_memalign((void *)&buf, PAGE_SIZE, PAGE_SIZE);
if (ret) {
printf("setup error %u\n", ret);
goto err_ret;
}
io_prep_pwrite(&iocb, fd, buf, PAGE_SIZE, 0);
ioarray[0] = &iocb;
ret = io_submit(ctx, 1, ioarray);
if (ret != 1) {
ret = -ret;
printf("setup error %u\n", ret);
goto err_ret;
}
ret = io_getevents(ctx, 1, 1, &events, NULL);
if (ret < 0) {
ret = -ret;
printf("io_getevents error %u\n", ret);
goto err_ret;
}
printf("test ok\n");
io_destroy(ctx);
return 0;
err_ret:
return ret;
}
void _align(int *_size, int *_offset, int align)
{
int offset, size, newoffset, newsize, end;
offset = *_offset;
size = *_size;
end = offset + size;
newoffset = (offset / align) * align;
if (end % align)
end = (end / align + 1) * align;
else
end = (end / align) * align;
newsize = end - newoffset;
*_offset = newoffset;
*_size = newsize;
}
void _str_split(char *from, char split, char *to[], int *_count)
{
int max, i;
char *pos;
if (from[strlen(from) - 1] == ‘,‘)
from[strlen(from) - 1] = ‘\0‘;
pos = from;
max = *_count;
to[0] = pos;
for (i = 1; i < max; i++) {
pos = strchr(pos, split);
if (pos) {
pos[0] = ‘\0‘;
pos++;
} else {
break;
}
to[i] = pos;
}
*_count = i;
}
int _set_text(const char *path, const char *value, int size, int flag)
{
int ret, fd;
char buf[MAX_BUF_LEN];
retry:
fd = open(path, O_WRONLY | flag, 0644);
if (fd < 0) {
ret = errno;
if ((flag & O_CREAT) && ret == ENOENT) {
ret = path_validate(path, 0, YLIB_DIRCREATE);
if (ret)
GOTO(err_ret, ret);
goto retry;
}
if (ret == EEXIST)
goto err_ret;
else
GOTO(err_ret, ret);
}
if (value) {
if (value[size - 1] != ‘\n‘) {
strcpy(buf, value);
buf[size] = ‘\n‘;
ret = _write(fd, buf, size + 1);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
} else {
ret = _write(fd, value, size);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
}
}
close(fd);
return 0;
err_ret:
return ret;
}
int _get_text(const char *path, char *value, int buflen)
{
int ret, fd;
struct stat stbuf;
ret = stat(path, &stbuf);
if (ret) {
ret = errno;
if (ret == ENOENT)
goto err_ret;
else
GOTO(err_ret, ret);
}
if (buflen < stbuf.st_size) {
ret = EINVAL;
GOTO(err_ret, ret);
}
fd = open(path, O_RDONLY);
if (ret < 0) {
ret = errno;
GOTO(err_ret, ret);
}
ret = _read(fd, value, buflen);
if (ret < 0) {
ret = -ret;
GOTO(err_ret, ret);
}
close(fd);
if (ret > 0) {
YASSERT(value[ret - 1] == ‘\n‘);
value[ret - 1] = ‘\0‘;
return ret - 1;
} else
return 0;
err_ret:
return -ret;
}
int _lock_file(const char *key, int flag)
{
int ret, fd, flags;
ret = path_validate(key, YLIB_NOTDIR, YLIB_DIRCREATE);
if (ret)
GOTO(err_ret, ret);
/* avoid multiple copies */
fd = open(key, O_CREAT | O_RDONLY, 0640);
if (fd == -1) {
ret = errno;
GOTO(err_ret, ret);
}
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0 ) {
ret = errno;
GOTO(err_fd, ret);
}
ret = fcntl(fd, F_SETFL, flags | FD_CLOEXEC);
if (ret < 0) {
ret = errno;
GOTO(err_fd, ret);
}
ret = flock(fd, flag);
if (ret == -1) {
ret = errno;
if (ret == EWOULDBLOCK) {
goto err_fd;
} else
GOTO(err_fd, ret);
}
return fd;
err_fd:
close(fd);
err_ret:
return -ret;
}
void _replace(char *new, const char *old, char from, char to)
{
char *tmp;
strcpy(new, old);
tmp = strchr(new, from);
if (tmp)
*tmp = to;
}
int _get_timeout()
{
int ret, timeout;
double load[3];
return gloconf.rpc_timeout;
ret = getloadavg(load, 3);
if (ret < 0) {
ret = errno;
DWARN("(%u) %s\n", ret, strerror(ret));
return gloconf.rpc_timeout;
} else {
timeout = gloconf.rpc_timeout * (load[0] + 1);
DBUG("load %f %f %f timeout %u\n", load[0], load[1], load[2], timeout);
return timeout;
}
}
void __diskmap_split_disk(const char *name, char *node, char *disk)
{
char *c;
strcpy(node, name);
c = strchr(node, ‘/‘);
if (c) {
*c = ‘\0‘;
strcpy(disk, c + 1);
} else {
strcpy(disk, "0");
}
}
void __diskmap_split_dot(const char *name, char *rack, char *node)
{
char *c;
int len;
strcpy(rack, name);
c = strrchr(name, ‘.‘);
if (c) {
len = c - name;
strcpy(node, c + 1);
memcpy(rack, name, len);
rack[len] = ‘\0‘;
} else {
strcpy(node, name);
strcpy(rack, "default");
}
}
void _diskmap_split(const char *name, char *site, char *rack, char *node, char *disk)
{
char tmp[MAX_NAME_LEN], tmp1[MAX_NAME_LEN];
__diskmap_split_disk(name, tmp, disk);
__diskmap_split_dot(tmp, tmp1, node);
__diskmap_split_dot(tmp1, site, rack);
}
int _startswith(char *str1, char *starts) {
size_t i;
for (i = 0; i < strlen(starts); i++) {
if (str1[i] != starts[i]) {
return 0;
}
}
return 1;
}
#if 0
long long int get_now()
{
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return ((LLU) time.tv_sec) * 1000000000 + time.tv_nsec;
}
long start_time = get_now();
n = sendto(sock, &send_msg, sizeof(send_msg), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
long end_time = get_now();printf("It take time %ld nona.\n",end_time - start_time);
printf("It take time %ld nona.\n",end_time - start_time);
#endif
----------
#ifndef __SYSUTILS_H__
#define __SYSUTILS_H__
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <semaphore.h>
#define SHA1_HASH_SIZE 40
typedef enum {
DEGREE_NULL,
DEGREE_BG,
DEGREE_FG,
} degree_type_t;
typedef void (*fn_t)(char *p ,void *d);
extern int _delete_path(const char *path);
extern int sy_copy(char *src, char *dst);
extern inline int sy_vec_update(uint32_t newoff, uint32_t newlen, uint32_t *oldoff, uint32_t *oldlen);
inline void *_memset(void *s, int c, size_t n);
inline void *_memmove(void *dest, const void *src, size_t n);
inline void *_memcpy(void *dest, const void *src, size_t n);
inline ssize_t _splice(int fd_in, loff_t *off_in, int fd_out,
loff_t *off_out, size_t len, unsigned int flags);
inline int _epoll_wait(int epfd, struct epoll_event *events,
int maxevents, int timeout);
inline ssize_t _read(int fd, void *buf, size_t count);
inline ssize_t _write(int fd, const void *buf, size_t count);
inline ssize_t _pread(int fd, void *buf, size_t count, off_t offset);
inline ssize_t _pwrite(int fd, const void *buf, size_t count, off_t offset);
inline ssize_t _send(int sockfd, const void *buf, size_t len, int flags);
inline ssize_t _recv(int sockfd, void *buf, size_t len, int flags);
inline ssize_t _recvmsg(int sockfd, struct msghdr *msg, int flags);
inline size_t _strlen(const char *s);
inline char *_strcpy(char *dest, const char *src);
inline char *_strncpy(char *dest, const char *src, size_t n);
inline int _gettimeofday(struct timeval *tv, struct timezone *tz);
inline int _strcmp(const char *s1, const char *s2);
inline int _epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
inline int _posix_memalign(void **memptr, size_t alignment, size_t size);
inline ssize_t _readv(int fd, const struct iovec *iov, int iovcnt);
inline ssize_t _writev(int fd, const struct iovec *iov, int iovcnt);
inline ssize_t _sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
inline int _sem_wait(sem_t *sem);
inline int _sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
inline int _sem_timedwait1(sem_t *sem, int tmo);
int path_action(char *path, fn_t fn, void *arg);
int _set_file(const char* path,const char *key,const char* value);
int _get_file(const char* path,const char *key,char* value);
int _del_file(const char* path,const char *key,const char* value);
int _del_file1(const char* path);
int _sha1_file(const char *path, uint32_t offset, unsigned char *md);
void _sha1_print(char *str, const unsigned char *md);
inline void exit_handler(int sig);
int _set_degree(int m, int n, degree_type_t type, ...);
void _stolower(char *s);
void _stoupper(char *s);
int _tail(int s);
int64_t _time_used1(const struct timespec *prev, const struct timespec *now);
int64_t _time_used(const struct timeval *prev, const struct timeval *now);
int _run(int nonblock, const char *path, ...);
int _set_value(const char *path, const char *value, int size, int flag);
int _get_value(const char *path, char *value, int buflen);
int _set_text(const char *path, const char *value, int size, int flag);
int _get_text(const char *path, char *value, int buflen);
int _get_file2(const char *path, const char *key, char *value1, char *value2);
int _get_file3(const char *path, const char *key, char *value1, char *value2, char *value3);
int _get_file0(const char *path, char *value);
int _disk_alloc(int fd, int size);
int _mkdir(const char *path, mode_t mode);
int _open(const char *path, int flag, mode_t mode);
int _dir_iterator(const char *path, int (*callback)(const char *parent,
const char *name, void *opaque), void *opaque);
const char *_inet_ntoa(uint32_t addr);
int _inet_addr(struct sockaddr_in *sin, const char *host);
int _disk_dfree(const char *path, uint64_t *size, uint64_t *free);
int _fallocate(int fd, size_t size);
void _splithost(char *_addr, char *_port, const char *buf, int default_port);
long int _random(void);
long int _random_range(int from, int to);
void *_opaque_encode(void *buf, uint32_t *len, ...);
const void *_opaque_decode(const void *buf, uint32_t len, ...);
int _getkey(const char *key, char *value, const char *buf);
pid_t _gettid(void);
int _path_split(const char *path, char *namespace, char *bucket, char *object, char *_chunk);
int _path_split1(const char *path, char *parent, char *name);
int _test_aio(void);
void _align(int *_size, int *_offset, int align);
void _str_split(char *from, char split, char *to[], int *_count);
int _lock_file(const char *key, int flag);
void _replace(char *new, const char *old, char from, char to);
int _get_timeout(void);
int _startswith(char *str1, char *starts);
#endif
标签:
原文地址:http://www.cnblogs.com/banwhui/p/4642947.html