MCPcopy Create free account
hub / github.com/VirtualGL/virtualgl / killproc

Function killproc

client/vglclient.cpp:88–202  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86
87
88void killproc(bool userOnly)
89{
90 #ifdef __APPLE__
91
92 unsigned char *buf = NULL;
93
94 if(!userOnly && getuid() != 0) THROW("Only root can do that");
95
96 try
97 {
98 int mib_all[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
99 int mib_user[4] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid() };
100 size_t len = 0;
101
102 TRY_UNIX(sysctl(userOnly ? mib_user : mib_all, 4, NULL, &len, NULL, 0));
103 if(len < sizeof(kinfo_proc)) THROW("Process table is empty");
104 buf = new unsigned char[len];
105 TRY_UNIX(sysctl(userOnly ? mib_user : mib_all, 4, buf, &len, NULL, 0));
106 int nprocs = len / sizeof(kinfo_proc);
107 kinfo_proc *kp = (kinfo_proc *)buf;
108
109 for(int i = 0; i < nprocs; i++)
110 {
111 int pid = kp[i].kp_proc.p_pid;
112 if(!strcmp(kp[i].kp_proc.p_comm, "vglclient") && pid != getpid())
113 {
114 vglout.println("Terminating vglclient process %d", pid);
115 kill(pid, SIGTERM);
116 }
117 }
118 delete [] buf; buf = NULL;
119 }
120 catch(...)
121 {
122 delete [] buf;
123 throw;
124 }
125
126 #else
127
128 DIR *procdir = NULL; struct dirent *dent = NULL; int fd = -1;
129
130 if(!userOnly && getuid() != 0) THROW("Only root can do that");
131
132 try
133 {
134 if(!(procdir = opendir("/proc"))) THROW_UNIX();
135 while((dent = readdir(procdir)) != NULL)
136 {
137 if(dent->d_name[0] >= '1' && dent->d_name[0] <= '9')
138 {
139 char temps[1024]; struct stat fsbuf;
140 int pid = atoi(dent->d_name);
141
142 if(pid == getpid()) continue;
143 #ifdef sun
144 snprintf(temps, 1024, "/proc/%s/psinfo", dent->d_name);
145 #else

Callers 1

mainFunction · 0.85

Calls 1

printlnMethod · 0.80

Tested by

no test coverage detected