由于要在STB中实现待机,需要kill掉一些进程 ,这里采用的是先ps查出有哪些进程,并保存到文件,然后把该kill掉的进程kill掉,还没有做好。部分kill的代码:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cutils/log.h>
#define PATH_MAX 128
#define PROCESS_RESERVER_NUM 58
char reserve_process[PROCESS_RESERVER_NUM][64] =
{
"/init",
"kthreadd",
"ksoftirqd/0",
"watchdog/0",
"events/0",
"khelper",
"async/mgr",
"sync_supers",
"bdi-default",
"kblockd/0",
"ata_aux",
"ata_sff/0",
"khubd",
"kseriod",
"kgameportd",
"kmmcd",
"cfg80211",
"rpciod/0",
"khungtaskd",
"kswapd0",
"aio/0",
"nfsiod",
"crypto/0",
"scsi_eh_0",
"mtdblock0",
"mtdblock1",
"mtdblock2",
"mtdblock3",
"mtdblock4",
"mtdblock5",
"mtdblock6",
"mtdblock7",
"mtdblock8",
"mtdblock9",
"mtdblock10",
"mtdblock11",
"mtdblock12",
"mtdblock13",
"mtdblock14",
"wusbd",
"kpsmoused",
"kstriped",
"usbhid_resumer",
"binder",
"l2cap",
"krfcommd",
"yaffs-bg-1",
"yaffs-bg-1",
"yaffs-bg-1",
"/system/bin/sh",
"/system/bin/irkeyservice",
"logcat",
"zygote",
"flush-31:6",
"system_server",
"galcore",
"/system/bin/hdmi"
};
typedef struct {
char user[32];
char pid[10];
char ppid[10];
char vsize[10];
char rss[10];
char wchan[10];
char pc[10];
char s[10];
char process_name[64];
} PS_INFO_T;
pthread_mutex_t mutex[1];
int check_process_reserve(char *process_name)
{
int i = 0;
LOGI("process_name = %s", process_name);
for(i = 0; i < PROCESS_RESERVER_NUM; i++)
if(memcmp(reserve_process[i], process_name, strlen(process_name)) == 0)
return 1;
return -1;
}
/* reads a file, making sure it is terminated with \n \0 */
void *read_file(const char *fn, unsigned *_sz)
{
char *data;
int sz;
int fd;
data = 0;
fd = open(fn, O_RDONLY);
if(fd < 0)
{
printf("open %s fail\n", fn);
return 0;
}
printf("fd = %d\n", fd);
sz = lseek(fd, 0, SEEK_END);
printf("sz = %d\n", sz);
if(sz < 0) goto oops;
if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
data = (char*) malloc(sz + 2);
if(data == 0) goto oops;
if(read(fd, data, sz) != sz) goto oops;
close(fd);
data[sz] = '\n';
data[sz+1] = 0;
if(_sz) *_sz = sz;
printf("read_file success\n");
return data;
oops:
close(fd);
printf("read_file fail\n");
if(data != 0) free(data);
return 0;
}
char *mydata = NULL;
char * parse_next_process(char *data, PS_INFO_T *p_info)
{
char *p = NULL;
pthread_mutex_lock(mutex);
LOGI("Enter parse_next_process--------------------------");
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->user, p, data - p ); //user
p_info->user[data - p ] = '\0';
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->pid, p, data - p);//pid
p_info->pid[data - p] = '\0';
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->ppid, p, data - p);//ppid
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->vsize, p, data - p);//vsize
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->rss, p, data - p);//rss
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->wchan, p, data - p);//wchan
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->pc, p, data - p);//pc
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ')data++;
memcpy(p_info->s, p, data - p);//s
while(*data == ' ' || *data == '\n') data++;
p = data;
while(*data != ' ' && *data != '\n')data++;
memcpy(p_info->process_name, p, data - p);//process_name
p_info->process_name[data - p ] = '\0';
if(*data != '\n')
{
LOGI("process name have space");//进程名有空格
while( *data != '\n')data++;
}
LOGI("Exit parse_next_process------------------------------pid = %d",getpid());
pthread_mutex_unlock(mutex);
return data;
}
int main(int argc, char *argv[])
{
int i, rv, pid_t[128];
int status = 0;
char *p = NULL;
int pid = -1;
PS_INFO_T ps_info;
pthread_mutex_init(mutex, NULL);
pid = fork();
printf("pid = %d\n", pid);
LOGI("rv = %d.\n", rv);
if(pid == 0)
{
rv = execl("/system/bin/sh", "/system/bin/sh", "/system/etc/ps.sh", (char*)0);
LOGI("rv1 = %d \n", rv);
exit(0);
return 0;
}
else
{
sleep(1);
while(waitpid(pid, &status, 0) < 0)
{
printf( "waitpid error pid:%d \n", pid);
break;
}
mydata = read_file("/ps.log", 0);
p = &mydata[61];//去掉第一行
while(p)
{
p = parse_next_process(p, &ps_info);
if(memcmp("ps", ps_info.process_name, strlen(ps_info.process_name)) == 0)
{
LOGI("Exit process");
break;
}
pid = fork();
if(pid == 0)
{
if(check_process_reserve(ps_info.process_name) != 1)
{
LOGI("find process %s, pid = %s not reserve", ps_info.process_name, ps_info.pid);
rv = execl("/system/bin/kill", "/system/bin/kill", ps_info.pid, (char*)0);
LOGI("rv = %d\n", rv);
}
exit(0);
return 0;
}
else
{ while(waitpid(pid, &status, 0) < 0)
{
printf( "waitpid error pid:%d \n", pid);
break;
}
}
}
}
LOGI("Exit Main \n");
return 0;
} /system/etc/ps.sh这个脚本 执行ps命令并把文件保存在根目录下。
335

被折叠的 条评论
为什么被折叠?



