1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/****************************************************************
*****************************************************************
*****************************************************************
KillJobs.c : Programme permettant de tuer une liste de processus
en fournissant des elements du nom ou de l'utilisateur
permettant d'identifier les jobs par un grep...
Copyright : Environnement Canada & COGITECH Jean Souviron, 2001
Auteur : Jean Souviron (COGITECH Jean Souviron)
Janvier 2001
*****************************************************************
*****************************************************************
*****************************************************************
*/
/*
--- DEFINITIONS des INCLUDE
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/*
========================================================================================
========================================================================================
Routines necessaires
========================================================================================
========================================================================================
*/
/*
* U s a g e
*
*/
static void Usage ( char *filename )
{
fprintf ( stderr, "\n%c You need to call this program as :", toascii(7));
fprintf ( stderr, "\n\n %s part1 part2 ....", filename);
fprintf ( stderr, "\n\n where part1, part2, .. are parts of the line you wish");
fprintf ( stderr, "\n to select in the 'ps -ef' command.");
fprintf ( stderr, "\n (for instance a username, or part of the name of a program)");
fprintf ( stderr, "\n As you can see it can handle multiple searches at a time.");
fprintf ( stderr, "\n Moerover in the case of a parent-child hierarchy it begins");
fprintf ( stderr, "\n by killing the parent and then children.\n\n");
}
/*
========================================================================================
========================================================================================
========================================================================================
========================================================================================
Programme principal
========================================================================================
========================================================================================
========================================================================================
========================================================================================
*/
int main(int argc, char *argv[])
{
int i, l, k, last, *Num1=(int *)NULL, *Num2=(int *)NULL, nli=0, s=0, End=0 ;
char *name=(char *)NULL, command[200], *p=(char *)NULL, bidon[80] ;
FILE *fich = (FILE *)NULL ;
if ( argc <= 1 )
{
Usage(argv[0]);
exit(1);
}
/*
--- Initializes
*/
strcpy ( command, "ps -e | grep ");
l = strlen(command);
name = tmpnam((char *)NULL);
/*
--- Loops in order to find what is required
*/
for ( i = 1 ; i < argc ; i++ )
{
sprintf ( &(command[l]), "%s", argv[i] );
k = strlen(command);
if ( i == 1 )
sprintf ( &(command[k]), " > %s", name);
else
sprintf ( &(command[k]), " >> %s", name);
k = system(command);
}
/*
--- Opens the file and kills what needs to be killed
*/
fich = fopen(name, "r");
s = 0 ;
if ( fich != (FILE *)NULL )
{
while ( (p = fgets(command, 200, fich)) != (char *)NULL )
{
if ( ((p = strstr (command, "sh")) != (char *)NULL) ||
((p = strstr (command, "grep")) != (char *)NULL) ||
((p = strstr (command, argv[0])) != (char *)NULL) )
continue ;
Num1 = (int *) realloc ( Num1, ((nli+1)*sizeof(int)) );
Num2 = (int *) realloc ( Num2, ((nli+1)*sizeof(int)) );
if ( (Num1 == (int *)NULL) || (Num2 == (int *)NULL) )
{
s = 1 ;
break ;
}
#ifndef LINUX
sscanf ( command, "%s %d %d", bidon, &Num1[nli], &Num2[nli] );
#else
sscanf ( command, "%d", &Num1[nli] );
Num2[nli] = 1 ;
#endif
nli = nli + 1 ;
}
if ( (nli > 0) && (s == 0) )
{
/*
--- Sorts ids in order to kill parents before childs
*/
End = 1 ;
last = 0 ;
while ( End == 1 )
{
End = 0 ;
for ( i = last ; i < nli ; i++ )
{
if ( (Num2[i] != 1) && (i < (nli-1)) )
{
for ( k = (i+1) ; k < nli ; k++ )
if ( Num1[k] == Num2[i] )
{
l = Num1[i] ;
Num1[i] = Num1[k] ;
Num1[k] = l ;
l = Num2[i] ;
Num2[i] = Num2[k] ;
Num2[k] = l ;
End = 1 ;
last = i ;
break ;
}
}
if ( End == 1 )
break ;
}
}
/*
--- Now kills everybody in order
*/
for ( i = 0 ; i < nli ; i ++ )
{
sprintf ( command, "kill -9 %d", Num1[i]);
k = system (command);
}
}
if ( Num1 != (int *)NULL )
free ( Num1);
if ( Num2 != (int *)NULL )
free(Num2);
fclose ( fich );
sprintf ( command, "rm -f %s", name);
i = system(command);
}
return (s);
} |
Partager