16081088a5c727c2c5517ae3817c53e519ece8fa
[tpg/acess2.git] / Usermode / Applications / mount_src / main.c
1 /*
2  * Acess2 mount command
3  */
4 #include <acess/sys.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #define MOUNTABLE_FILE  "/Acess/Conf/Mountable"
9 #define MOUNTED_FILE    "/Devices/System/VFS/Mounts"
10
11 // === PROTOTYPES ===
12 void    ShowUsage();
13  int    GetMountDefs(char *Ident, char **spDevice, char **spDir, char **spType, char **spOptions);
14
15 // === CODE ===
16 /**
17  * \fn int main(int argc, char *argv[])
18  * \brief Entrypoint
19  */
20 int main(int argc, char *argv[])
21 {
22          int    fd;
23          int    i;
24         char    *arg;
25         char    *sType = NULL;
26         char    *sDevice = NULL;
27         char    *sDir = NULL;
28         char    *sOptions = NULL;
29
30         if(argc < 3) {
31                 ShowUsage();
32                 return EXIT_FAILURE;
33         }
34         
35         // Parse Arguments
36         for( i = 1; i < argc; i++ )
37         {
38                 arg = argv[i];
39                 
40                 if(arg[0] == '-')
41                 {
42                         switch(arg[1])
43                         {
44                         // -t <driver> :: Filesystem driver to use
45                         case 't':       sType = argv[++i];      break;
46                         case '-':
47                                 //TODO: Long Arguments
48                         default:
49                                 ShowUsage(argv[0]);
50                                 return EXIT_FAILURE;
51                         }
52                         continue;
53                 }
54                 
55                 // Device?
56                 if(sDevice == NULL) {
57                         sDevice = arg;
58                         continue;
59                 }
60                 
61                 // Directory?
62                 if(sDir == NULL) {
63                         sDir = arg;
64                         continue;
65                 }
66                 
67                 ShowUsage(argv[0]);
68                 return EXIT_FAILURE;
69         }
70
71         // Check if we even got a device/mountpoint
72         if(sDevice == NULL) {
73                 ShowUsage(argv[0]);
74                 return EXIT_FAILURE;
75         }
76
77         // If no directory was passed (we want to use the mount list)
78         // or we are not root (we need to use the mount list)
79         // Check the mount list
80         if(sDir == NULL || getuid() != 0)
81         {
82                 // Check if it is defined in the mounts file
83                 // - At this point sDevice could be a device name or a mount point
84                 if(GetMountDefs(sDevice, &sDevice, &sDir, &sType, &sOptions) == 0)
85                 {
86                         if(sDir == NULL)
87                                 fprintf(stderr, "Unable to find '%s' in '%s'\n",
88                                         sDevice, MOUNTABLE_FILE
89                                         );
90                         else
91                                 fprintf(stderr, "You must be root to mount devices or directories not in '%s'\n",
92                                         MOUNTABLE_FILE
93                                         );
94                         return EXIT_FAILURE;
95                 }
96         
97                 // We need to be root to mount a filesystem, so, let us be elevated!
98                 setuid(0);      // I hope I have the setuid bit implemented.
99         }
100         else
101         {
102                 // Check that we were passed a filesystem type
103                 if(sType == NULL) {
104                         fprintf(stderr, "Please pass a filesystem type\n");
105                         return EXIT_FAILURE;
106                 }
107         }
108         
109         // Check Device
110         fd = open(sDevice, OPENFLAG_READ);
111         if(fd == -1) {
112                 printf("Device '%s' cannot be opened for reading\n", sDevice);
113                 return EXIT_FAILURE;
114         }
115         close(fd);
116         
117         // Check Mount Point
118         fd = open(sDir, OPENFLAG_EXEC);
119         if(fd == -1) {
120                 printf("Directory '%s' does not exist\n", sDir);
121                 return EXIT_FAILURE;
122         }
123         close(fd);
124
125         // Replace sOptions with an empty string if it is still NULL
126         if(sOptions == NULL)    sOptions = "";
127
128         // Let's Mount!
129         _SysMount(sDevice, sDir, sType, sOptions);
130
131         return 0;
132 }
133
134 void ShowUsage(char *ProgName)
135 {
136         fprintf(stderr, "Usage:\n", ProgName);
137         fprintf(stderr, "    %s [-t <type>] <device> <directory>\n");
138         fprintf(stderr, "or  %s <device>\n");
139         fprintf(stderr, "or  %s <directory>\n");
140 }
141
142 /**
143  * \fn int GetMountDefs(char *Ident, char **spDevice, char **spDir, char **spType, char **spOptions)
144  * \brief Reads the mountable definitions file and returns the corresponding entry
145  * \param spDevice      Pointer to a string (pointer) determining the device (also is the input for this function)
146  * \note STUB
147  */
148 int GetMountDefs(char *Ident, char **spDevice, char **spDir, char **spType, char **spOptions)
149 {
150         // TODO: Read the mounts file (after deciding what it will be)
151         return 0;
152 }

UCC git Repository :: git.ucc.asn.au