??д???????????Linux??????
???????????? ???????[ 2014/9/30 10:46:15 ] ?????????????? Linux
??????????????if()?????while()?????????????????????????????write????????????????????????????????????????????У?????????δ????л?????е????????????????ο???????????????????????buf->data ???????????copy_to_user()????????????£?
????size = min(size?? (size_t) (buf->end - buf->read_ptr));
????if (copy_to_user(out?? buf->read_ptr?? size)) {
????result = -EFAULT;
????goto out;
????}
?????????????????????????????????????????????£????????-EFAULT???????????????κ???????????????
????buf->read_ptr += size;
????result = size;
????out:
????return result;
????}
?????????????????????????????м??????÷????????????????????????????????
????д????????????????????黺?????????????????????????copy_from_userspace()???????????????????read_ptr?????????????????????洢???????????
????buf->end = buf->data + size;
????buf->read_ptr = buf->data;
????if (buf->end > buf->data)
????reverse_phrase(buf->data?? buf->end - 1);
???????? reverse_phrase()???????г??????????????????reverse_word()???????ú????????????????????????????????????????????????????????á?????????????????????????????
?????????????????read_queue?е????????????????????????????wake_up_interruptible()??????????μ??
????wake_up_interruptible(&buf->read_queue);
????????????????????????????飬?????????????????????????????????????
??????????????
?????????????г??????????????????????????????????????printk() ????????KERN_DEBUG?????????????????????и????????????????д????豸??????????豸?????????????“struct device”?????????pr_debug()????dev_dbg()????????????????dyndbg???????????????????????????????????Documentation/dynamic-debug-howto.txt????????????????????????pr_devel()????????????DEBUG????????????????????????????????DEBUG????????????е?Makefile?У?
????CFLAGS_reverse.o := -DDEBUG
??????????????dmesg????pr_debug()??pr_devel()????????????? ?????????????????????????????????????????????????console_loglevel???????8???????????echo 8 /proc/sys/kernel/printk?????????????????????KERN_ERR??????????????????????????????????????????????????????????????????
??????????????????????????????Xterm?????????????????????????????????????????????????X????????е????
??????????????
??????????飬???????????
????$ make
????$ sudo insmod reverse.ko buffer_size=2048
????$ lsmod
????reverse 2419 0
????$ ls -l /dev/reverse
????crw-rw-rw- 1 root root 10?? 58 Feb 22 15:53 /dev/reverse
??????????λ?????????????????????????????????д???С??????????????????????в?????main()????????????????????????????
????int fd = open("/dev/reverse"?? O_RDWR);
????write(fd?? argv[1]?? strlen(argv[1]));
????read(fd?? argv[1]?? strlen(argv[1]));
????printf("Read: %s "?? argv[1]);
?????????????У?
????$ ./test 'A quick brown fox jumped over the lazy dog'
????Read: dog lazy the over jumped fox brown quick A
????????????????????????????????????????????????????????????????????????????????????????????????????????????????κζ?????
??????????????????????????????????????????????????????????????????????????????????????????????????д??????????豸????????????????Щ??????????????У??????????fork(2)?????????pthreads?????á?????????????豸???????????????????????????
????char *phrase = "A quick brown fox jumped over the lazy dog";
????if (fork())
????/* Parent is the writer */
????while (1)
????write(fd?? phrase?? len);
????else
????/* child is the reader */
????while (1) {
????read(fd?? buf?? len);
????printf("Read: %s "?? buf);
????}
??????????????????????????????????????????????????
????Read: dog lazy the over jumped fox brown quick A
????Read: A kcicq brown fox jumped over the lazy dog
????Read: A kciuq nworb xor jumped fox brown quick A
????Read: A kciuq nworb xor jumped fox brown quick A
????...
????????????????????????????????????????read??write??????????????????β????????????????????????????????????μ?????reverse_phrase()????????????????????д???????????????????д????????????????read()?????????????????????????????????bug?????????????????????????????????????
???????????????????????д????????????read?????????С????????????д?????????????ó????????????????????????綏????????????Linux?????Щ??????Щ??????????????????н????????????????????“????”????????????????????????????????????磬???IRQ??????????????????????????????к??????????????????????????????????????????????????????ж????????????????????????????????????????????????????á????????????????????????????????У????????????? —— ??????????????????????“???”????? —— ???????????????????????????????????????-д????
??????????????Щ????????????????У??????“struct buffer”??????????????????????????????????????????С???????????????????????‘struct mutex lock’????“struct buffer”?С????????????mutex_init()???????????????buffer_alloc??????????????μ???????????????????????????linux/mutex.h??
??????????????????? —— ????????????????????????????á??????????????????????????????????????????????????reverse_read()??reverse_write()?????????????????????????read???? —— write?????????????
????static ssize_t reverse_read(struct file *file?? char __user * out??
????size_t size?? loff_t * off)
????{
????struct buffer *buf = file->private_data;
????ssize_t result;
????if (mutex_lock_interruptible(&buf->lock)) {
????result = -ERESTARTSYS;
????goto out;
????}
????????????????????????mutex_lock_interruptible()????????????????????y???????????п?????????????????????_interruptible?????ζ????????????????ж??
????while (buf->read_ptr == buf->end) {
????mutex_unlock(&buf->lock);
????/* ... wait_event_interruptible() here ... */
????if (mutex_lock_interruptible(&buf->lock)) {
????result = -ERESTARTSYS;
????goto out;
????}
????}
???????????????“???????”??????????????????????????????“????”?????????????y????????????????????????????????????????wait_event_interruptible()??????????????????????????????????????????????
????if (copy_to_user(out?? buf->read_ptr?? size)) {
????result = -EFAULT;
????goto out_unlock;
????}
????...
????out_unlock:
????mutex_unlock(&buf->lock);
????out:
????return result;
???????????????????????????????????????з???????????????????????????±?????饗?????????????????????ν??в?????????????????????????????
????????????????
????????????????????????????????????????????????????£????滹?и?????????????????????????????????д?ü?????????????????????????????????????????????????????????????????????????????????Щ?????????????????????????????????????????????????????????????????????????????????Linux????????? —— ?????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Linux??????DDR????????????Linux?????????????????豸??????????linux???в???????????м??????????????????????????Linux????Linux??DNS????????????γ???????????Linux?????????????Linux????(???)????????Linux ????????????????????????????????Windows???У?Linux???????Linux????????????????Linux iommu??vfio???????Linux????ε???TCP???????Linux?????????????СLinux?·???????????Linux???Jexus?й?Asp.Net Core??ó???Linux?????????Щ??
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????