]> git.ipfire.org Git - ipfire-2.x.git/blame - src/misc-progs/smartctrl.c
misc-progs: smartctrl: Sanitise device name
[ipfire-2.x.git] / src / misc-progs / smartctrl.c
CommitLineData
40b0879d
MT
1/* This file is part of the IPFire Firewall.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 */
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
12#include <sys/types.h>
13#include <fcntl.h>
14#include "setuid.h"
15
16int main(int argc, char *argv[]) {
614f54b9 17 if (!(initsetuid()))
ab89cb22 18 exit(1);
40b0879d 19
ab89cb22
MT
20 if (argc < 2) {
21 fprintf(stderr, "\nNo argument given.\n\nsmartctrl <device>\n\n");
22 exit(1);
23 }
614f54b9 24
14dc1c68
MT
25 if (!is_valid_argument_alnum(argv[1])) {
26 fprintf(stderr, "Invalid device name '%s'\n", argv[1]);
27 exit(2);
28 }
29
ab89cb22
MT
30 char command[STRING_SIZE];
31 snprintf(command, STRING_SIZE, "/var/run/hddshutdown-%s", argv[1]);
614f54b9 32
ab89cb22
MT
33 FILE *fp = fopen(command, "r");
34 if (fp != NULL) {
614f54b9 35 fclose(fp);
ab89cb22 36
614f54b9 37 printf("\nDisk %s is in Standby. Do nothing because we won't wakeup\n",argv[1]);
ab89cb22 38 exit(1);
40b0879d 39 }
40b0879d 40
ab89cb22
MT
41 snprintf(command, STRING_SIZE, "smartctl -iHA /dev/%s", argv[1]);
42 safe_system(command);
614f54b9
MT
43
44 return 0;
40b0879d 45}