Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128745
  • 博文数量: 44
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 407
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-02 21:38
文章分类
文章存档

2015年(1)

2013年(43)

我的朋友

分类: C/C++

2013-03-16 15:03:17

root权限运行的daemon进程:

#include
#include
#include
#include
#include
#include
#include
#include


key_t key;
pthread_mutex_t mutex;


typedef struct
{
char device[15];
char command[10];
}MY_MEMORY;


int deal(char *dev,char *comd)
{
printf("in deal:\n");
printf("dev is :%s\n",dev);
printf("comd is :%s\n",comd);
if(strcmp(dev,"dcp_im_sys")==0)
{
if(strcmp(comd,"start")==0)
system("service dcp_im_sys start");
if(strcmp(comd,"stop")==0)
system("service dcp_im_sys stop");
if(strcmp(comd,"restart")==0)
system("service dcp_im_sys restart");
}


if(strcmp(dev,"dcp_lm_sys")==0)
        {
                if(strcmp(comd,"start")==0)
                        system("service dcp_lm_sys start");
                if(strcmp(comd,"stop")==0)
                        system("service dcp_lm_sys stop");
                if(strcmp(comd,"restart")==0)
                        system("service dcp_lm_sys restart");
        }


if(strcmp(dev,"dcp_lm_agent")==0)
        {
                if(strcmp(comd,"start")==0)
                        system("service dcp_lm_agent start");
                if(strcmp(comd,"stop")==0)
                        system("service dcp_lm_agent stop");
                if(strcmp(comd,"restart")==0)
                        system("service dcp_lm_agent restart");
        }


return 0;
}
int main(int argc,char** argv)
{
int shmid;
char device[15];
char command[10];
MY_MEMORY* memory;
void* mem;
pthread_mutex_init(&mutex,NULL);
key=ftok("/home/geotmt/a",1);
while(1)
{
shmid=shmget(key,sizeof(MY_MEMORY),IPC_CREAT|0666);
if(shmid<0)
{
printf("Open share memory error!!!\n");
return 0;
}
memory=(MY_MEMORY *)shmat(shmid,(const void *)0,0);
if(memory==(void *)-1)
{
printf("Get share memory error!!!\n");
return 0;
}
pthread_mutex_lock(&mutex);
memset(device,0,sizeof(device));
memset(command,0,sizeof(command));
memcpy(device,memory->device,strlen(memory->device));
memcpy(command,memory->command,strlen(memory->command));
memset(memory,0,sizeof(MY_MEMORY));
pthread_mutex_unlock(&mutex);
shmdt(memory);

if(strlen(device)!=0)
{
printf("There is something in share memory\n");
printf("device is: %s\n",device);
printf("comand is: %s\n",command);

deal(device,command);
}


sleep(1);
printf("after sleep 1s\n");
}
}


普通账户运行的所谓的客户端:

#include
#include
#include
#include
#include
#include
#include
#include


key_t key;
pthread_mutex_t mutex;
typedef struct
{
char device[15];
char command[10];
}MY_MEMORY;


int main(int argc,char** argv)
{
int shmid;
MY_MEMORY* memory;
void* mem;
if(argc<3)
{
printf("******!!!ERROR!!!**********\n");
printf("Please input param.\n");
printf("Like: geo dcp_im_sys stop\n");
printf("***************************\n");
}
/***********company the first argument****************/
if(strcmp(argv[1],"dcp_im_sys")!=0)
{
if(strcmp(argv[1],"dcp_lm_sys")!=0)
{
if(strcmp(argv[1],"dcp_lm_agent")!=0)
{
printf("argv[1] input is wrong\n");
printf("argv[1] is like: dcp_im_sys or dcp_lm_sys or dcp_lm_agent\n");
return 0;
}
}
}
/**********company the second argument******************/
if(strcmp(argv[2],"start")!=0)
{
if(strcmp(argv[2],"stop")!=0)
{
if(strcmp(argv[2],"restart")!=0)
{
printf("argv[2] input is wrong\n");
printf("argv[2] is like: start or stop or restart\n");
return 0;
}
}
}
key=ftok("/home/geotmt/a",1);
if(key<0)
printf("ftok error!!!\n");
shmid=shmget(key,sizeof(MY_MEMORY),IPC_CREAT|0666);
if(shmid<0)
{
printf("Create share memory error!!!\n");
printf("the reasion is:%s\n",strerror(errno));
return 0;
}


memory=(MY_MEMORY *)shmat(shmid,0,0);
if(memory==(void *)-1)
{
printf("Get share memory error!!!\n");
printf("the reason is: %s\n",strerror(errno));
return 0;
}

//memset(memory,0,sizeof(MY_MEMORY));
pthread_mutex_init(&mutex,NULL);
pthread_mutex_lock(&mutex);
memset(memory,0,sizeof(MY_MEMORY));
memcpy(memory->device,argv[1],strlen(argv[1]));
memcpy(memory->command,argv[2],strlen(argv[2]));
pthread_mutex_unlock(&mutex);
printf("memory->device string is :%s\n",memory->device);
printf("memory->command string is :%s\n",memory->command);

return 0;
}









阅读(1324) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~