-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimpadsk.c
166 lines (139 loc) · 3.43 KB
/
limpadsk.c
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
#define _FILE_OFFSET_BITS 64
// asprintf, basename
#define _GNU_SOURCE
// setores a apagar
#define NSETORES 4096
#include <fcntl.h>
#include <glob.h>
#include <inttypes.h>
#include <limits.h>
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd, setor_sz, status;
int r = EXIT_FAILURE;
uint64_t dev_sz, intervalo[2];
struct stat sb;
char *devname, *devglob;
pid_t pid;
if (argc != 2)
{
fprintf(stderr, "Uso: %s dispositivo\n", basename(argv[0]));
exit(EXIT_FAILURE);
}
devname = realpath(argv[1], NULL);
if (devname == NULL)
{
perror("realpath");
exit(EXIT_FAILURE);
}
fd = open(devname, O_RDWR|O_EXCL);
if (fd < 0)
{
perror("open");
goto fim1;
}
if (fstat(fd, &sb) != 0)
{
perror("fstat");
goto fim2;
}
if (!S_ISBLK(sb.st_mode))
{
fprintf(stderr, "Não é dispositivo de bloco.\n");
goto fim2;
}
if (asprintf(&devglob, "%s*", devname) < 0)
{
perror("asprintf");
goto fim2;
}
// https://systemd.io/BLOCK_DEVICE_LOCKING/
flock(fd, LOCK_EX);
pid = fork();
if (pid < 0)
{
perror("fork");
goto fim3;
}
if (pid == 0)
{
glob_t gl;
gl.gl_offs = 3;
glob(devglob, GLOB_DOOFFS|GLOB_NOSORT, NULL, &gl);
gl.gl_pathv[0] = "wipefs";
gl.gl_pathv[1] = "--all";
gl.gl_pathv[2] = "--force";
if (gl.gl_pathc > 0)
{
unsetenv("LOCK_BLOCK_DEVICE");
execvp(gl.gl_pathv[0], gl.gl_pathv);
perror(gl.gl_pathv[0]);
}
exit(EXIT_FAILURE);
}
while (waitpid(pid, &status, 0) < 0)
{
continue;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS)
{
fprintf(stderr, "Erro ao rodar wipefs: assinaturas possivelmente não apagadas.\n");
}
if (ioctl(fd, BLKSSZGET, &setor_sz))
{
perror("ioctl BLKSSZGET");
goto fim3;
}
if (ioctl(fd, BLKGETSIZE64, &dev_sz))
{
perror("ioctl BLKGETSIZE64");
goto fim3;
}
intervalo[0] = 0;
intervalo[1] = NSETORES * setor_sz;
if (dev_sz >= (2 * intervalo[1]))
{
printf("Zerando %" PRIu64 " bytes no início do dispositivo... ", intervalo[1]);
fflush(stdout);
r = ioctl(fd, BLKZEROOUT, &intervalo);
printf("%s.\n", r ? "falha" : "sucesso");
fflush(stdout);
intervalo[0] = dev_sz - intervalo[1];
printf("Zerando %" PRIu64 " bytes no fim do dispositivo (offset %" PRIu64 " bytes)... ",
intervalo[1], intervalo[0]);
fflush(stdout);
r = ioctl(fd, BLKZEROOUT, &intervalo);
printf("%s.\n", r ? "falha" : "sucesso");
fflush(stdout);
}
else
{
fprintf(stderr, "Dispositivo pequeno demais: início e fim não zerados.\n");
}
intervalo[0] = 0;
intervalo[1] = dev_sz;
printf("TRIMando o dispositivo... ");
fflush(stdout);
r = ioctl(fd, BLKDISCARD, &intervalo);
printf("%s.\n", r ? "sem suporte" : "sucesso");
fflush(stdout);
ioctl(fd, BLKRRPART);
r = EXIT_SUCCESS;
fim3:
free(devglob);
fim2:
close(fd);
fim1:
free(devname);
return r;
}