├── BSD_3-Clause_License.txt ├── LICENSE ├── Makefile ├── README.md └── fanout.c /BSD_3-Clause_License.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010-2015, Bob Smith 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or 5 | without modification, are permitted provided that the 6 | following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above 9 | copyright notice, this list of conditions and the following 10 | disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above 13 | copyright notice, this list of conditions and the following 14 | disclaimer in the documentation and/or other materials 15 | provided with the distribution. 16 | 17 | 3. Neither the name of the copyright holder nor the names of 18 | its contributors may be used to endorse or promote products 19 | derived from this software without specific prior written 20 | permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 23 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,BUT 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 33 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | obj-m += fanout.o 3 | else 4 | KERNELDIR ?= /lib/modules/$(shell uname -r)/build 5 | PWD := $(shell pwd) 6 | 7 | all: 8 | make -C $(KERNELDIR) M=$(PWD) modules 9 | 10 | clean: 11 | make -C $(KERNELDIR) M=$(PWD) clean 12 | 13 | install: 14 | make -C $(KERNELDIR) M=$(PWD) modules_install 15 | endif 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## /dev/fanout 2 | Kernel-based publish-subscribe system 3 | 4 | ## WHAT: 5 | - simple, efficient, broadcast IPC, or alternatively 6 | - simple, efficient, publish-subscribe IPC 7 | - kernel module implementing a character device 8 | 9 | 10 | ## WHY: 11 | - Efficient: Only N writes for N subscribers compared to 2*N 12 | writes for a sockets based system 13 | - Simple: Kernel based, no need for a new process or daemon 14 | - Simple: Use mknod to create a channel or topic as a device node 15 | - Simple: API is just open()/read()/write()/close() 16 | - Simple: Works with select() for event driven programming 17 | - Simple: Works with ALL programming languages, even Bash 18 | - Simple: No dependencies and no libraries to install 19 | - Simple: Builds on all Linux systems 20 | - Secure: Access rights tied to filesystem permissions on device node 21 | 22 | 23 | ## INSTALLATION: 24 | // comment out #define DEV_MKNOD if on Ubuntu 20.04. 25 | sudo apt-get install linux-headers-`uname -r` 26 | git clone --depth=1 https://github.com/bob-linuxtoys/fanout 27 | cd fanout 28 | make 29 | sudo make install 30 | 31 | 32 | ## TEST: 33 | sudo modprobe fanout # load the kernel module, create 8 nodes 34 | # rename one of the nodes for our use of it 35 | # (or create /dev nodes manually if Ubuntu 20.04) 36 | sudo mv /dev/fanout7 /dev/fanouttest 37 | # Add three subscribers to the topic on /dev/fanouttest 38 | cat /dev/fanouttest & 39 | cat /dev/fanouttest & 40 | cat /dev/fanouttest & 41 | # Publish a messagte to fanouttest topic 42 | echo Hello, World > /dev/fanouttest 43 | 44 | 45 | ## NOTES: 46 | See http://linustoys.org for an article on fanout. 47 | See Linux Journal of August, 2010 for another article 48 | See also http://github.org/bob-linuxtoys/proxy 49 | 50 | 51 | 52 | ## COPYRIGHT 53 | Copyright 2010-2021, Bob Smith, Frederic Roussel 54 | 55 | 56 | 57 | ## LICENSE: 58 | At your discretion, you may consider this software covered 59 | by either the GNU Public License, Version 2, or the BSD 60 | 3-Clause license. 61 | http://opensource.org/licenses/GPL-2.0 62 | http://opensource.org/licenses/BSD-3-Clause 63 | 64 | -------------------------------------------------------------------------------- /fanout.c: -------------------------------------------------------------------------------- 1 | /* 2 | * fanout.c: A one-to-many multiplexer 3 | * 4 | * Copyright (C) 2010-2021, Bob Smith, Frederic Roussel 5 | * This software is released under your choice of either 6 | * the GPLv2 or the 3-clause BSD license. 7 | * 8 | * Initial release: Bob Smith 9 | * changes, added more locking: Edwin van den Oetelaar (www.oetelaar.com) 10 | * added automatic mknod: Frederic Roussel (fr.frasc@gmail.com) 11 | */ 12 | 13 | /* Comment out to forgo the creation of /dev entries 14 | * The companion udev rules 'fanout.rules' sets the special file mode 15 | */ 16 | #define DEV_MKNOD 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #ifdef DEV_MKNOD 27 | # include 28 | # include 29 | #endif /* DEV_MKNOD */ 30 | 31 | 32 | /* Limits and other defines */ 33 | /* The # fanout devices. Max minor # is one less than this */ 34 | #ifdef DEV_MKNOD 35 | # define NUM_FO_DEVS (8) 36 | #else 37 | # define NUM_FO_DEVS (255) 38 | #endif /* DEV_MKNOD */ 39 | #define DEVNAME "fanout" 40 | #define DEBUGLEVEL (2) 41 | 42 | 43 | /* Data structure definitions */ 44 | /* This data structure describes one fanout device. There 45 | * is one of these for each instance (minor #) of fanout */ 46 | struct fo { 47 | int minor; /* minor number of this fanout instance */ 48 | char *buf; /* points to circular buffer, first char */ 49 | int indx; /* where to put next char received */ 50 | loff_t count; /* number chars received */ 51 | wait_queue_head_t inq; /* readers wait on this queue */ 52 | struct semaphore sem; /* lock to keep buf/indx sane */ 53 | #ifdef DEV_MKNOD 54 | struct device *dev; /* automatic mknod */ 55 | #endif /* DEV_MKNOD */ 56 | }; 57 | 58 | 59 | /* Function prototypes. */ 60 | int fanout_init_module(void); 61 | void fanout_exit_module(void); 62 | static int fanout_open(struct inode *, struct file *); 63 | static int fanout_release(struct inode *, struct file *); 64 | static ssize_t fanout_read(struct file *, char *, size_t, loff_t *); 65 | static ssize_t fanout_write(struct file *, const char *, size_t, loff_t *); 66 | static unsigned int fanout_poll(struct file *, poll_table *); 67 | 68 | 69 | /* Global variables */ 70 | static int buffersize = 0x4000; /* Circular buffer size 0x4000 (16K) */ 71 | static unsigned int numberofdevs = NUM_FO_DEVS; 72 | static int fo_major = 0; /* major device number */ 73 | /* debuglevel controls whether a printk is executed 74 | * 0 = no printk at all 75 | * 1 = printk on error only 76 | * 2 = printk on errors and on init/remove 77 | * 3 = debug printk to trace calls into fanout 78 | * 4 = debug trace inside of fanout calls 79 | */ 80 | static unsigned int debuglevel = DEBUGLEVEL; /* printk verbosity */ 81 | 82 | struct cdev fo_cdev; /* a char device global just 1 */ 83 | dev_t fo_devicenumber; /* first device number */ 84 | 85 | #ifdef DEV_MKNOD 86 | static struct class *fo_class; /* fanout class */ 87 | static mode_t nodemode = 0666; /* special files permissions bits */ 88 | /* PARAM_DESC uses that value */ 89 | /* forward declaration */ 90 | static char *fo_dev_devnode(struct device *dev, umode_t *mode); 91 | #endif /* DEV_MKNOD */ 92 | 93 | module_param(buffersize, int, S_IRUSR); 94 | module_param(debuglevel, int, S_IRUSR); 95 | module_param(numberofdevs, int, S_IRUSR); 96 | #ifdef DEV_MKNOD 97 | module_param(nodemode, int, S_IRUSR); 98 | #endif /* DEV_MKNOD */ 99 | 100 | static struct fo *fo_devs; /* point to devices (minors) */ 101 | 102 | 103 | /* map the callbacks into this driver */ 104 | static struct file_operations fanout_fops = { 105 | .owner = THIS_MODULE, 106 | .read = fanout_read, 107 | .open = fanout_open, 108 | .write = fanout_write, 109 | .poll = fanout_poll, 110 | .release = fanout_release 111 | }; 112 | 113 | 114 | /* Module description and macros */ 115 | MODULE_DESCRIPTION 116 | ("A device to replicate input (writer) on all outputs (readers), readers block, writer never blocks"); 117 | MODULE_AUTHOR("Bob Smith"); 118 | MODULE_LICENSE("GPL"); 119 | MODULE_PARM_DESC(buffersize, "Size of each buffer. default=16384 (16K) "); 120 | MODULE_PARM_DESC(debuglevel, "Debug level. Higher=verbose. default=2"); 121 | MODULE_PARM_DESC(numberofdevs, 122 | "Create this many minor devices. default=16"); 123 | #ifdef DEV_MKNOD 124 | MODULE_PARM_DESC(nodemode, "Special files permission bits. default=0666"); 125 | #endif /* DEV_MKNOD */ 126 | 127 | 128 | int fanout_init_module(void) 129 | { 130 | int i, err; 131 | fo_devs = kmalloc(numberofdevs * sizeof(struct fo), GFP_KERNEL); 132 | if (fo_devs == NULL) { 133 | if (debuglevel >= 1) 134 | printk(KERN_ALERT "%s: init fails. no memory.\n", 135 | DEVNAME); 136 | return 0; 137 | } 138 | /* clean memory and init device structures */ 139 | memset(fo_devs, 0, numberofdevs * sizeof(struct fo)); 140 | for (i = 0; i < numberofdevs; i++) { /* for every minor device */ 141 | fo_devs[i].minor = i; /* set number */ 142 | fo_devs[i].buf = (char *) 0; /* init buf */ 143 | fo_devs[i].indx = 0; /* init index */ 144 | fo_devs[i].count = 0; /* init count */ 145 | init_waitqueue_head(&fo_devs[i].inq); 146 | #ifdef init_MUTEX 147 | init_MUTEX(&fo_devs[i].sem); /* init sema */ 148 | #else 149 | sema_init(&fo_devs[i].sem,1); /* init sema */ 150 | #endif 151 | 152 | } 153 | 154 | err = alloc_chrdev_region(&fo_devicenumber, 0, numberofdevs, DEVNAME); 155 | if (err < 0) { 156 | if (debuglevel >= 1) 157 | printk(KERN_ALERT "%s: init fails. err=%d.\n", 158 | DEVNAME, err); 159 | return err; 160 | } 161 | fo_major = MAJOR(fo_devicenumber); /* save assign major */ 162 | cdev_init(&fo_cdev, &fanout_fops); /* init dev structures */ 163 | kobject_set_name(&(fo_cdev.kobj), "%s%d", DEVNAME, fo_devicenumber); 164 | 165 | #ifdef DEV_MKNOD 166 | fo_class = class_create(THIS_MODULE, DEVNAME); 167 | if (IS_ERR(fo_class)) { 168 | if (debuglevel >= 1) 169 | printk(KERN_ALERT "%s: class_create fails.\n", DEVNAME); 170 | cdev_del(&fo_cdev); /* delete major device */ 171 | kfree(fo_devs); /* free */ 172 | fo_devs = NULL; /* reset pointer */ 173 | return PTR_ERR(fo_class); 174 | } 175 | 176 | /* limit permission bits */ 177 | nodemode &= 0666; 178 | /* callback to set permission bits for the nodes we will create */ 179 | fo_class->devnode = fo_dev_devnode; 180 | #endif /* DEV_MKNOD */ 181 | 182 | err = cdev_add(&fo_cdev, fo_devicenumber, numberofdevs); 183 | if (err < 0) { 184 | if (debuglevel >= 1) 185 | printk(KERN_ALERT "%s: init fails. err=%d.\n", 186 | DEVNAME, err); 187 | return err; 188 | } 189 | 190 | #ifdef DEV_MKNOD 191 | /* Create the special files and register with sysfs */ 192 | for (i = 0; i < numberofdevs; i++) { /* for every minor device */ 193 | fo_devs[i].dev = device_create(fo_class, NULL, 194 | MKDEV(fo_major, i), NULL, i == 0 ? "%s" : "%s%d", 195 | DEVNAME, i); 196 | if (IS_ERR(fo_devs[i].dev)) { 197 | if (debuglevel >= 1) 198 | printk(KERN_ALERT \ 199 | "%s%d: device_create fails. err=%ld.\n", 200 | DEVNAME, i, PTR_ERR(fo_devs[i].dev)); 201 | } 202 | 203 | } 204 | #endif /* DEV_MKNOD */ 205 | 206 | if (debuglevel >= 2) { 207 | printk(KERN_INFO 208 | "%s: Installed %d minor devices on major number %d.\n", 209 | DEVNAME, numberofdevs, fo_major); 210 | } 211 | return 0; /* success */ 212 | } 213 | 214 | 215 | void fanout_exit_module(void) 216 | { 217 | int i; 218 | 219 | if (!fo_devs) /* anything to release ? */ 220 | return; 221 | 222 | for (i = 0; i < numberofdevs; i++) { /* for every minor */ 223 | 224 | #ifdef DEV_MKNOD 225 | /* Delete the special file for this minor */ 226 | device_destroy(fo_class, MKDEV(fo_major, i)); 227 | #endif /* DEV_MKNOD */ 228 | 229 | if (fo_devs[i].buf) 230 | kfree(fo_devs[i].buf); /* free alloced memory */ 231 | } 232 | 233 | cdev_del(&fo_cdev); /* delete major device */ 234 | kfree(fo_devs); /* free */ 235 | fo_devs = NULL; /* reset pointer */ 236 | 237 | #ifdef DEV_MKNOD 238 | class_destroy(fo_class); 239 | #endif /* DEV_MKNOD */ 240 | 241 | unregister_chrdev_region(fo_devicenumber, numberofdevs); 242 | 243 | if (debuglevel >= 2) 244 | printk(KERN_INFO "%s: Uninstalled.\n", DEVNAME); 245 | } 246 | 247 | 248 | static int fanout_open(struct inode *inode, struct file *filp) 249 | { 250 | int mnr = iminor(inode); 251 | struct fo *dev = &fo_devs[mnr]; 252 | if (debuglevel >= 3) 253 | printk(KERN_DEBUG "%s open. Minor#=%d\n", DEVNAME, mnr); 254 | 255 | if (down_interruptible(&dev->sem)) /* prevent races on open */ 256 | return -ERESTARTSYS; 257 | 258 | if (!dev->buf) { 259 | /* alloc the buffer, shared by all readers */ 260 | dev->buf = kmalloc(buffersize, GFP_KERNEL); 261 | if (!dev->buf) { 262 | if (debuglevel >= 1) { 263 | printk(KERN_ALERT "%s: No memory dev=%d.\n", 264 | DEVNAME, mnr); 265 | } 266 | up(&dev->sem); /* unlock sema */ 267 | return -ENOMEM; 268 | } 269 | } 270 | 271 | /* store which fanout device in the file's private data */ 272 | filp->private_data = (void *) dev; 273 | 274 | /* define the file to be immediately caught up with the fanout dev */ 275 | filp->f_pos = dev->count; 276 | up(&dev->sem); /* unlock semaphore we are done */ 277 | return nonseekable_open(inode, filp); /* success */ 278 | } 279 | 280 | 281 | static int fanout_release(struct inode *inode, struct file *filp) 282 | { 283 | if (debuglevel >= 3) 284 | printk(KERN_DEBUG "%s close. Minor#=%d.\n", DEVNAME, 285 | ((struct fo *) filp->private_data)->minor); 286 | 287 | return 0; /* success */ 288 | } 289 | 290 | 291 | static ssize_t fanout_read(struct file *filp, char __user * buff, 292 | size_t count, loff_t * offset) 293 | { 294 | int ret; 295 | loff_t xfer; /* num bytes read from fanout buf */ 296 | int cpcnt, cpstrt; /* cp count and start location */ 297 | struct fo *dev = (struct fo *) filp->private_data; 298 | 299 | if (down_interruptible(&dev->sem)) /* lock semaphore */ 300 | return -ERESTARTSYS; 301 | 302 | if (debuglevel >= 3) 303 | printk(KERN_DEBUG "%s: read %zu char from dev%d, off=%lld.\n", 304 | DEVNAME, count, dev->minor, *offset); 305 | 306 | /* Wait here until new data is available */ 307 | while (*offset == dev->count) { 308 | up(&dev->sem); /* unlock sema */ 309 | if (wait_event_interruptible(dev->inq, (*offset != dev->count))) 310 | return -ERESTARTSYS; 311 | if (down_interruptible(&dev->sem)) /* lock */ 312 | return -ERESTARTSYS; 313 | } 314 | 315 | /* Verify that data requested is in the buffer or is next byte */ 316 | xfer = dev->count - *offset; /* send count minus requested pointer */ 317 | if ((xfer > (loff_t) buffersize) || (xfer < 0)) { 318 | printk(KERN_DEBUG "%s: Overrun. xfer=%lld buffersize=%d", 319 | DEVNAME, xfer, buffersize); 320 | up(&dev->sem); /* unlock sema */ 321 | return -EPIPE; /* buffer overrun */ 322 | } 323 | 324 | /* Copy the new data out to the user */ 325 | xfer = dev->count - *offset; /* amount of data available to copy */ 326 | 327 | /* BUG: we need to check for a wrap on offset and count */ 328 | 329 | /* xfer less then available when requested */ 330 | xfer = ((loff_t)count < xfer) ? (loff_t)count : xfer; 331 | ret = xfer; /* we will handle these bytes */ 332 | while (xfer) { 333 | /* copy start is where the reader last read (indx - (count - offset)) */ 334 | cpstrt = dev->indx - (dev->count - *offset); 335 | if (cpstrt < 0) { /* adjust copy count if needed */ 336 | cpcnt = ((loff_t)(-cpstrt) < xfer) ? (loff_t)(-cpstrt) : xfer; 337 | cpstrt += buffersize; 338 | } else { 339 | cpcnt = xfer; 340 | } 341 | 342 | if (copy_to_user(buff, dev->buf + cpstrt, cpcnt)) { 343 | up(&dev->sem); 344 | return -EFAULT; 345 | } 346 | 347 | buff += cpcnt; 348 | xfer -= cpcnt; 349 | *offset += cpcnt; 350 | } 351 | 352 | up(&dev->sem); /* unlock sema */ 353 | 354 | return ret; 355 | } 356 | 357 | 358 | static ssize_t fanout_write( 359 | struct file *filp, 360 | const char __user * buff, 361 | size_t count, loff_t * off) 362 | { 363 | struct fo *dev = filp->private_data; 364 | 365 | int ret; 366 | int xfer; /* num bytes to read from user */ 367 | int cpcnt; /* num bytes in a copy */ 368 | 369 | if (down_interruptible(&dev->sem)) { /* lock semaphore */ 370 | return -ERESTARTSYS; 371 | } 372 | 373 | if (debuglevel >= 3) 374 | printk(KERN_DEBUG "%s: write %zu char to dev%d, off=%d.\n", 375 | DEVNAME, count, dev->minor, (int) *off); 376 | 377 | /* Copy at most one-quarter of the circular buffer size. This 378 | * gives readers more of a chance to wake up and get some data 379 | * In other words feed the reader little chuncks of data, they will 380 | * call again if they still want more 381 | */ 382 | ret = xfer = min((int) count, buffersize / 4); 383 | 384 | /* loop over the amount since the buffer is not a single block 385 | * but wraps arround 386 | */ 387 | while (xfer) { 388 | cpcnt = buffersize - dev->indx; 389 | cpcnt = min(cpcnt, xfer); 390 | 391 | if (debuglevel >= 3) 392 | printk(KERN_DEBUG "%s: write copy from user(%p,%p,%d)\n", 393 | DEVNAME, dev->buf + dev->indx, buff, cpcnt); 394 | 395 | if (copy_from_user(dev->buf + dev->indx, buff, cpcnt)) { 396 | up(&dev->sem); /* unlock semaphore */ 397 | return -EFAULT; 398 | } 399 | *off += cpcnt; 400 | dev->indx += cpcnt; 401 | dev->indx = (dev->indx == buffersize) ? 0 : dev->indx; 402 | xfer -= cpcnt; 403 | buff += cpcnt; 404 | } 405 | 406 | dev->count += ret; /* update file size */ 407 | up(&dev->sem); /* unlock semaphore */ 408 | 409 | /* This is what the readers have been waiting for */ 410 | wake_up_interruptible(&dev->inq); 411 | 412 | return ret; 413 | } 414 | 415 | static unsigned int fanout_poll(struct file *filp, poll_table * ppt) 416 | { 417 | /* The circular buffer is always available for writing */ 418 | int ready_mask = POLLOUT | POLLWRNORM; 419 | 420 | struct fo *dev = filp->private_data; 421 | poll_wait(filp, &dev->inq, ppt); 422 | 423 | if (filp->f_pos != dev->count) { 424 | ready_mask = (POLLIN | POLLRDNORM); 425 | } 426 | 427 | if (debuglevel >= 3) 428 | printk(KERN_DEBUG "%s: poll returns 0x%x\n", 429 | DEVNAME, ready_mask); 430 | 431 | return ready_mask; 432 | } 433 | 434 | #ifdef DEV_MKNOD 435 | /* callback invoked when making the nodes */ 436 | static char *fo_dev_devnode(struct device *dev, umode_t *mode) 437 | { 438 | if (!mode) 439 | return NULL; 440 | if (MAJOR(dev->devt) == fo_major) 441 | *mode = nodemode; 442 | return kasprintf(GFP_KERNEL, "%s", dev_name(dev)); 443 | } 444 | #endif /* DEV_MKNOD */ 445 | 446 | module_init(fanout_init_module); 447 | module_exit(fanout_exit_module); 448 | --------------------------------------------------------------------------------