File: test_sysfs.c

package info (click to toggle)
pmount 0.9.23-10
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,344 kB
  • sloc: sh: 10,190; ansic: 2,459; makefile: 72
file content (79 lines) | stat: -rw-r--r-- 2,048 bytes parent folder | download | duplicates (6)
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
/* 
 * Copyright (c) 2009 Vincent Fourmond <fourmond@debian.org>
 *
 * This software is distributed under the terms and conditions of the 
 * GNU General Public License. See file GPL for the full text of the license.
 */

/**
   This program tries uses find_sysfs_device from src/policy.c to find
   sysfs device information for the device given as argument.

   This program is only intended for development purposes.

   DO NOT INSTALL IT SUID ROOT !

   DO NOT RUN IT WITH ROOT PRIVILEGES ! (there's no need for that)
 */


/* #include "policy.h" */
#include <stdio.h>

/* For stat: */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/* The very functions we want to test ! */
int find_sysfs_device( const char* dev, char* blockdevpath, 
		       size_t blockdevpathsize );

int is_blockdev_attr_true( const char* blockdevpath, 
			   const char* attr );

const char * bus_has_ancestry(const char * blockdevpath, 
			      char** buses);

static char* hotplug_buses[] = { "usb", "ieee1394", 
				 "mmc", "pcmcia", NULL };



int main(int argc, char *argv[])
{
  char device_path[512];
  const char * bus;
  struct stat devstat;
  if(argc != 2) {
    fprintf(stderr, "Usage: %s device\n", argv[0]);
    return 1;
  }
  if(stat(argv[1], &devstat)) {
    perror("Could not open file");
    return 1;
  }
  if(! (devstat.st_mode & S_IFBLK)) {
    fprintf(stderr, "Sorry, `%s' is not a block device.\n", argv[1]);
    return 1;
  }

  if(find_sysfs_device(argv[1], device_path, sizeof(device_path))) {
    fprintf(stdout, "Found sysfs device for %s : %s\n", 
	    argv[1], device_path);
    fprintf(stdout, "Device %s is removable: %s\n",
	    argv[1], is_blockdev_attr_true(device_path,"removable") ?
	    "yes" :"no");
    bus = bus_has_ancestry(device_path, hotplug_buses);
    if(bus) 
      fprintf(stdout, "Found whitelisted bus: %s\n", bus);
    else 
      fprintf(stdout, "No whitelisted bus found\n", bus);
    return 0;
  }
  else {
    fprintf(stderr, "find_sysfs_device failed for %s\n", argv[1]);
    return 1;
  }

}