3 * Copyright (C) 2007 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 2 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 myls.c: An example ls done after a chroot
23 * Author: S. Piccardi Apr. 2007
25 *****************************************************************************/
26 #include <sys/types.h> /* primitive system data types */
27 #include <sys/stat.h> /* file characteristics constants and functions */
28 #include <dirent.h> /* directory operation constants and functions */
29 #include <stdlib.h> /* C standard library */
30 #include <unistd.h> /* unix standard library */
34 * Program mylschrootout
36 * List files and their size inside exiting from a chroot
38 /* Help printing routine */
40 /* computation function for dir_scan */
41 int do_ls(struct dirent * direntry);
43 int main(int argc, char *argv[])
46 char * arg[]={"ls","-l", NULL};
49 * Input section: decode command line parameters
52 opterr = 0; /* don't want writing to stderr */
53 while ( (i = getopt(argc, argv, "h")) != -1) {
58 case 'h': /* help option */
59 printf("Wrong -h option use\n");
63 case '?': /* unrecognized options */
64 printf("Unrecognized options -%c\n",optopt);
66 default: /* should not reached */
70 /* ***********************************************************
72 * Options processing completed
76 * ***********************************************************/
77 if ((argc - optind) != 1) { /* There must be remaing parameters */
78 printf("Wrong number of arguments %d\n", argc - optind);
82 printf("Chrooting to %s\n", argv[1]);
83 if (chroot(argv[1])) {
84 perror("Chroot fail");
87 if (execve("/ls", arg, NULL)) {
88 perror("errore in execve");
90 if (stat("/ls", &data)) {
91 perror("errore in stat");
93 printf("inode: %d\n", data.st_ino);
95 mkdir("escape", 0777);
97 dir_scan("../", do_ls);
102 * Routine to print file name and size inside dir_scan
104 int do_ls(struct dirent * direntry)
107 stat(direntry->d_name, &data); /* get stat data */
108 printf("File: %s \t inode: %d\n", direntry->d_name, data.st_ino);
112 * routine to print usage info and exit
115 printf("Program mylschrootout: chroot in a directory and list files \n");
117 printf(" myls [-h] dirname \n");
118 printf(" -h print this help\n");