3 * Copyright (C) 2012 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /*****************************************************************************
21 * File mymount.c: An example for mount function
23 * Author: S. Piccardi Jan. 2012
25 *****************************************************************************/
26 #include <sys/types.h> /* primitive system data types */
27 #include <sys/stat.h> /* file characteristics constants and functions */
28 #include <stdlib.h> /* C standard library */
29 #include <stdio.h> /* standard I/O library */
30 #include <unistd.h> /* unix standard library */
32 #include <sys/mount.h>
34 /* pass -D DEBUG to gcc to enable debug printing */
38 #define debug(fmt, arg...)
41 /* Adding undefined flags in glibc headers */
43 #define MS_DIRSYNC 128
55 #define MS_SILENT 32768
58 #define MS_UNBINDABLE (1<<17)
61 #define MS_PRIVATE (1<<18)
64 #define MS_SLAVE (1<<19)
67 #define MS_SHARED (1<<20)
70 #define MS_RELATIME (1<<21)
72 #ifndef MS_STRICTATIME
73 #define MS_STRICTATIME (1<<24)
80 /* Help printing routine */
83 /* struct for name<->flag correspondence */
89 /* compare funtion to find a flag name */
90 int ms_flag_cmp(const void *c1, const void *c2)
92 struct ms_flag *v1 = (struct ms_flag *) c1;
93 struct ms_flag *v2 = (struct ms_flag *) c2;
94 return strcmp(v1->name, v2->name);
98 int main(int argc, char *argv[])
101 * Variables definition
104 char * mountflags=NULL, *data=NULL, *filesystemtype=NULL;
106 /* mount flag table, must be alphabetically ordered */
107 struct ms_flag ms_values[]=
110 {"dirsync",MS_DIRSYNC},
111 {"mandlock",MS_MANDLOCK},
113 {"noatime",MS_NOATIME},
115 {"nodiratime",MS_NODIRATIME},
116 {"noexec",MS_NOEXEC},
117 {"nosuid",MS_NOSUID},
118 {"private",MS_PRIVATE},
119 {"rdonly",MS_RDONLY},
121 {"relatime",MS_RELATIME},
122 {"remount",MS_REMOUNT},
124 {"silent",MS_SILENT},
126 {"strictatime",MS_STRICTATIME},
127 {"synchronous",MS_SYNCHRONOUS},
128 {"unbindable",MS_UNBINDABLE}
130 struct ms_flag key, *result;
131 int ms_count = sizeof(ms_values)/sizeof(struct ms_flag);
134 * Input section: decode command line parameters
135 * Use getopt function
137 opterr = 0; /* don't want writing to stderr */
138 while ( (i = getopt(argc, argv, "o:f:t:h")) != -1) {
143 case 'h': /* help option */
144 printf("Wrong -h option use\n");
148 case 'o': /* options data */
150 debug("passed option data: %s\n", data);
152 case 't': /* filesystem type */
153 filesystemtype = optarg;
154 debug("passed filesystem type: %s\n", filesystemtype);
156 case 'f': /* mount flags */
158 debug("passed mount flags: %s\n", mountflags);
160 case '?': /* unrecognized options */
161 printf("Unrecognized options -%c\n",optopt);
163 default: /* should not reached */
167 /* ***********************************************************
169 * Options processing completed
171 * Main code beginning
173 * ***********************************************************/
174 /* There must be 2 arguments, source and target */
175 if ((argc - optind) != 2) {
176 printf("Wrong number of arguments %d\n", argc - optind);
179 debug("source %s, target %s\n",argv[optind], argv[optind+1]);
181 /* parse -f option parameter, and build mount flags argument */
182 if ( token = strtok(mountflags, ",") ) {
185 result = bsearch(&key, ms_values, ms_count, sizeof(struct ms_flag),
188 debug("found option: %s\n",result->name);
189 flags |= result->flag;
191 printf("unknown flag: %s\n", key.name);
194 } while (token=strtok(NULL,","));
196 debug("flags value %X\n", flags);
198 if (mount(argv[optind], argv[optind + 1], filesystemtype, flags, data) == -1) {
199 perror("Mount call failed");
205 * routine to print usage info and exit
208 printf("Program mymount: \n");
210 printf("mymount [-h] [-o opt1[,...]] [-t type] [-f fl1[,...]] source target\n");
211 printf(" -h print this help\n");
212 printf(" -o options comma separated options data string\n");
213 printf(" -t fstype filesystemtype string\n");
214 printf(" -f flags comma separated mount flags string\n");
216 printf("you always need to specify two argument, source and target,\n");
217 printf("matching the system call ones, if one of them is not needed\n");
218 printf("just pass an empty string\n");