{"id":344379,"date":"2023-01-24T09:02:17","date_gmt":"2023-01-24T09:02:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=344379"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=344379","title":{"rendered":"<span>\u0423\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430 Linux<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e? \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b \u043f\u043e\u043c\u043e\u0433\u0430\u043b\u0438 \u0432\u044b\u044f\u0432\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 Linux, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c, \u043a\u0430\u043a \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u044c \u0441 \u0441\u043e\u043a\u0435\u0442\u043e\u043c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/876\/86c\/e25\/87686ce2550e4400b074666f5da85033.jpg\" width=\"1920\" height=\"1080\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/876\/86c\/e25\/87686ce2550e4400b074666f5da85033.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0438 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/33d\/f9d\/2dc\/33df9d2dc3c8cafaac2b845560f48650.png\" width=\"936\" height=\"218\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/33d\/f9d\/2dc\/33df9d2dc3c8cafaac2b845560f48650.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>Application<\/strong>: \u043b\u044e\u0431\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430.<\/p>\n<p><strong>Kernel Module<\/strong>: \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f API-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u044f\u0434\u0440\u0430 \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/e14\/ab8\/44c\/e14ab844ccc43edb454cc788fd74cfa8.jpg\" width=\"780\" height=\"100\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e14\/ab8\/44c\/e14ab844ccc43edb454cc788fd74cfa8.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p><a href=\"https:\/\/slurm.club\/3R0i8ZI\">\u00ab\u0410\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u00a0Linux.\u00a0\u041c\u0435\u0433\u0430\u00bb<\/a><\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430<\/h3>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u044f\u0434\u0440\u0430.<\/p>\n<h4>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430<\/h4>\n<p><strong>insmod<\/strong>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043c\u043e\u0434\u0443\u043b\u044f \u0432 \u044f\u0434\u0440\u043e.<\/p>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: insmod \u2018kernel_ext_binary\u2019<\/p>\n<pre><code># insmod helloWorld.ko Welcome to Hello world Module.<\/code><\/pre>\n<h4>\u0412\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430<\/h4>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: rmmod \u2018kernel_ext_binary\u2019<\/p>\n<pre><code># rmmod helloWorld.ko Goodbye, from Hello world.<\/code><\/pre>\n<h4>\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u044f\u0434\u0440\u0430<\/h4>\n<p><strong>lsmod<\/strong>: \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u044f\u0434\u0440\u0430.\u00a0<\/p>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: lsmod | grep \u2018kernel_ext_binary\u2019<\/p>\n<pre><code># lsmod | grep hello helloWorld 12189  1<\/code><\/pre>\n<h3>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043c\u043e\u0434\u0443\u043b\u0435 \u044f\u0434\u0440\u0430<\/h3>\n<p><strong>modinfo<\/strong>: \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043c\u043e\u0434\u0443\u043b\u0435.\u00a0<\/p>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: modinfo hello*.ko<\/p>\n<pre><code># modinfo helloWorld.ko filename:       \/root\/helloWorld.ko description:    Basic Hello World KE author:         helloWorld license:        GPL rhelversion:    7.3 srcversion:     5F60F86F84D8477986C3A50 depends: vermagic:       3.10.0-514.el7.ppc64le SMP mod_unload modversions<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0438 \u0447\u0435\u0440\u0435\u0437 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 <code>system()<\/code>.<\/p>\n<h3>\u0421\u0432\u044f\u0437\u044c \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c<\/h3>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0444\u0430\u0439\u043b \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e open() API. \u042d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0438 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u0412\u0441\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435.<\/p>\n<pre><code>#include &lt;sys\/types.h> #include &lt;sys\/stat.h> #include &lt;fcntl.h>  int open(const char *pathname, int flags);<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code>int fd; #define DEVICE_FILE_NAME \"\/dev\/char_dev\" fd = open(DEVICE_FILE_NAME, 0);<\/code><\/pre>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<strong> <\/strong><code>open()<\/code> \u2014 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440 \u0444\u0430\u0439\u043b\u0430, \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445 (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>ioctl<\/code>).<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 ioctl<\/h3>\n<p>\u0421\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 <code>ioctl()<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<pre><code>#include &lt;sys\/ioctl.h>  int ioctl(int fd, int cmd, ...);<\/code><\/pre>\n<p><code>fd<\/code> \u2014 \u044d\u0442\u043e \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0438\u0437 <code>open()<\/code>, \u0430 <code>cmd<\/code> \u2014 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432 <code>ioctl()<\/code> \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code>#define IOCTL_SEND_MSG _IOR(MAJOR_NUM, 0, char *) int ret_val; char message[100]; ret_val = ioctl(file_desc, IOCTL_SEND_MSG, message); if (ret_val &lt; 0) { printf(\"ioctl_send_msg failed:%d\\n\", ret_val); exit(\u22121); }<\/code><\/pre>\n<p>\u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <code>IOCTL_SEND_MSG<\/code> \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044e.<\/p>\n<p><code>_IOR<\/code> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043d\u043e\u043c\u0435\u0440 \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>ioctl<\/code> \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430.\u00a0<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, <code>MAJOR_NUM<\/code>, \u2014 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u043d\u0430\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u043d\u043e\u043c\u0435\u0440 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (\u0438\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441 \u0440\u0430\u0437\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c).<\/p>\n<p>\u0422\u0440\u0435\u0442\u0438\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043a \u044f\u0434\u0440\u0443.<\/p>\n<p>\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 \u044f\u0434\u0440\u0430 \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 <code>ioctl<\/code>.<\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 \u044f\u0434\u0440\u0430<\/h3>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u0434\u0435\u043b\u0430\u0445 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u044f\u0434\u0440\u0430.\u00a0<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430<\/h4>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u044b:<\/p>\n<pre><code>#include &lt;linux\/kthread.h> static struct task_struct * sampleThread = NULL; sampleThread = kthread_run(threadfn, data, namefmt, \u2026)<\/code><\/pre>\n<p><code>kthread_run()<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u0435\u043c\u0443 \u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0435.<\/p>\n<p><code>threadfn<\/code> \u2014 \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430.\u00a0<\/p>\n<p><code>data<\/code> * \u2014 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p><code>namefmt<\/code> \u2014 \u0438\u043c\u044f \u043f\u043e\u0442\u043e\u043a\u0430 (\u0432 \u0432\u044b\u0432\u043e\u0434\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>ps<\/code>)<\/p>\n<h4>\u041e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u0430<\/h4>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0432\u044b\u0437\u043e\u0432:<\/p>\n<p><code>kthread_stop(sampleThread)<\/code><\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0441\u043e\u043a\u0435\u0442\u043e\u043c<\/h3>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0439 \u0441\u043e\u043a\u0435\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>sock_create()<\/code>. \u0427\u0435\u0440\u0435\u0437 \u044d\u0442\u043e\u0442 \u0441\u043e\u043a\u0435\u0442 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0438\u043b\u0438 \u0432\u043d\u0435 \u0445\u043e\u0441\u0442\u0430.<\/p>\n<pre><code>struct socket *sock; struct sockaddr_ll *s1 = kmalloc(sizeof(struct sockaddr_ll),GFP_KERNEL); result = sock_create(PF_PACKET, SOCK_RAW, htons(ETH_P_IP), &amp;sock); if(result &lt; 0) { printk(KERN_INFO \"[vmmKE] unable to create socket\");     return -1; }  \/\/copy the interface name to ifr.name  and other required information. strcpy((char *)ifr.ifr_name, InfName); s1->sll_family = AF_PACKET; s1->sll_ifindex = ifindex; s1->sll_halen = ETH_ALEN; s1->sll_protocol = htons(ETH_P_IP);  result = sock->ops->bind(sock, (struct sockaddr *)s1, sizeof(struct sockaddr_ll)); if(result &lt; 0) { printk(KERN_INFO \"[vmmKE] unable to bind socket\");     return -1; }<\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>sock_sendmsg()<\/code> \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f.<\/p>\n<pre><code>struct msghdr message; int ret= sock_sendmsg(sock, (struct msghdr *)&amp;message);<\/code><\/pre>\n<h3>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430<\/h3>\n<p>\u0421\u0438\u0433\u043d\u0430\u043b\u044b \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0415\u0441\u043b\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 (PID) \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u044f\u0434\u0440\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e\u0442 pid, \u043c\u043e\u0434\u0443\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 pid \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0435 \u0432 <code>send_sig_info()<\/code> \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0438\u0433\u043d\u0430\u043b\u0430.<\/p>\n<pre><code>struct pid *pid_struct = find_get_pid(pid); struct task_struct *task = pid_task(pid_struct,PIDTYPE_PID); int signum = SIGKILL, sig_ret; struct siginfo info; memset(&amp;info, '\\0', sizeof(struct siginfo)); info.si_signo = signum; \/\/send a SIGKILL to the daemon sig_ret = send_sig_info(signum, &amp;info, task); if (sig_ret &lt; 0) { printk(KERN_INFO \"error sending signal\\n\"); return -1; }<\/code><\/pre>\n<h3>\u0420\u043e\u0442\u0430\u0446\u0438\u044f \u043b\u043e\u0433\u043e\u0432\u00a0<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0445\u043e\u0447\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u043b\u043e\u0433\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430, \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0432 rsyslog (\/etc\/rsyslog.conf) \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p><code>:msg,startswith,\"[HelloModule]\" \/var\/log\/helloModule.log<\/code>\u00a0<\/p>\n<p>\u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 rsyslog \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u043b\u043e\u0433\u0438 \u044f\u0434\u0440\u0430, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0441 [Hello Module], \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \/var\/log\/helloModule.log file.\u00a0<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong>: \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0440\u043e\u0442\u0430\u0446\u0438\u0438 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \/etc\/logrotate.d.<\/p>\n<pre><code>\"\/var\/log\/helloModule.log\" { daily rotate 4 maxsize 2M create 0600 root postrotate     service rsyslog restart > \/dev\/null endscript }<\/code><\/pre>\n<p>\u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439 <strong>\u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e<\/strong> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u0444\u0430\u0439\u043b\u0430 \u043b\u043e\u0433\u043e\u0432 <strong>2<\/strong> \u041c\u0411, \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 <strong>4<\/strong> \u0440\u043e\u0442\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430. \u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043b\u043e\u0433\u043e\u0432 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 <strong>2<\/strong> \u041c\u0411, \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0441 \u0442\u0435\u043c \u0436\u0435 \u0438\u043c\u0435\u043d\u0435\u043c \u0438 \u043f\u0440\u0430\u0432\u0430\u043c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0444\u0430\u0439\u043b\u0443 <strong>0600<\/strong>, \u0430 \u043a \u0441\u0442\u0430\u0440\u043e\u043c\u0443 \u0444\u0430\u0439\u043b\u0443 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430 \u043e\u0442\u043c\u0435\u0442\u043a\u0430 \u0434\u0430\u0442\u044b \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0440\u043e\u0442\u0430\u0446\u0438\u0438 \u043e\u043d \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0441\u043b\u0443\u0436\u0431\u0443 rsyslog.<\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430<\/h3>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435\u0441\u044c \u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u043c\u0443 makefile, \u0447\u0442\u043e\u0431\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0441\u044d\u043c\u043f\u043b\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b:<\/p>\n<pre><code>obj\u2212m += helloWorld.o all: make \u2212C \/lib\/modules\/$(shell uname \u2212r)\/build M=$(PWD) modules clean: make \u2212C \/lib\/modules\/$(shell uname \u2212r)\/build M=$(PWD) clean<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/strong>: \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 RHEL. \u0414\u0440\u0443\u0433\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 makefile \u043c\u043e\u0433\u0443\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f.<\/p>\n<h3>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430 \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c\u00a0<\/h3>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b <code>ioctl<\/code> \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430. \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u043e\u043c \u043d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044d\u0442\u0438 \u0432\u044b\u0437\u043e\u0432\u044b <code>ioctl<\/code> \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u0439 \u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0439 \u0432 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0437\u0434\u043d\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h3>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0432\u0441\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u043d\u0435\u0435.<\/p>\n<pre><code># cat helloWorld.h  #ifndef HELLOWORLD_H #define HELLOWORLD_H #include &lt;linux\/ioctl.h>  \/\/ cmd \u2018KE_DATA_VAR\u2019 to send the integer type data #define KE_DATA_VAR _IOR('q', 1, int *)  #endif  # cat helloWorld.c  #include &lt;stdio.h> #include &lt;sys\/types.h> #include &lt;fcntl.h> #include &lt;unistd.h> #include &lt;string.h> #include &lt;sys\/ioctl.h> #include &lt;stdlib.h> #include \"helloWorld.h\"  \/* @brief: function to load the kernel module *\/ void load_KE() {     printf (\"loading KE\\n\");     if (system (\"insmod \/root\/helloWorld.ko\") == 0)     {         printf (\"KE loaded successfully\");     } }  \/* @brief: function to unload the kernel module *\/ void unload_KE() {     printf (\"unloading KE\\n\");     if (system (\"rmmod \/root\/helloWorld.ko\") == 0)     {         printf (\"KE unloaded successfully\");     } }  \/* @brief: method to send data to kernel module *\/ void send_data(int fd) {     int v;      printf(\"Enter value: \");     scanf(\"%d\", &amp;v);     getchar();     if (ioctl(fd, KE_DATA_VAR, &amp;v) == -1)     {         perror(\"send data error at ioctl\");     } }  int main(int argc, char *argv[]) {     const char *file_name = \"\/dev\/char_device\"; \/\/used by ioctl     int fd;     enum     {         e_load, \/\/load the kernel module         e_unload, \/\/unload the kernel module         e_send, \/\/send a HB from test binary to kernel module     } option;      if (argc == 2)     {         if (strcmp(argv[1], \"-l\") == 0)         {             option = e_load;         }         else if (strcmp(argv[1], \"-u\") == 0)         {             option = e_unload;         }                 }         else if (strcmp(argv[1], \"-s\") == 0)         {             option = e_send;         }         else         {             fprintf(stderr, \"Usage: %s [-l | -u | -s ]\\n\", argv[0]);             return 1;         }     }     else     {         fprintf(stderr, \"Usage: %s [-l | -u | -s ]\\n\", argv[0]);         return 1;     }      if ((option != e_load) &amp;&amp; (option != e_unload))     {         fd = open(file_name, O_RDWR);         if (fd == -1)         {             perror(\"KE ioctl file open\");             return 2;         }     }     switch (option)     {         case e_load:             load_KE();             break;         case e_unload:             unload_KE();             break;         case e_send:             send_data(fd);             break;         default:             break;     }      if ((option != e_load) &amp;&amp; (option != e_unload))     {         close (fd);     } return 0; }  Sample kernel module # cat helloWorld.c #include &lt;linux\/slab.h> #include &lt;linux\/kthread.h> #include &lt;linux\/module.h> #include &lt;linux\/kernel.h> #include &lt;linux\/version.h> #include &lt;linux\/fs.h> #include &lt;linux\/cdev.h> #include &lt;linux\/device.h> #include &lt;linux\/errno.h> #include &lt;asm\/uaccess.h> #include &lt;linux\/time.h> #include &lt;linux\/mutex.h> #include &lt;linux\/socket.h> #include &lt;linux\/ioctl.h> #include &lt;linux\/notifier.h> #include &lt;linux\/reboot.h> #include &lt;linux\/sched.h> #include &lt;linux\/pid.h> #include &lt;linux\/kmod.h> #include &lt;linux\/if.h> #include &lt;linux\/net.h> #include &lt;linux\/if_ether.h> #include &lt;linux\/if_packet.h> #include &lt;linux\/unistd.h> #include &lt;linux\/types.h> #include &lt;linux\/time.h> #include &lt;linux\/delay.h>  typedef struct {     char ethInfName[8];     char srcMacAdr[15];     char destMacAdr[15];     int ifindex; }KEConfig_t;  MODULE_LICENSE(\"GPL\"); MODULE_AUTHOR(\"Owner Name\"); MODULE_DESCRIPTION(\"Sample Hello world\"); MODULE_VERSION(\"0.1\");  static char *name = \"world\"; static struct task_struct *ke_thread; static struct KEConfig_t KECfg; module_param(name, charp, S_IRUGO); MODULE_PARM_DESC(name, \"The name to display in \/var\/log\/kern.log\");   \/* @brief: create socket and send required data to HM  * creates the socket and binds on to it.  * This method will also send event notification  * to HM.  * *\/ static int createSocketandSendData(char *data) {     int ret_l =0;     mm_segment_t oldfs;     struct msghdr message;     struct iovec ioVector;      int result;     struct ifreq ifr;     struct socket *sock;     struct sockaddr_ll *s1 = kmalloc(sizeof(struct sockaddr_ll),GFP_KERNEL);     if (!s1)     {        printk(KERN_INFO \"failed to allocate memory\");        return -1;     }     printk(KERN_INFO \"inside configureSocket\");     memset(s1, '\\0', sizeof(struct sockaddr_ll));     memset(\ud526, '\\0', sizeof(ifr));      result = sock_create(PF_PACKET, SOCK_RAW, htons(ETH_P_IP), &amp;sock);     if(result &lt; 0)     {         printk(KERN_INFO \"unable to create socket\");         return -1;     }     printk(KERN_INFO \"interface: %s\", KECfg.ethInfName);     printk(KERN_INFO \"ifr index: %d\", KECfg.ifindex);     strcpy((char *)ifr.ifr_name, KECfg.ethInfName);      s1->sll_family = AF_PACKET;     s1->sll_ifindex = KECfg.ifindex;     s1->sll_halen = ETH_ALEN;     s1->sll_protocol = htons(ETH_P_IP); result = sock->ops->bind(sock, (struct sockaddr *)s1, sizeof(struct sockaddr_ll));     if(result &lt; 0)     {         printk(KERN_INFO \"Unable to bind socket\");         return -1;     }      \/\/create the message header     memset(&amp;message, 0, sizeof(message));     message.msg_name = sockData->sock_ll;     message.msg_namelen = sizeof(*(sock_ll));      ioVector.iov_base = data;     ioVector.iov_len  = sizeof(data);     message.msg_iov = &amp;ioVector;     message.msg_iovlen = 1;     message.msg_control = NULL;     message.msg_controllen = 0;     oldfs = get_fs();     set_fs(KERNEL_DS);     ret_l = sock_sendmsg(sockData->sock, &amp;message, sizeof(data));       return 0; }  static long ke_ioctl(struct file *f, unsigned int cmd, unsigned long arg) {     int b;      switch (cmd)     {         case KE_DATA_VAR:         if (get_user(b, (int *)arg))         {         return -EACCES;         }         \/\/set the time of HB here         mutex_lock(&amp;dataLock);         do_gettimeofday(&amp;hbTv);         printk(KERN_INFO \"time of day is %ld:%lu \\n\", hbTv.tv_sec, hbTv.tv_usec);         printk(KERN_INFO \"data %d\\n\", b);         \/\/send data out         createSocketandSendData(&amp;b);         mutex_unlock(&amp;dataLock);         break;         default:             return -EINVAL;     }      return 0; }  \/* @brief: method to register the ioctl call *\/ static struct file_operations ke_fops = {     .owner = THIS_MODULE, #if (LINUX_VERSION_CODE &lt; KERNEL_VERSION(2,6,35))     .ioctl = ke_ioctl #else     .unlocked_ioctl = ke_ioctl #endif };  \/* @brief The thread function *\/ int ke_init() {     printk(KERN_INFO \"Inside function\");     return 0; }  \/* @brief The LKM initialization function *\/ static int __init module_init(void) {    printk(KERN_INFO \"module_init initialized\\n\");    if ((ret = alloc_chrdev_region(&amp;dev, FIRST_MINOR, MINOR_CNT, \"KE_ioctl\")) &lt; 0)    {        return ret;    }     cdev_init(&amp;c_dev, &amp;ke_fops);     if ((ret = cdev_add(&amp;c_dev, dev, MINOR_CNT)) &lt; 0)    {        return ret;    }     if (IS_ERR(cl = class_create(THIS_MODULE, \"char\")))    {        cdev_del(&amp;c_dev);        unregister_chrdev_region(dev, MINOR_CNT);        return PTR_ERR(cl);    }    if (IS_ERR(dev_ret = device_create(cl, NULL, dev, NULL, \"KEDevice\")))    {        class_destroy(cl);        cdev_del(&amp;c_dev);        unregister_chrdev_region(dev, MINOR_CNT);        return PTR_ERR(dev_ret);    }     \/\/create related threads    mutex_init(&amp;dataLock); \/\/initialize the lock    KEThread = kthread_run(ke_init,\"KE thread\",\"KEThread\");    return 0; }  void thread_cleanup(void) {     int ret = 0;      if (ke_thread)     ret = kthread_stop(ke_thread);     if (!ret)         printk(KERN_INFO \"Kernel thread stopped\"); }  \/* @brief The LKM cleanup function *\/ static void __exit module_exit(void) {    device_destroy(cl, dev);    class_destroy(cl);    cdev_del(&amp;c_dev);    unregister_chrdev_region(dev, MINOR_CNT);     thread_cleanup();    printk(KERN_INFO \"Exit %s from the Hello world!\\n\", name); }  module_init(module_init); module_exit(module_exit);<\/code><\/pre>\n<p>\u041c\u043e\u0434\u0443\u043b\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u0441<strong> <\/strong><code>init_module<\/code>, \u043b\u0438\u0431\u043e \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 <code>module_init()<\/code><strong>. <\/strong>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u044f\u0434\u0440\u0443, \u043a\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043e\u0434\u0443\u043b\u044c, \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442 \u044f\u0434\u0440\u043e \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043c\u043e\u0434\u0443\u043b\u044f, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b.<\/p>\n<p>\u0412\u0441\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043b\u0438\u0431\u043e<strong> <\/strong><code>cleanup_module()<\/code><strong>,<\/strong> \u043b\u0438\u0431\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 <code>module_exit()<\/code><strong>.<\/strong> \u042d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0445\u043e\u0434\u0430 \u0434\u043b\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u2014 \u043e\u043d\u0430 \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u0442 \u0432\u0441\u0451, \u0447\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u0432\u043e\u0434\u0430.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/strong>: \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0444\u0430\u0439\u043b \u043e\u0442\u043a\u0440\u044b\u0442 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438<strong> <\/strong><code>open()<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u0432\u044f\u0437\u0438 \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430.\u00a0<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0432\u044b\u0437\u043e\u0432 <code>execve()<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c, \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0441\u043e\u043a\u0435\u0442\u0430 <code>FD_CLOEXEC<\/code> \u0432 <code>fd<\/code> (\u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440).<\/p>\n<pre><code>fd = open(\u201c\/dev\/char_device\u201d, O_RDWR); fcntl(fd, F_SETFD, FD_CLOEXEC);<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>FD_CLOEXEC<\/code> \u043d\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e <code>fd<\/code>, \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440 \u0444\u0430\u0439\u043b\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>execve()<\/code><strong>.<\/strong>\u00a0<\/p>\n<h2>\u041a\u043e\u0440\u043e\u0442\u043a\u043e \u043e \u0433\u043b\u0430\u0432\u043d\u043e\u043c<\/h2>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u0438\u0445 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0431\u043e\u044f \u0438\u043b\u0438 \u0437\u0430\u0432\u0438\u0441\u0430\u043d\u0438\u044f. \u0410 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0440\u043e\u0442\u0430\u0446\u0438\u0438 \u043b\u043e\u0433\u043e\u0432 \u044f\u0434\u0440\u0430 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u0438 \u0441\u043e\u043a\u0435\u0442\u0430 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/e14\/ab8\/44c\/e14ab844ccc43edb454cc788fd74cfa8.jpg\" alt=\"\" title=\"\" width=\"780\" height=\"100\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e14\/ab8\/44c\/e14ab844ccc43edb454cc788fd74cfa8.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p><a href=\"https:\/\/slurm.club\/3R0i8ZI\">\u00ab\u0410\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u00a0Linux.\u00a0\u041c\u0435\u0433\u0430\u00bb<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/southbridge\/blog\/712414\/\"> https:\/\/habr.com\/ru\/company\/southbridge\/blog\/712414\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e? \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b \u043f\u043e\u043c\u043e\u0433\u0430\u043b\u0438 \u0432\u044b\u044f\u0432\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 Linux, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c, \u043a\u0430\u043a \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u044c \u0441 \u0441\u043e\u043a\u0435\u0442\u043e\u043c.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0438 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>Application<\/strong>: \u043b\u044e\u0431\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430.<\/p>\n<p><strong>Kernel Module<\/strong>: \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f API-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u044f\u0434\u0440\u0430 \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><a href=\"https:\/\/slurm.club\/3R0i8ZI\">\u00ab\u0410\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u00a0Linux.\u00a0\u041c\u0435\u0433\u0430\u00bb<\/a><\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430<\/h3>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u044f\u0434\u0440\u0430.<\/p>\n<h4>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430<\/h4>\n<p><strong>insmod<\/strong>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043c\u043e\u0434\u0443\u043b\u044f \u0432 \u044f\u0434\u0440\u043e.<\/p>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: insmod \u2018kernel_ext_binary\u2019<\/p>\n<pre><code># insmod helloWorld.ko Welcome to Hello world Module.<\/code><\/pre>\n<h4>\u0412\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430<\/h4>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: rmmod \u2018kernel_ext_binary\u2019<\/p>\n<pre><code># rmmod helloWorld.ko Goodbye, from Hello world.<\/code><\/pre>\n<h4>\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u044f\u0434\u0440\u0430<\/h4>\n<p><strong>lsmod<\/strong>: \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u044f\u0434\u0440\u0430.\u00a0<\/p>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: lsmod | grep \u2018kernel_ext_binary\u2019<\/p>\n<pre><code># lsmod | grep hello helloWorld 12189  1<\/code><\/pre>\n<h3>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043c\u043e\u0434\u0443\u043b\u0435 \u044f\u0434\u0440\u0430<\/h3>\n<p><strong>modinfo<\/strong>: \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043c\u043e\u0434\u0443\u043b\u0435.\u00a0<\/p>\n<p><strong>\u043f\u0440\u0438\u043c\u0435\u0440<\/strong>: modinfo hello*.ko<\/p>\n<pre><code># modinfo helloWorld.ko filename:       \/root\/helloWorld.ko description:    Basic Hello World KE author:         helloWorld license:        GPL rhelversion:    7.3 srcversion:     5F60F86F84D8477986C3A50 depends: vermagic:       3.10.0-514.el7.ppc64le SMP mod_unload modversions<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0438 \u0447\u0435\u0440\u0435\u0437 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 <code>system()<\/code>.<\/p>\n<h3>\u0421\u0432\u044f\u0437\u044c \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c<\/h3>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0444\u0430\u0439\u043b \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e open() API. \u042d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0438 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u0412\u0441\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435.<\/p>\n<pre><code>#include &lt;sys\/types.h> #include &lt;sys\/stat.h> #include &lt;fcntl.h>  int open(const char *pathname, int flags);<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code>int fd; #define DEVICE_FILE_NAME \"\/dev\/char_dev\" fd = open(DEVICE_FILE_NAME, 0);<\/code><\/pre>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<strong> <\/strong><code>open()<\/code> \u2014 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440 \u0444\u0430\u0439\u043b\u0430, \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445 (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>ioctl<\/code>).<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 ioctl<\/h3>\n<p>\u0421\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 <code>ioctl()<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<pre><code>#include &lt;sys\/ioctl.h>  int ioctl(int fd, int cmd, ...);<\/code><\/pre>\n<p><code>fd<\/code> \u2014 \u044d\u0442\u043e \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0439 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0438\u0437 <code>open()<\/code>, \u0430 <code>cmd<\/code> \u2014 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432 <code>ioctl()<\/code> \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code>#define IOCTL_SEND_MSG _IOR(MAJOR_NUM, 0, char *) int ret_val; char message[100]; ret_val = ioctl(file_desc, IOCTL_SEND_MSG, message); if (ret_val &lt; 0) { printf(\"ioctl_send_msg failed:%d\\n\", ret_val); exit(\u22121); }<\/code><\/pre>\n<p>\u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <code>IOCTL_SEND_MSG<\/code> \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044e.<\/p>\n<p><code>_IOR<\/code> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043d\u043e\u043c\u0435\u0440 \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>ioctl<\/code> \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430.\u00a0<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, <code>MAJOR_NUM<\/code>, \u2014 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u043d\u0430\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u043d\u043e\u043c\u0435\u0440 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (\u0438\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441 \u0440\u0430\u0437\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c).<\/p>\n<p>\u0422\u0440\u0435\u0442\u0438\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043a \u044f\u0434\u0440\u0443.<\/p>\n<p>\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 \u044f\u0434\u0440\u0430 \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 <code>ioctl<\/code>.<\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 \u044f\u0434\u0440\u0430<\/h3>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u0434\u0435\u043b\u0430\u0445 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u044f\u0434\u0440\u0430.\u00a0<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430<\/h4>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u044b:<\/p>\n<pre><code>#include &lt;linux\/kthread.h> static struct task_struct * sampleThread = NULL; sampleThread = kthread_run(threadfn, data, namefmt, \u2026)<\/code><\/pre>\n<p><code>kthread_run()<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u0435\u043c\u0443 \u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0435.<\/p>\n<p><code>threadfn<\/code> \u2014 \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430.\u00a0<\/p>\n<p><code>data<\/code> * \u2014 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p><code>namefmt<\/code> \u2014 \u0438\u043c\u044f \u043f\u043e\u0442\u043e\u043a\u0430 (\u0432 \u0432\u044b\u0432\u043e\u0434\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>ps<\/code>)<\/p>\n<h4>\u041e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u0430<\/h4>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0432\u044b\u0437\u043e\u0432:<\/p>\n<p><code>kthread_stop(sampleThread)<\/code><\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0441\u043e\u043a\u0435\u0442\u043e\u043c<\/h3>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0439 \u0441\u043e\u043a\u0435\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>sock_create()<\/code>. \u0427\u0435\u0440\u0435\u0437 \u044d\u0442\u043e\u0442 \u0441\u043e\u043a\u0435\u0442 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0438\u043b\u0438 \u0432\u043d\u0435 \u0445\u043e\u0441\u0442\u0430.<\/p>\n<pre><code>struct socket *sock; struct sockaddr_ll *s1 = kmalloc(sizeof(struct sockaddr_ll),GFP_KERNEL); result = sock_create(PF_PACKET, SOCK_RAW, htons(ETH_P_IP), &amp;sock); if(result &lt; 0) { printk(KERN_INFO \"[vmmKE] unable to create socket\");     return -1; }  \/\/copy the interface name to ifr.name  and other required information. strcpy((char *)ifr.ifr_name, InfName); s1->sll_family = AF_PACKET; s1->sll_ifindex = ifindex; s1->sll_halen = ETH_ALEN; s1->sll_protocol = htons(ETH_P_IP);  result = sock->ops->bind(sock, (struct sockaddr *)s1, sizeof(struct sockaddr_ll)); if(result &lt; 0) { printk(KERN_INFO \"[vmmKE] unable to bind socket\");     return -1; }<\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>sock_sendmsg()<\/code> \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f.<\/p>\n<pre><code>struct msghdr message; int ret= sock_sendmsg(sock, (struct msghdr *)&amp;message);<\/code><\/pre>\n<h3>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430<\/h3>\n<p>\u0421\u0438\u0433\u043d\u0430\u043b\u044b \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0415\u0441\u043b\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 (PID) \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u044f\u0434\u0440\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e\u0442 pid, \u043c\u043e\u0434\u0443\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 pid \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0435 \u0432 <code>send_sig_info()<\/code> \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0438\u0433\u043d\u0430\u043b\u0430.<\/p>\n<pre><code>struct pid *pid_struct = find_get_pid(pid); struct task_struct *task = pid_task(pid_struct,PIDTYPE_PID); int signum = SIGKILL, sig_ret; struct siginfo info; memset(&amp;info, '\\0', sizeof(struct siginfo)); info.si_signo = signum; \/\/send a SIGKILL to the daemon sig_ret = send_sig_info(signum, &amp;info, task); if (sig_ret &lt; 0) { printk(KERN_INFO \"error sending signal\\n\"); return -1; }<\/code><\/pre>\n<h3>\u0420\u043e\u0442\u0430\u0446\u0438\u044f \u043b\u043e\u0433\u043e\u0432\u00a0<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0445\u043e\u0447\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u043b\u043e\u0433\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u044f\u0434\u0440\u0430, \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0432 rsyslog (\/etc\/rsyslog.conf) \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p><code>:msg,startswith,\"[HelloModule]\" \/var\/log\/helloModule.log<\/code>\u00a0<\/p>\n<p>\u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 rsyslog \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u043b\u043e\u0433\u0438 \u044f\u0434\u0440\u0430, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0441 [Hello Module], \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \/var\/log\/helloModule.log file.\u00a0<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong>: \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0440\u043e\u0442\u0430\u0446\u0438\u0438 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \/etc\/logrotate.d.<\/p>\n<pre><code>\"\/var\/log\/helloModule.log\" { daily rotate 4 maxsize 2M create 0600 root postrotate     service rsyslog restart > \/dev\/null endscript }<\/code><\/pre>\n<p>\u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439 <strong>\u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e<\/strong> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u0444\u0430\u0439\u043b\u0430 \u043b\u043e\u0433\u043e\u0432 <strong>2<\/strong> \u041c\u0411, \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 <strong>4<\/strong> \u0440\u043e\u0442\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430. \u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043b\u043e\u0433\u043e\u0432 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 <strong>2<\/strong> \u041c\u0411, \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0441 \u0442\u0435\u043c \u0436\u0435 \u0438\u043c\u0435\u043d\u0435\u043c \u0438 \u043f\u0440\u0430\u0432\u0430\u043c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0444\u0430\u0439\u043b\u0443 <strong>0600<\/strong>, \u0430 \u043a \u0441\u0442\u0430\u0440\u043e\u043c\u0443 \u0444\u0430\u0439\u043b\u0443 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430 \u043e\u0442\u043c\u0435\u0442\u043a\u0430 \u0434\u0430\u0442\u044b \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0440\u043e\u0442\u0430\u0446\u0438\u0438 \u043e\u043d \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0441\u043b\u0443\u0436\u0431\u0443 rsyslog.<\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430<\/h3>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435\u0441\u044c \u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u043c\u0443 makefile, \u0447\u0442\u043e\u0431\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0441\u044d\u043c\u043f\u043b\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b:<\/p>\n<pre><code>obj\u2212m += helloWorld.o all: make \u2212C \/lib\/modules\/$(shell uname \u2212r)\/build M=$(PWD) modules clean: make \u2212C \/lib\/modules\/$(shell uname \u2212r)\/build M=$(PWD) clean<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/strong>: \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 RHEL. \u0414\u0440\u0443\u0433\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 makefile \u043c\u043e\u0433\u0443\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f.<\/p>\n<h3>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u044f \u044f\u0434\u0440\u0430 \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c\u00a0<\/h3>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b <code>ioctl<\/code> \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u044f\u0434\u0440\u0430. \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u043e\u043c \u043d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044d\u0442\u0438 \u0432\u044b\u0437\u043e\u0432\u044b <code>ioctl<\/code> \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u0439 \u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0439 \u0432 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0437\u0434\u043d\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h3>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0432\u0441\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u043d\u0435\u0435.<\/p>\n<pre><code># cat helloWorld.h  #ifndef HELLOWORLD_H #define HELLOWORLD_H #include &lt;linux\/ioctl.h>  \/\/ cmd \u2018KE_DATA_VAR\u2019 to send the integer type data #define KE_DATA_VAR _IOR('q', 1, int *)  #endif  # cat helloWorld.c  #include &lt;stdio.h> #include &lt;sys\/types.h> #include &lt;fcntl.h> #include &lt;unistd.h> #include &lt;string.h> #include &lt;sys\/ioctl.h> #include &lt;stdlib.h> #include \"helloWorld.h\"  \/* @brief: function to load the kernel module *\/ void load_KE() {     printf (\"loading KE\\n\");     if (system (\"insmod \/root\/helloWorld.ko\") == 0)     {         printf (\"KE loaded successfully\");     } }  \/* @brief: function to unload the kernel module *\/ void unload_KE() {     printf (\"unloading KE\\n\");     if (system (\"rmmod \/root\/helloWorld.ko\") == 0)     {         printf (\"KE unloaded successfully\");     } }  \/* @brief: method to send data to kernel module *\/ void send_data(int fd) {     int v;      printf(\"Enter value: \");     scanf(\"%d\", &amp;v);     getchar();     if (ioctl(fd, KE_DATA_VAR, &amp;v) == -1)     {         perror(\"send data error at ioctl\");     } }  int main(int argc, char *argv[]) {     const char *file_name = \"\/dev\/char_device\"; \/\/used by ioctl     int fd;     enum     {         e_load, \/\/load the kernel module         e_unload, \/\/unload the kernel module         e_send, \/\/send a HB from test binary to kernel module     } option;      if (argc == 2)     {         if (strcmp(argv[1], \"-l\") == 0)         {             option = e_load;         }         else if (strcmp(argv[1], \"-u\") == 0)         {             option = e_unload;         }                 }         else if (strcmp(argv[1], \"-s\") == 0)         {             option = e_send;         }         else         {             fprintf(stderr, \"Usage: %s [-l | -u | -s ]\\n\", argv[0]);             return 1;         }     }     else     {         fprintf(stderr, \"Usage: %s [-l | -u | -s ]\\n\", argv[0]);         return 1;     }      if ((option != e_load) &amp;&amp; (option != e_unload))     {         fd = open(file_name, O_RDWR);         if (fd == -1)         {             perror(\"KE ioctl file open\");             return 2;         }     }     switch (option)     {         case e_load:             load_KE();             break;         case e_unload:             unload_KE();             break;         case e_send:             send_data(fd);             break;         default:             break;     }      if ((option != e_load) &amp;&amp; (option<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-344379","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/344379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344379"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/344379\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}