What are the implications of the linux __user macro?
I was hoping someone could explain the nuances of the __user macro used in the linux kernel source.
First of all, the macro:
Now, after some googling I read that this macro allows one to designate a pointer as belonging to the user address space, and that it should not be dereferenced.
I may be missing some obvious facts, but could someone please explain the implications of such a macro? For instance, what is a good example of where this macro would be of use? Again, forgive me if I am missing something obvious.
To put this in some context, I came accross the macro while examining some USB code (linux/usbdevice_fs.h). I am only looking for a general understanding of this macros( or others like it) use within the kernel.
Thanks for looking!
3 Answers 3
It allows tools like sparse to tell kernel developers that they’re possibly using an untrusted pointer (or a pointer that may be invalid in the current virtual address mapping) improperly.
I think __user marks user space pointers and tells the developer/system not to trust it. If user gives you «invalid» pointer, then kernel tries to reference it (note that kernel can reference everywhere) and it can corrupt it’s own space.
For example in «read»(in you usbdevice_fs.h) should provide you a (__user) buffer to write the result to. So you have to use copy_to_user, but not memcopy, strcpy or anything like this.
Note: This is not formal definition/description, but the only part I’m aware of.
The __user macro is defined with some other macros like __force / __kernel etc in the compiler.h header file. They are actually not of any use to traditional compilers, including GCC/ICC etc. But it’s useful for kernel static analysis tools like sparse (more information here: Sparse — Linux Kernel Newbies). When you mention the macros like __user / __kernel / __force etc, it keeps special meaning for sparse. In the Linux kernel mailing list, Linus Torvalds explains the use of it like this:
This is important to remember: for gcc, the sparse annotations are meaningless. They can still be useful just to tell the programmer that «hey, that pointer you got wasn’t a normal pointer» in a fairly readable manner, but in the end, unless you use sparse, they don’t actually do anything.
HOWEVER. When you do use parse, it is another matter entirely. For «sparse», that «__iomem» has lots of meaning:
ie «iomem» means two separate things: it means that sparse should complain
if the pointer is ever dereferenced (it’s a «noderef» pointer) directly, and it’s in «address space 2» as opposed to the normal address space (0).
Now, that means that sparse will complain if such a pointer is ever passed into a function that wants a regular pointer (because it is not a normal pointer, and you obviously shouldn’t do things like «strcmp()» etc on it), and sparse will also complain if you try to cast it to another pointer in another address space.
Источник
what is the meaning of this macro _IOR(MY_MACIG, 0, int)?
i was going through ioctl sample programs to check how it communicates with kernel space. in program WRITE_IOCTL is used as command
I am not able to understand what is _IOW(MY_MACIG, 1, int) . here is the link from where i downloaded the program. please help me. http://people.ee.ethz.ch/
2 Answers 2
As you may know, an ioctl should be unique, as explained in the Linux Device Drivers book:
The ioctl command numbers should be unique across the system in order to prevent errors caused by issuing the right command to the wrong device.Such a mismatch is not unlikely to happen, and a program might find itself trying to change the baudrate of a non-serial-port input stream, such as a FIFO or an audio device. If each ioctl number is unique, the application gets an EINVAL error rather than succeeding in doing something unintended.
Furthermore, an ioctl can require to write data to and/or read data from kernel space.
When one creates it’s own driver that performs ioctls, he will need to describe all this in the ioctl command.
_IO, _IOW, _IOR, _IORW are helper macros to create a unique ioctl identifier and add the required R/W needed features (direction).
These can take the following params: magic number, the command id, and the data type that will be passed (if any)
The magic number is a unique number that will allow the driver to detect errors such as the one mentioned in the LDD book’s quote.
The command id, is a way for your dirver to understand what command is needed to be called.
Last parameter (the type) will allow the kernel to understand the size to be copied.
hope this helps.
PS: you can have more details in Linux Device Drivers book (chapter 6) https://lwn.net/images/pdf/LDD3/ch06.pdf
The Linux header file /usr/include/asm/ioctl.h defines macros that must be used to create the ioctl command number. These macros take various combinations of three arguments:
- type, an 8-bit integer selected to be specific to the device driver. type should be chosen so as not to conflict with other drivers that might be «listening» to the same file descriptor. (Inside the kernel, for example, the TCP and IP stacks use distinct numbers since an ioctl sent to a socket file descriptor might be examined by both stacks.)
- number, an 8-bit integer command number. Within a driver, distinct numbers should be chosen for each different kind of ioctl command that the driver services
- data_type, the name of a type used to compute how many bytes are exchanged between the client and the driver. This argument is, for example, the name of a structure.
The macros used to generate command numbers are:
- _IO(int type, int number), used for a simple ioctl that sends nothing but the type and number, and receives back nothing but an (integer) retval
- _IOR(int type, int number, data_type), used for an ioctl that reads data from the device driver. The driver will be allowed to return sizeof(data_type) bytes to the user
- _IOW(int type, int number, data_type), similar to _IOR, but used to write data to the driver
- _IORW(int type, int number, data_type), a combination of _IOR and _IOW. That is, data is both written to the driver and then read back from the driver by the client
Источник
How can I create Macros for games on Linux? [duplicate]
I am searching for a free software to create macros for games on Linux, that could work on any keyboard with a GUI.
I want it to do successive keystrokes and tweak the delay between each key.
1 Answer 1
You must install the software Autokey, you can type this in the terminal:
Open it and click on New then Script and name it (you can also create a folder with the name of your game).
Keyboard
There is two important lines, here is the code for pressing the key «x»:
And here is how to add a delay, 1/10 of a second (try lower values until the game doesn’t allow it, also keep in mind than an high ping can make some keys doesn’t input if the delay is too low):
Then repeat the code by adding a delay between each keys, for the last key you don’t need to add a delay after it.
To hold a key type that, for example to hold then release Shift and typing x in between:
Mouse
To click use this two commands:
The first command is relative (to click near the mouse’s current location) the second is absolute (from the entire screen). The button takes 3 arguments:
This script work best if you add a minimum of 0.1 second of sleep. Here an example to click at the center of the screen:
Comment
You can add a comment with «#»:
Once your script is finished you can bind it to a key, bellow your script you will see script settings, at the line Hotkey click on Set and define a key. I recommend you to use the key F# to attach it or the key. You can test if your macro is working by opening a text editor like gedit.
For Dota it is better to use the in game queue feature, for example to use 3 items and one spell:
Источник
What is macro linux
The easiest way to write a macro is not to write it from scratch but to let UltraEdit do most of the work for you. One of UltraEdit’s trademark features is the ability to use powerful macros.
The macro menu includes a feature called quick record which can write most of your macro for you if you understand how it works. The quick record function records what you are doing until you tell it to stop.
When writing a macro, using quickrecord, the idea is to manipulate the text/data as much as possible using the keyboard, UltraEdit’s native functions, Templates, and even other macros so quickrecord can record your steps. After you stop quickrecord, you can then go back and edit the macro by hand.
For instance, you can go back through your macros and add loops, conditional statments, regular expressions, etc.
We have used this method MANY times in creating very complex macros which have saved us hours and hours of work. It’s just another example of the power of UltraEdit!
Writing a Macro
For the following example, we will take a pipe delimited data file (containing mailing addresses) and format the addresses into a standard format. Furthermore, we want the macro to run through the entire file, formatting each record.
The sample file which we are using looks like this:
We understand that this may not be the exact macro you are looking to create, but pay attention to the methods used to create your own macro.
Open the data file and first think through what you are starting with and what you need to end up with.
It’s easiest to take a single record and determine what needs to be done with it, and worry about repeating the macro later.
For our example, this is what we are starting with:
Kevin Smith|109 Red Oak Dr.|Waukesha|WI|53189|USA
and this is what we would like to end up:
Kevin Smith
109 Red Oak Dr.
Waukesha, WI 53189
USA
Step through how you will format the data using only the keyboard.
Keep in mind, if you are planning on using loops (covered later) you will want to think through how you are incrementing to satisfy the loop condition.
You don’t necessarily need to do this, but we always suggest running through the steps one time before using the quick record to make sure you know how to use only the keyboard and menu functions get the results you need.
Go to Macro : Quick Record (CTRL SHIFT R)
For the above example, we did a Find for «|». This placed our cursor at the pipe symbol every time we hit F3 (Find Next).
Then, we hit ENTER, SPACEBAR, and ‘,’ (where appropriate) to format the data as needed. The result:
Now that the single record is formatted as desired, stop quick recording in the Macro menu.
To edit the macro you’ve recorded, go to Macro : Edit Macro. You will then see the Edit/Create Macro dialog.
Modify the Macro properties
I like to start by naming the macro, and assigning it a Hot Key. Click on the «Modify» button on the edit macro dialog.
The «Modify Macro» dialog will open and allow you to rename the macro and assign a Hot Key:
Editing the macro
Depending on what you want the macro to do, there are many functions listed in the command section. You can move them over using the arrow or type them in by hand. For further information regarding the functionality of the commands available please see the help.
You will do your editing, as you may have guessed, in the text area:
For our example, we want to run through the entire file and format each record. This is known as a loop.
Loop the macro
This is where some programming experience comes in handy as you can add loops, conditional statements, etc. Loops and conditional statements are not covered in the scope of this powertip as it assumes some prior knowledge of this.
Keep in mind, not every macro will require loops; however for our example we will use a loop to run through all of the data until the end of the file.
The idea of the loop is to continue doing some action(s) until a condition is satisfied, at which point the loop will stop. When using a loop, you MUST ensure that the condition will be satisfied in order to stop the loop, otherwise the loop will run indefinitely.
For our example the loop condition to be satisfied is the end of the file. The basic logic of the loop is: ‘if the cursor is at the end of the file, exit the loop; otherwise, keep running these steps’. ie:
When recording my keyboard actions, after running through all of our formatting, we intentionally placed the cursor at the beginning of the next line. We are therefore ‘incrementing’ the position of the cursor so with each loop, we are moving the cursor towards the end of the file. If we didn’t do this, the loop would spin indefinitely on the first record creating an ‘infinite loop’.
Now, where to place the loop to do the appropriate actions. In the edit macro dialog, our macro looks like this:
The main action we want accomplished is the code below «ColumnModeOff». So we edited our macro to look like this:
To help you understand where the loop/conditional statements flow, please see the following:
To save the macro you have created you can either switch to a different Macro (in the drop down) or close the macro. You will then be prompted to update the macro. Answer ‘yes‘. This will update the macro and then return you to the edit window.
You can save now save the macro (for later use) by going to Macro : Save All.
Finally, to play the macro, go to Macro : Play Any Multiple Times.
Take a look at the result of running the macro:
Please note, it is always a good idea to first test the macro on sample data, to ensure it is performing as you intend it to.
Источник