📄 ipa_ok.c
字号:
sprintf(asctm,"%02d",tm1->tm_hour);
sprintf(asctm+2,"%02d",tm1->tm_min);
sprintf(stime, "%s%s", ascday+4, asctm);
return 0;
}
//getNow--取得当前日期和时间
//Argument:
// ascday--YYYYMMDD格式
// asctm--HHMMSS格式
int getNow(char *ascday, char *asctm)
{
time_t t;
struct tm * tm1;
t = time(NULL);
tm1 = localtime(&t);
sprintf(ascday,"%04d",tm1->tm_year+1900);
sprintf(ascday+4,"%02d",tm1->tm_mon+1);
sprintf(ascday+6,"%02d",tm1->tm_mday);
ascday[8] = 0;
sprintf(asctm,"%02d",tm1->tm_hour);
sprintf(asctm+2,"%02d",tm1->tm_min);
sprintf(asctm+4,"%02d",tm1->tm_sec);
asctm[6] = 0;
return 0;
}
int client_request(const char *host, const char *service, char *hdata, int hsize, char *outbuf, int *outlen)
{
int ret;
int sockfd;
unsigned char *p_zip;
unsigned int zip_len;
int len;
sockfd = connectTCP(host, service);
if (sockfd < 0)
{
printf("connect to %s:%s fail, error[%s]!\n", host, service, strerror(errno));
return -1;
}
ret = writesock(sockfd, hdata, hsize);
if (ret < 0)
{
close(sockfd);
printf("writesock to %s:%s fail, error[%s]!\n", host, service, strerror(errno));
return -2;
}
ret = readStream(sockfd, outbuf, outlen);
if (ret < 0)
{
//exit(9);
close(sockfd);
printf("readStream2 from %s:%s fail, error[%s]!\n", host, service, strerror(errno));
return -3;
}
close(sockfd);
return 0;
}
/*
readStream--读数据流,流的前4字节一定是整个报长
*/
int readStream(int sock, char *buf, int *factlen)
{
int aint, i, rsize, ret;
int n;
int pos, fd;
char str[128];
for (i=0; i<4; i++)
{
n = read(sock, buf+i, 1);
if (n < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in read sock, ret[%d] errno[%d:%s]\n",__FILE__,__LINE__, n, errno, strerror(errno));
return -1;
}
if (n == 0) return -1;
str[i] = buf[i];
}
str[4] = 0;
memcpy(&aint, str, 4);
aint = ntohl(aint);
*factlen = aint;
//printf("readStream *factlen[%d]\n", *factlen);
pos = 4;
rsize = (*factlen) - 4;
if (rsize <= 0) return 0;
for (;;)
{
n = read(sock, buf+pos, rsize);
if (n <= 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in read sock, ret[%d] errno[%d:%s]\n",__FILE__,__LINE__, n, errno, strerror(errno));
return -1;
}
rsize -= n;
pos += n;
//printf("readStream *factlen[%d] pos[%d]\n", *factlen, pos);
if (pos == *factlen) break;
}
return 0;
}
int kill_a_process(char *procname)
{
char cmd[256];
FILE *fp;
int namelen;
char aline[1024];
char col1[128], col2[128], col3[128], col4[128],
col5[128], col6[128], col7[128], col8[128];
int col8len;
sprintf(cmd, "ps -ef|grep %s 1>__a_temp_file.txt 2>&1", procname);
system(cmd);
fp = fopen("__a_temp_file.txt", "r");
if (fp == NULL)
{
printf("error on fopen(__a_temp_file.txt) [%d:%s]\n", errno, strerror(errno));
return -1;
}
namelen = strlen(procname);
for (;;)
{
memset(aline, 0, sizeof(aline));
fgets(aline, sizeof(aline)-1, fp);
if ( feof(fp) ) break;
if (strlen(aline) == 0) continue;
sscanf(aline, "%s%s%s%s%s%s%s%s", col1, col2, col3, col4, col5, col6, col7, col8 );
//printf("col2[%s] col3[%s] col8[%s]\n", col2, col3, col8);
col8len = strlen(col8);
if (col8len < namelen) continue;
if ( memcmp(col8+col8len-namelen, procname, namelen) != 0 ) continue;
if (col8len > namelen)
{
if (col8[col8len-namelen-1] != '/') continue;
}
if (atoi(col3) == 1)
{
kill(atoi(col2), 15);
break;
}
}
fclose(fp);
unlink("__a_temp_file.txt");
comSleep(200);
// sleep(1);
return 0;
}
int comMkShfn(char *filename, int size, int creat)
{
int fd;
int ret;
char *p;
if (creat == 1)
{
fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
//printf("fd[%d] errno[%d]\n", fd, errno);
if (fd < 0)
{
logit(gd_SYS_LOG_FILE, "in comMkShfn(), ret[%d] errno[%d:%s]\n", fd, errno, strerror(errno));
return -1;
}
if (size <= 0)
{
close(fd);
return 0;
}
p = (char *)malloc(size);
memset(p, 0, size);
ret = write(fd, (void *)p, size);
free(p);
close(fd);
}
return 0;
}
int comReadShfn(char *fn, int offset, int count, char *buf)
{
int ret;
int rsize;
int fd;
fd = open(fn, O_RDONLY);
//printf("fd[%d] errno[%d]\n", fd, errno);
if (fd < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d] in comMkShf(), ret[%d] errno[%d:%s]\n", __FILE__,__LINE__, fd, errno, strerror(errno));
return -1;
}
ret = lseek(fd, offset, SEEK_SET);
if (ret < 0)
{
close(fd);
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in comReadShf() lseek[%d] errno[%d:%s]\n", __FILE__,__LINE__, ret, errno, strerror(errno));
return -1;
}
rsize = read(fd, buf, count);
close(fd);
if (rsize < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in comReadShf() read[%d] errno[%d:%s]\n", __FILE__,__LINE__, ret, errno, strerror(errno));
return -1;
}
if (rsize != count)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in rsize[%d] != count[%d] errno[%d:%s]\n", __FILE__,__LINE__, rsize, count, errno, strerror(errno));
return -1;
}
return 0;
}
int comWriteShfn(char *fn, int offset, char *buf, int count)
{
int ret;
int wsize;
int fd;
struct timeval first;
struct timezone tzp;
fd = open(fn, O_WRONLY);
//printf("fd[%d] errno[%d]\n", fd, errno);
if (fd < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d] in comMkShf(), ret[%d] errno[%d:%s]\n", __FILE__,__LINE__, fd, errno, strerror(errno));
return -1;
}
ret = lseek(fd, offset, SEEK_SET);
if (ret < 0)
{
close(fd);
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in comWriteShf() lseek[%d] errno[%d:%s]\n", __FILE__,__LINE__, ret, errno, strerror(errno));
return -1;
}
wsize = write(fd, buf, count);
close(fd);
if (wsize < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in comWriteShf() write[%d] errno[%d:%s]\n", __FILE__,__LINE__, ret, errno, strerror(errno));
return -1;
}
if (wsize != count)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in wsize[%d] != count[%d] errno[%d:%s]\n", __FILE__,__LINE__, wsize, count, errno, strerror(errno));
return -1;
}
return 0;
}
int appendWriteShfn(char *fn, char *buf, int count)
{
int ret;
int fd;
int wsize;
fd = open(fn, O_WRONLY|O_APPEND);
//printf("fd[%d] errno[%d]\n", fd, errno);
if (fd < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d] in appendWriteShfn(), ret[%d] errno[%d:%s]\n", __FILE__,__LINE__, fd, errno, strerror(errno));
return -1;
}
wsize = write(fd, buf, count);
close(fd);
if (wsize < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in comWriteShf() write[%d] errno[%d:%s]\n", __FILE__,__LINE__, ret, errno, strerror(errno));
return -1;
}
if (wsize != count)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in wsize[%d] != count[%d] errno[%d:%s]\n", __FILE__,__LINE__, wsize, count, errno, strerror(errno));
return -1;
}
return 0;
}
int mkfSemSet(char *filename, int nsems, int creat)
{
int fd;
int i;
char *p;
int ret;
if (creat == 0)
{
fd = open(filename, O_WRONLY);
if (fd < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d] in open() errno[%d:%s]\n", __FILE__,__LINE__, errno, strerror(errno));
return -1;
}
}
else
{
//fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d] in open() errno[%d:%s]\n", __FILE__,__LINE__, errno, strerror(errno));
return -2;
}
p = (char *)malloc(nsems);
memset(p, 0, nsems);
i = write(fd, (void *)p, nsems);
free(p);
if (i != nsems)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d] in write() 写[%d] 实际写入[%d]\n", __FILE__,__LINE__, nsems, i);
return -3;
}
ret = fdatasync(fd);
if (ret < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d] in fdatasync() errno[%d:%s]\n", __FILE__,__LINE__, errno, strerror(errno));
return -4;
}
}
return( fd );
}
int fSemSetPlus(int semfd, int semnum)
{
int ret;
ret = writew_lock(semfd, semnum, SEEK_SET, 1);
if (ret < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in fSemSetPlus() writew_lock[%d] errno[%d:%s]\n", __FILE__,__LINE__, ret, errno, strerror(errno));
}
return ret;
}
int fSemSetSub(int semfd, int semnum)
{
int ret;
ret = un_lock(semfd, semnum, SEEK_SET, 1);
if (ret < 0)
{
logit(gd_SYS_LOG_FILE, "file[%s] line[%d]in fSemSetSub() writew_lock[%d] errno[%d:%s]\n", __FILE__,__LINE__, ret, errno, strerror(errno));
}
return ret;
}
int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
struct flock lock;
lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */
lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
lock.l_len = len; /* #bytes (0 means to EOF) */
return( fcntl(fd, cmd, &lock) );
}
int comSleep(long stime)
{
struct timeval tv;
int tmp;
if (stime <= 0)
return 0;
tmp = stime%1000;
tv.tv_sec = stime / 1000;
tv.tv_usec = tmp*1000;
tmp = select(1,NULL,NULL,NULL,&tv);
if (tmp < 0)
{
printf("comSleep ret[%d] errno[%d:%s]\n", tmp, errno, strerror(errno));
return -1;
}
return 0;
}
int ltrim(char *str)
{
int len;
len = strlen(str);
if (len == 0) return 0;
while (str[0] == ' ')
{
if (len == 1)
{
str[0] = 0;
break;
}
memcpy(str, &(str[1]), len-1);
str[len-1] = 0;
len = strlen(str);
if (len == 0) break;
}
return 0;
}
void rtrim(char *str, char c)
{
int i, j;
i = strlen(str);
if (i == 0) return;
j = i-1;
if (str[j] != c) return;
for (;;)
{
if (str[j] == c)
{
str[j] = 0;
j --;
if (j < 0)
return;
else
continue;
}
else
return;
}
}
void m_inet_ntoa(u_int *addr, char *saddr)
{
struct in_addr in;
in.s_addr = *addr;
strcpy(saddr, inet_ntoa(in) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -