{"id":3749,"date":"2020-07-03T21:50:53","date_gmt":"2020-07-03T12:50:53","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=3749"},"modified":"2020-07-03T21:51:25","modified_gmt":"2020-07-03T12:51:25","slug":"inotify-%ec%98%88%ec%a0%9c","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/inotify-%ec%98%88%ec%a0%9c\/","title":{"rendered":"inotify \uc608\uc81c"},"content":{"rendered":"\n<p>linux system programming, 283p<\/p>\n\n\n\n<p>\ub9ac\ub205\uc2a4\uc5d0 \ud30c\uc77c, \ub514\ub809\ud1a0\ub9ac\ub97c \uac10\uc2dc\ud558\ub294 watch\uac00 \uc788\ub2e4. \ud2b9\uc815 \uacbd\ub85c\ub97c \uc124\uc815\ud558\uc5ec \uac10\uc2dc\ud558\uc5ec \uc801\uc808\ud55c \uc774\ubca4\ud2b8\ub97c \uc791\uc131\ud560 \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;sys\/inotify.h>\n#include &lt;stdlib.h>\n#include &lt;errno.h>\n#include &lt;stdio.h>\n#include &lt;unistd.h>\n#define BUF_LEN 128 \n\n\/\/int inotify_init1 (int flags);\nvoid main(){\n\tint fd; \/\/file descritor\n\tint wd; \/\/watch descriptor\n\tconst char path[50]=\"\/home\/now0930\/test\/ttt\";\n\tfd = inotify_init1 (0);\n\tif (fd == -1) {\n\t\tperror(\"inotify_init1\");\n\t\texit(EXIT_FAILURE);\n\t\t}\n\n\t\/\/printf(\"%d\", IN_MODIFY);\n\twd = inotify_add_watch(fd, path, IN_OPEN);\n\tif (wd == -1){\n\t\tperror(\"add notify\");\n\t\texit(EXIT_FAILURE);\n\t}\n\tprintf(\"file descriptor is %d\\n\",fd);\n\tprintf(\"path is %s\\n\",path);\n\t\n\t\/\/__attribute__ compiler\uac00 \uc81c\uacf5\ud558\ub294 \uba54\ubaa8\ub9ac \ubc88\uc9c0 \uc815\ub82c. 4\ubc14\uc774\ud2b8 \ub2e8\uc704\ub85c \uc815\ub82c.\n\t\/\/https:\/\/blog.naver.com\/PostView.nhn?blogId=msyang59&amp;logNo=220885276415\n\tchar buf[BUF_LEN] __attribute__((aligned(4)));\n\t\/\/printf(\"buf[0] is  0x%ld\\n\",&amp;buf[0]);\n\t\/\/printf(\"buf[1] is  0x%ld\\n\",&amp;buf[1]);\n\t\/\/printf(\"buf[2] is  0x%ld\\n\",&amp;buf[2]);\n\t\/\/printf(\"buf[3] is  0x%ld\\n\",&amp;buf[3]);\n\t\/\/printf(\"buf[4] is  0x%ld\\n\",&amp;buf[4]);\n\t\/\/printf(\"buf[5] is  0x%ld\\n\",&amp;buf[5]);\n\t\/\/\n\t\/\/\n\tssize_t len, i = 0;\n\n\tfor (int j=0;j&lt;5;j++){\n\t\ti = 0;\n\t\t\/* read BUF_LEN bytes' worth of events *\/\n\t\tlen = read (fd, buf, BUF_LEN);\n\t\tprintf(\"char is %ld\\n\",buf);\n\t\tprintf(\"len is %ld\\n\",len);\n\t\t\/\/\ubcf4\ud1b5 \uc774\ub7f0 \ubc29\uc2dd\uc73c\ub85c \uc0ac\uc6a9.\n\t\t\/* loop over every read event until none remain *\/\n\t\twhile (i &lt; len) {\n\t\t\tstruct inotify_event *event =\n\t\t\t(struct inotify_event *) &amp;buf[i];\n\t\t\tprintf (\"wd=%d mask=%d cookie=%d len=%d dir=%s\\n\", event->wd, event->mask, event->cookie, event->len, (event->mask &amp; IN_ISDIR) ? \"yes\" : \"no\");\n\t\t\t\/* if there is a name, print it *\/\n\t\t\tif (event->len)\n\t\t\t\tprintf (\"name=%s\\n\", event->name);\n\t\t\t\t\/* update the index to the start of the next event *\/\n\t\t\t\/\/printf(\"i is %d\\n\",i);\n\t\t\t\/\/printf(\"buf address is 0x%lx\\n\",&amp;buf[i]);\n\t\t\tif (event->mask &amp;&amp; IN_ACCESS)\n\t\t\t\tprintf(\"the file was read\\n\");\n\t\t\ti += sizeof (struct inotify_event) + event->len;\n\t\t\t}\n\t}\n\n\n\tint ret;\n\tret = inotify_rm_watch(fd, wd);\n\tif(ret)\n\t\tperror(\"remove watch\\n\");\n\t\n}\n\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[21:45:06]>rm .\/a.out ;gcc notify.c;.\/a.out \nnotify.c: In function \u2018main\u2019:\nnotify.c:45:21: warning: format \u2018%ld\u2019 expects argument of type \u2018long int\u2019, but argument 2 has type \u2018char *\u2019 [-Wformat=]\n   printf(\"char is %ld\\n\",buf);\n                   ~~^    ~~~\n                   %s\nfile descriptor is 3\npath is \/home\/now0930\/test\/ttt\nchar is 140729024548928\nlen is 16\nwd=1 mask=32 cookie=0 len=0 dir=no\nthe file was read\nchar is 140729024548928\nlen is 16\nwd=1 mask=32 cookie=0 len=0 dir=no\nthe file was read\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux system programming, 283p \ub9ac\ub205\uc2a4\uc5d0 \ud30c\uc77c, \ub514\ub809\ud1a0\ub9ac\ub97c \uac10\uc2dc\ud558\ub294 watch\uac00 \uc788\ub2e4. \ud2b9\uc815 \uacbd\ub85c\ub97c \uc124\uc815\ud558\uc5ec \uac10\uc2dc\ud558\uc5ec \uc801\uc808\ud55c \uc774\ubca4\ud2b8\ub97c \uc791\uc131\ud560 \uc218 \uc788\ub2e4.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12],"tags":[775,53,776],"class_list":["post-3749","post","type-post","status-publish","format-standard","hentry","category-12","tag-c","tag-linux","tag-system-program"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=3749"}],"version-history":[{"count":2,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3749\/revisions"}],"predecessor-version":[{"id":3751,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3749\/revisions\/3751"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=3749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=3749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=3749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}