3

I'm trying to write a program that checks files in a directory. When a new file is created I have to check if it executable and if so I have to execute it.

I'm using the inotify interface and it works well, but I have some problems when I try to check whether a file is executable using stat(2). I found that I don't have permission to execute it.

Does passing to the program an absolute path of the directory that I want to check create permissions problems?

int main(int argc,char * argv []){

int fd,wd,len,i=0;
char buffer [EVENT_SIZE_BUF];

if(argc != 2)
    usage();

if((fd = inotify_init()) == -1)
    perror("inotify_init()");   


if((wd = inotify_add_watch(fd,argv[1],IN_CREATE) -1))
    perror("inotify_add_watch()");

while(1){


    if((len = read(fd,buffer,EVENT_SIZE_BUF)) < 0)
        perror("read()");

    struct inotify_event * ev = (struct inotify_event *)&buffer;

    if(ev->len > 0){

        if(ev->mask & IN_CREATE && ((ev-> mask & IN_ISDIR) == 0x00)){

            printf("SPY; new file is created %s\n",ev->name);

            char * path = strcat(argv[1],ev->name);
            printf("%s\n",path);

            struct stat sb;

            if(!stat(path,&sb)){

                printf( (S_ISDIR(sb.st_mode)) ? "d" : "-");
                printf( (sb.st_mode & S_IRUSR) ? "r" : "-");
                printf( (sb.st_mode & S_IWUSR) ? "w" : "-");
                printf( (sb.st_mode & S_IXUSR) ? "x" : "-");
                printf( (sb.st_mode & S_IRGRP) ? "r" : "-");
                printf( (sb.st_mode & S_IWGRP) ? "w" : "-");
                printf( (sb.st_mode & S_IXGRP) ? "x" : "-");
                printf( (sb.st_mode & S_IROTH) ? "r" : "-");
                printf( (sb.st_mode & S_IWOTH) ? "w" : "-");
                printf( (sb.st_mode & S_IXOTH) ? "x" : "-");
                fflush(stdout);
                printf("\n");
            }




        }else{ printf("dir\n"); }
    }               
}

inotify_rm_watch(fd,wd);
close(fd);
return 0;
}
1
  • I've edited the question to try to make it a bit clearer. Feel free to roll-back/edit it if I've made incorrect changes/forgotten something. Commented Apr 16, 2013 at 13:58

3 Answers 3

1

You are using linux, so you can use access() function

if (access("/my/file", X_OK) != -1) {
    printf("execution permission enabled\n");
} else {
    printf("execution permission disabled\n");
}
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for answer. i tryed access too but are you shure that if it returns -1 means that the file is not executable? because linux man page tells that it return -1 in case of error
ah ok yes but it always tells that i don't have permissions. but i have execution permission on both files
check your file it should have not execution permission
done. i have permission on both files checked with stat from shell
Note that in the case of a normal (not setuid, not setgid) program, access() provides the answer you want, but the check that it performs is whether the real UID or real GID (or one of the supplementary group IDs) has the relevant access, rather then the effective UID or effective GID, but it is the effective UID and effective GID (plus the supplementary group IDs) that will actually control the access. Thus, you could end up with the wrong answer if the program is run SUID or SGID. Admittedly, this is cavilling at the details, but they do matter...sometimes.
0

Are you solving Renzo Davoli's exercises? :)

However, I was stuck likes you and I've discovered that copying an executable with Nautilus, it creates a "Executable (Copy)" that returns X-Permission error with stat() or access().

Copying with cp cp exe1 exe2 it works!!

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <sys/stat.h>

#define _GNU_SOURCE 
#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

void main(int argc, char **argv){

int fd, wd, length, i=0;
char buffer[EVENT_BUF_LEN];

char* dir = argv[1];

/*creating the INOTIFY instance*/
if((fd = inotify_init()) < 0)
    perror( "inotify_init" );


/*adding the directory into watch list. 
Here, the suggestion is to validate the existence of the directory before adding into monitoring list.*/
wd = inotify_add_watch( fd, dir, IN_CREATE | IN_DELETE );

while(1) {

    i=0;
    pid_t p;
    struct stat st;

    /*read to determine the event change happens on “/tmp” directory. Actually this read blocks until the change event occurs*/ 
    if((length = read(fd, buffer, EVENT_BUF_LEN )) < 0)
        perror( "read" );
    /*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly.*/
    while ( i < length ){     
        struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];     
        if ( event->len ) {
            if ( event->mask & IN_CREATE ) {
                if ( event->mask & IN_ISDIR )
                    printf("New directory %s created.\n", event->name );
                else{
                    printf("New file %s created.\n", event->name );
                    char* file_name;
                    asprintf(&file_name, "%s/%s", dir, event->name);

                    printf("Check if %s is executable\n", file_name);
                    if (access(file_name, X_OK) == 0){ //If file is executable
                        printf("Executable file! Call fork()\n");
                        if ((p = fork()) == 0) {  // Child process: do your work here
                            if (execlp(file_name, file_name,  (char*) 0)<0)
                                fprintf(stderr, "execlp error\n");
                            exit(0);
                        }
                        else{
                            while(waitpid(p, NULL, WNOHANG) != p);
                            remove (event->name);
                            printf("%s done & gone\n", event->name);
                        }
                    }
                    else
                        fprintf(stderr, "Not executable file\n");
                }
            }
            else if ( event->mask & IN_DELETE ) {
                if ( event->mask & IN_ISDIR ) 
                    printf( "Directory %s deleted.\n", event->name );
                else
                    printf( "File %s deleted.\n", event->name );
            }
        }

        i += EVENT_SIZE + event->len;
    }
}

/*removing the “/tmp” directory from the watch list.*/
inotify_rm_watch( fd, wd );

/*closing the INOTIFY instance*/
close( fd );

 }

Comments

0

This is impossible in ANSI C, you need POSIX.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.