aboutsummaryrefslogtreecommitdiff
path: root/nukectl.c
blob: 723d411dbef6247ac49fb9a9780b096f7cfb77e0 (plain) (blame)
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
/**
 *  NetNuke - Erases all storage media detected by the system
 *  Copyright (C) 2009-2010  Joseph Hunkeler <jhunkeler@gmail.com, jhunk@stsci.edu>
 *
 *  This file is part of NetNuke.
 *
 *  NetNuke is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  NetNuke is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NetNuke.  If not, see <http://www.gnu.org/licenses/>.
 **/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include "netnuke.h"

FILE* randfp;
unsigned int randseed;
unsigned long long total_written_bytes;
extern pthread_mutex_t lock_global;
extern pthread_mutex_t lock_write;

void* wipe(void* device)
{
    nndevice_t* d = (nndevice_t*)device;
    int output_progress = 0;
    unsigned int blocks = d->blksz * d->blks;
    unsigned long long blocks_written = 0;
    long double percent = 0.0L;

    FILE* fp = fopen(d->path, "w+t");
    if(fp == NULL)
    {
        COM(self, "Unable to open %s: %s\n", d->path, strerror(errno));
        return (int*)1;
    }
    COM(self, "path: %s, block size %d, blocks %lu, total bytes %lu\n",
           d->path, d->blksz, d->blks, d->sz);

    srand(nngetseed());
    while(blocks_written < blocks)
    {
        pthread_mutex_lock(&lock_global);
        blocks_written += nnwrite(fp, d->blksz);
        pthread_mutex_unlock(&lock_global);

        if(output_progress >= 102400)
        {
            percent = (blocks_written / (long double)d->sz) * 100;
            printf("%s: %llu of %llu (%0.2Lf%%)\n", d->path, blocks_written, d->sz, percent);
            output_progress = 0;
        }

        ++output_progress;
    }
    COM(self, "%s complete\n", d->path);
    pthread_exit(NULL);

    return NULL;
}

pthread_t nnthread(nndevice_t* device)
{
    pthread_t thread;
    if((pthread_create(&thread, NULL, wipe, device)) != 0)
    {
        COM(self, "Failed to create thread %lu\n", thread);
        return 1;
    }

    return thread;
}

int nnwrite(FILE* fp, int bsize)
{
    int bytes_written = 0;
    char* buffer = randstr(bsize);
    pthread_mutex_lock(&lock_write);
    //bytes_written = fwrite(buffer, sizeof(char), bsize, fp);
    bytes_written += bsize; //temporary testing
    total_written_bytes += bytes_written;
    pthread_mutex_unlock(&lock_write);

    free(buffer);
    return bytes_written;
}

void nnrandinit()
{
    COM(self, "Initializing random seed\n");
    randfp = fopen("/dev/urandom", "r");
    if(randfp == NULL)
    {
        COM(self, "urandom: %s\n", strerror(errno));
        return;
    }
    srand(nngetseed());
}

void nnrandfree()
{
    COM(self, "Closing urandom\n");
    fclose(randfp);
}

unsigned int nngetseed()
{
    if((fread(&randseed, 1, sizeof(int), randfp)) > 0)
    {
        COM(self, "(urandom) Seed is %lu\n", randseed);
        return randseed;
    }

    unsigned int t = time(NULL);
    COM(self, "(UNIX Epoch) Seed is %lu\n", t);
    return t;
}

unsigned int nnrand(int min, int max)
{
    return rand()%(min + max);
}

char* randstr(int size)
{
    char* buffer = (char*)malloc(sizeof(char) * size + 1);
    unsigned char c;
    int i;
    for( i = 0 ; i < size ; i++ )
    {
         c = nnrand(1, 256);
         buffer[i] = c;
    }

    return buffer;
}