prog_limit.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 19 Nov 2010 12:05:57 +0100
changeset 157 e8fbd0653fda
parent 113 397270b5d21a
permissions -rw-r--r--
Simplified IPv6 aware ddos demo committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 67
diff changeset
     2
 * prog_limit.c
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 3
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
113
397270b5d21a sys/wait.h is the proper header, not wait.h
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
     8
#include <sys/wait.h>
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <unistd.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
int set_limit (int);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
int main (int argc, char **argv)
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
{
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    int i;
64
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    16
    int pid;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    17
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    char **args;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    if (argc < 3) {
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
        printf ("Usage: %s <mem> <cmd> [args]...\n", argv[0]);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
        printf ("Report bugs to mbroeker@largo.homelinux.org\n");
48
b94d657a9acb Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 27
diff changeset
    23
        return EXIT_FAILURE;
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    26
    if ((args = calloc ((argc - 1), sizeof (char *))) == NULL) {
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
        perror ("calloc");
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        return EXIT_FAILURE;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    for (i = 2; i < argc; i++)
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        args[i - 2] = argv[i];
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    34
    args[i - 2] = NULL;
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
64
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    36
    pid = fork ();
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    37
    switch (pid) {
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    38
    case 0:
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    39
        if (set_limit (atoi (argv[1])) == 0) {
67
abe63a276a36 file traversal demo && minor changes
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 64
diff changeset
    40
            i = execvp (args[0], args);
64
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    41
        } else {
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    42
            perror ("Limit Error");
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    43
            return EXIT_FAILURE;
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    44
        }
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    45
    case -1:
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    46
        perror ("Fork Error");
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    47
        return EXIT_FAILURE;
64
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    48
    default:
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 60
diff changeset
    49
        wait (&i);
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    50
    }
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    if (args != NULL)
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
        free (args);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    55
    return i;
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
}