Skip to content

Instantly share code, notes, and snippets.

@merces
Created November 21, 2012 12:24
Show Gist options
  • Save merces/4124617 to your computer and use it in GitHub Desktop.
Save merces/4124617 to your computer and use it in GitHub Desktop.
devprot - (un)set write protection flag in block devices
/*
devprot - (un)set write protection flag in block devices
Copyright (C) 2012 Fernando Mercês
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <unistd.h>
void fatal(const char *msg)
{
fprintf(stderr, msg);
exit(1);
}
void usage()
{
printf("Usage:\n\t"
"devprot <device>\n"
"Example:\n\t"
"devprot /dev/sdc\n");
}
int main(int argc, char *argv[])
{
int fd, state;
if (argc != 2)
{
usage();
exit(1);
}
// tenta abri-lo
if ((fd = open(argv[1], O_RDONLY|O_NONBLOCK)) == -1)
fatal("unable to open device\n");
// pega o estado atual
if (ioctl(fd, BLKROGET, &state) == -1)
fatal("error getting device status - are you ruth?\n");
// inverte a logica da coisa
state = !state;
// configura o novo estado
if (ioctl(fd, BLKROSET, &state) == -1)
fatal("error setting device status - are you ruth?\n");
printf("%s%sprotected!\n", argv[1], !state ? " un" : " ");
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment