Bugzilla – Attachment 46038 Details for
Bug 104658
inotify: inconsistent permission handling
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
IDP Log In
|
Forgot Password
inotify01.c
inotify01.c (text/plain), 7.48 KB, created by
Gernot Payer
on 2005-08-15 11:11:26 UTC
(
hide
)
Description:
inotify01.c
Filename:
MIME Type:
Creator:
Gernot Payer
Created:
2005-08-15 11:11:26 UTC
Size:
7.48 KB
patch
obsolete
>/* > * Copyright (c) 2005 SUSE Linux Products GmbH. All Rights Reserved. > * > * This program is free software; you can redistribute it and/or modify it > * under the terms of version 2 of the GNU General Public License as > * published by the Free Software Foundation. > * > * This program is distributed in the hope that it would be useful, but > * WITHOUT ANY WARRANTY; without even the implied warranty of > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. > * > * Further, this software is distributed without any warranty that it is > * free of the rightful claim of any third person regarding infringement > * or the like. Any license provided herein, whether implied or > * otherwise, applies only to this software file. Patent licenses, if > * any, provided herein do not apply to combinations of this program with > * other software, or any other product whatsoever. > * > * You should have received a copy of the GNU General Public License along > * with this program; if not, write the Free Software Foundation, Inc., 59 > * Temple Place - Suite 330, Boston MA 02111-1307, USA. > * > * > * Authors: > * Gernot Payer <gpayer@suse.de> > * Martin Mrazik <mmrazik@suse.cz> > * > */ > >#include <stdio.h> >#include <string.h> >#include <errno.h> >#include <stdlib.h> >#include <unistd.h> >#include <sys/ioctl.h> >#include <sys/types.h> >#include <sys/stat.h> >#include <fcntl.h> > >#include "inotify.h" > >#include "test.h" >#include "usctest.h" > >char *TCID="inotify01"; /* Test program identifier. */ >int TST_TOTAL=1; /* Total number of test cases. */ >extern int Tst_count; /* Test Case counter for tst_* routines */ > >/* The test environment */ > >struct test_env { > char *path; /* In this tmpdir all file actions take place */ > int inotify_fd; /* File descriptor returned by inotify_init */ > int priv_file; /* single file used during a test */ > int wd; /* watch descriptor used during a single test */ >}; > >/* Unfortunately we have to use global variables :-( */ >struct test_env env; > >/* Test definition structure */ > >struct test_definition { > char *name; > void (*setup)(); > int (*test)(char *); > void (*cleanup)(); > char *errmsg; >}; > >void setup(void); >void cleanup(void); > >/* Helper functions */ > >char *concat(char *s1, char *s2) >{ > static char buf[100]; > buf[0] = '\0'; > strcpy(buf,s1); > strcat(buf,s2); > return buf; >} > >int getevent(struct inotify_event *ev, int buflen, char *errmsg) >{ > size_t ntoread = 0, nread; > > if(-1 == ioctl(env.inotify_fd, FIONREAD, &ntoread)) { > sprintf(errmsg, "ioctl( FIONREAD ) failed: %s", strerror(errno)); > return 0; > } > /* tst_resm(TINFO, "ntoread == %d", ntoread); */ > if(ntoread == 0) { > sprintf(errmsg, "no event available"); > return 0; > } > if(ntoread != (nread = read(env.inotify_fd, ev, ntoread)) ) { > if(nread == -1) > sprintf(errmsg, "read from inotify_fd failed: %s", strerror(errno)); > else > sprintf(errmsg, "short read"); > return 0; > } > return 1; >} > >/* ACCESS event test */ > >void access_setup() >{ > char *fn = concat(env.path,"/access"); > env.priv_file = open(fn, O_CREAT|O_TRUNC|O_RDWR); > write(env.priv_file,"test",4); > lseek(env.priv_file, 0, SEEK_SET); > > env.wd = inotify_add_watch(env.inotify_fd, env.path, IN_ACCESS); > if(env.wd < 0) > tst_brkm(TFAIL, cleanup, "inotify_add_watch failed: %s", strerror(errno)); >} > >int access_test(char *errmsg) >{ > char buf[5]; > char bigbuf[1024]; > struct inotify_event *ev = (struct inotify_event*)bigbuf; > > /* first the access */ > read(env.priv_file,buf,4); > > /* And now the event */ > if(!getevent(ev, 1023, errmsg)) > return 0; > if(ev->wd != env.wd) { > sprintf(errmsg, "Wrong watch descriptor returned"); > return 0; > } > if(ev->mask != IN_ACCESS) { > sprintf(errmsg, "Unexpected event mask: 0x%08x", ev->mask); > return 0; > } > if(0 != strcmp(ev->name, "access")) { > sprintf(errmsg, "Got event for wrong file: %s", ev->name); > return 0; > } > return 1; >} > >void access_cleanup() >{ > //inotify_rm_watch(env.inotify_fd, env.wd); > close(env.priv_file); > //unlink(concat(env.path,"/access")); >} > >/* MODIFY event test */ > >void modify_setup() >{ > char *fn = concat(env.path,"/modify"); > > if(-1 == (env.priv_file = open(fn, O_CREAT|O_TRUNC|O_RDWR))) > tst_brkm(TFAIL, cleanup, "open() failed: %s", strerror(errno)); > write(env.priv_file, "test", 4); > > env.wd = inotify_add_watch(env.inotify_fd, env.path, IN_MODIFY); > if(env.wd < 0) > tst_brkm(TFAIL, cleanup, "inotify_add_watch failed: %s", strerror(errno)); >} > >int modify_test(char *errmsg) >{ > char bigbuf[1024]; > struct inotify_event *ev = (struct inotify_event*)bigbuf; > > /* Modify the watched file */ > write(env.priv_file, "test", 4); > > /* And now get the event */ > if(!getevent(ev, 1023, errmsg)) > return 0; > if(ev->wd != env.wd) { > sprintf(errmsg, "Wrong watch descriptor returned"); > return 0; > } > if(ev->mask != IN_MODIFY) { > sprintf(errmsg, "Unexpected event mask: 0x%08x", ev->mask); > return 0; > } > > return 1; >} > >void modify_cleanup() >{ > //inotify_rm_watch(env.inotify_fd, env.wd); > close(env.priv_file); > //unlink(concat(env.path,"/modify")); >} > >/* ATTRIB event test */ > >void attrib_setup() >{ > char *fn = concat(env.path,"/attrib"); > > if(-1 == (env.priv_file = open(fn, O_CREAT|O_TRUNC|O_WRONLY))) > tst_brkm(TFAIL, cleanup, "open() failed: %s", strerror(errno)); > > env.wd = inotify_add_watch(env.inotify_fd, env.path, IN_ATTRIB); > if(env.wd < 0) > tst_brkm(TFAIL, cleanup, "inotify_add_watch failed: %s", strerror(errno)); >} > >int attrib_test(char *errmsg) >{ > char bigbuf[1024]; > struct inotify_event *ev = (struct inotify_event*)bigbuf; > > /* Modify file attributes */ > if(0 != fchmod(env.priv_file, 0777)) > tst_brkm(TFAIL, cleanup, "fchmod() failed: %s", strerror(errno)); > > /* And now get the event */ > if(!getevent(ev, 1023, errmsg)) > return 0; > if(ev->wd != env.wd) { > sprintf(errmsg, "Wrong watch descriptor returned"); > return 0; > } > if(ev->mask != IN_ATTRIB) { > sprintf(errmsg, "Unexpected event mask: 0x%08x", ev->mask); > return 0; > } > > return 1; >} > >void attrib_cleanup() >{ > inotify_rm_watch(env.inotify_fd, env.wd); > close(env.priv_file); > unlink(concat(env.path,"/attrib")); >} > >struct test_definition all_tests[] = { > { "ACCESS", access_setup, access_test, access_cleanup, NULL}, > { "MODIFY", modify_setup, modify_test, modify_cleanup, NULL}, > { "ATTRIB", attrib_setup, attrib_test, attrib_cleanup, NULL}, > { NULL, NULL, NULL, NULL, NULL } >}; > >void setup() >{ > env.path = (char*)malloc(23); > if(!env.path) > tst_brkm(TBROK, NULL, "out of memory"); > > env.inotify_fd = inotify_init(); > if(env.inotify_fd < 0) > tst_brkm(TBROK, NULL, "inotify_init failed: %s", strerror(errno)); > > strcpy(env.path, "/tmp/inotifytestXXXXXX"); > if(NULL == mkdtemp(env.path)) > tst_brkm(TBROK, NULL, "could not create temporary directory: %s", strerror(errno)); > > TEST_PAUSE; >} > >void cleanup() >{ > system(concat("rm -rf ",env.path)); > free(env.path); > > TEST_CLEANUP; > tst_exit(); >} > >int main(int ac, char **av) >{ > int lc; /* loop counter */ > char *msg; /* message returned from parse_opts */ > int i, rc; > char errmsg[256]; > > /* parse standard options */ > if ( (msg=parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *) NULL ) > tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); > > setup(); > > for (lc = 0; TEST_LOOPING(lc); lc++) { > i = 0; > Tst_count = 0; > while (all_tests[i].test != NULL) { > if(all_tests[i].setup != NULL) > all_tests[i].setup(); > rc = all_tests[i].test(errmsg); > if(rc) > tst_resm(TPASS, "%s: Success", all_tests[i].name); > else > tst_resm(TFAIL, "%s: %s", all_tests[i].name, errmsg); > if(all_tests[i].cleanup != NULL) > all_tests[i].cleanup(); > i++; > } > } > > cleanup(); > > return 2; /* We should never get here */ >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
Actions:
View
Attachments on
bug 104658
: 46038