1、写文件
static const char *boilerplate_1 =
"#!/bin/sh\nexport DISPLAY=:1\nexport LD_LIBRARY_PATH=/usr/local/lib\n";
fd = open (filename, O_WRONLY | O_CREAT, 0700);
if (fd < 0)
{
g_free (filename);
g_free (uid_str);
return FALSE;
}
if ((fp = fdopen (fd, "w")) == NULL)
{
#ifdef DEBUG
printf ("can't reopen atjob file\n");
#endif
return FALSE;
}
fwrite (boilerplate_1, sizeof (char), strlen (boilerplate_1), fp);//真正写文件的地方
2、执行shell脚本
sprintf (call_at,
"/usr/bin/at -q g -f /tmp/atjob.txt %02d:%02d %02d.%02d.%02d 2>&1",
tm->tm_hour, tm->tm_min, tm->tm_mday, tm->tm_mon + 1,
tm->tm_year - 100);
g_print ("%s\n", call_at);
if ((at_return = popen (call_at, "r")) != NULL)
范例
#include
main()
{
FILE * fp;
char buffer[80];
fp=popen(“cat /etc/passwd”,”r”);
fgets(buffer,sizeof(buffer),fp);
printf(“%s”,buffer);
pclose(fp);
}
执行
root :x:0 0: root: /root: /bin/bash
3、把文件改成类似管道(先进先出)的操作
int ret;
int fd;
ret = mkfifo(PIPESERVER, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if (ret == -1) {
fprintf(stderr, "make pipe error:%s\n", strerror(errno));
return -1;
}
if ((s_fd = open(PIPESERVER,O_RDWR)) < 0) {
fprintf(stderr, "Open ioctl device file %s error.\n", "pipe");
return -1;
}
while (1) {
memset( &event, 0 ,sizeof(struct ipm_event));
FD_ZERO(&rfds);
if( flag ) {
FD_SET(ipmc_fd, &rfds);
}
FD_SET(s_fd, &rfds);
/* tv.tv_sec = 1; tv.tv_usec = 0; */
tv.tv_sec = 0;
tv.tv_usec = 100000;
ret = select(temp + 1, &rfds, NULL, NULL, &tv);
if( FD_ISSET(s_fd, &rfds) ) {
if ((size = read(s_fd, msg, IPM_MSG_SIZE))!=IPM_MSG_SIZE) {
fprintf(stderr, "read pipe error:%s\n", strerror(errno));
return -1;
}
这样,在一端用select监听,一端写入时就可以监听得到
写入端:
fd = open(PIPESERVER, O_WRONLY|O_NONBLOCK, 0);
if (fd == -1) {
printf("open pipe error:%s\n", strerror(errno));
return -1;
}
}
ret = write(fd, msg, IPM_MSG_SIZE);
if (ret == -1) {
printf("write to pipe error:%s\n", strerror(errno));
return -1;
}
if( ret != IPM_MSG_SIZE ) {
printf("write to pipe failed. return %d.\n",ret);
return -1;
}
注意:写入的时间的字节数以IPM_MSG_SIZE为准,而不是msg的字节大小
阅读(1746) | 评论(0) | 转发(0) |