#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* for getopt_long */
#endif

#include <unistd.h>
#include <getopt.h>

#include <sys/personality.h>

/* Peronsality bit for huge page backed stack */
#define HUGE_PAGE_STACK 0x0020000

extern int errno;
extern int optind;

void print_usage( )
{
        fprintf( stderr, "hugectl [options] target\n" );
        fprintf( stderr, "options:\n" );
        fprintf( stderr, " --help,  -h   Prints this message\n" );
        fprintf( stderr, " --stack, -s   Attempts to execute target program 
with a huge page backed stack\n" );
}

void set_huge_stack( )
{
        char * err;
        unsigned long curr_per = personality( 0xffffffff );
        if( personality( curr_per | HUGE_PAGE_STACK ) == -1 )
        {
                err = strerror( errno );
                fprintf( stderr, "Error setting HUGE_STACK personality flag: 
'%s'\n", err );
                exit( -1 );
        }
}

int main( int argc, char** argv )
{
        char opts [] = "+hs";
        int ret = 0, index = 0;
        struct option long_opts [] = 
        {
                { "help",  0, 0, 'h' },
                { "stack", 0, 0, 's' },
                { 0,       0, 0, 0   },
        };

        if( argc < 2 )
        {
                print_usage( );
                return 0;
        }

        while( ret != -1 )
        {
                ret = getopt_long( argc, argv, opts, long_opts, &index );
                switch( ret )
                {
                        case 's':
                                set_huge_stack( );
                        case -1:
                                break;

                        default:
                                ret = -1;
                                break;

                        case 'h':
                                print_usage( );
                                return 0;
                }
        }
        index = optind;

        if( execvp( argv[ index ], &argv[ index ] ) == -1 )
        {
                ret = errno;
                fprintf( stderr, "Error calling execvp: '%s'\n", strerror( ret 
) );
                return -1;
        }

        return 0;
}

Attachment: signature.asc
Description: This is a digitally signed message part

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Libhugetlbfs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to