<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span></span></div><div>It has been many years since I wrote any C but can you flush the printf with the fflush command and possibly a sync() to really force it to disk?</div><div><br></div> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <div class="hr" style="margin: 5px 0px; padding: 0px; border: 1px solid rgb(204, 204, 204); border-image: none; height: 0px; line-height: 0; font-size: 0px;" contenteditable="false" readonly="true"></div> <font face="Arial" size="2"> <b><span style="font-weight: bold;">From:</span></b> Andrei Porumb <anporumb@microsoft.com><br> <b><span style="font-weight: bold;">To:</span></b> Andrei Porumb <anporumb@microsoft.com>;
"snappy-app-devel@lists.ubuntu.com" <snappy-app-devel@lists.ubuntu.com> <br> <b><span style="font-weight: bold;">Sent:</span></b> Wednesday, 15 April 2015, 1:37<br> <b><span style="font-weight: bold;">Subject:</span></b> RE: service with shared libraries<br> </font> </div> <div class="y_msg_container"><br><div id="yiv4387372351"><style>#yiv4387372351 #yiv4387372351 --
_filtered #yiv4387372351 {panose-1:2 4 5 3 5 4 6 3 2 4;}
_filtered #yiv4387372351 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;}
#yiv4387372351
#yiv4387372351 p.yiv4387372351MsoNormal, #yiv4387372351 li.yiv4387372351MsoNormal, #yiv4387372351 div.yiv4387372351MsoNormal
{margin:0in;margin-bottom:.0001pt;font-size:11.0pt;}
#yiv4387372351 a:link, #yiv4387372351 span.yiv4387372351MsoHyperlink
{color:#0563C1;text-decoration:underline;}
#yiv4387372351 a:visited, #yiv4387372351 span.yiv4387372351MsoHyperlinkFollowed
{color:#954F72;text-decoration:underline;}
#yiv4387372351 span.yiv4387372351EmailStyle17
{color:windowtext;}
#yiv4387372351 span.yiv4387372351EmailStyle18
{color:#1F497D;}
#yiv4387372351 .yiv4387372351MsoChpDefault
{font-size:10.0pt;}
_filtered #yiv4387372351 {margin:1.0in 1.0in 1.0in 1.0in;}
#yiv4387372351 div.yiv4387372351WordSection1
{}
#yiv4387372351 </style><div>
<div class="yiv4387372351WordSection1">
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);">Hello,</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> After a lot of debugging and with the help of people much more knowledgeable than I am, I return with updated information.</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> The information which I posted was true, but I was on a wrong debug track. After int main(void) there was a printf in my code... and since I never saw that printf I was assuming "something happens
before main" (and since I was using a special shared library it made sense that "loading shared libraries has a problem"). On the other hand, having the app run just nicely, was a sign that my assumption was wrong.</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> And it was. The reason I was not seeing any printf debugging was because of the Segmentation Fault. Printf output is buffered and when segfault happens, that buffer is not flushed. And it is
lost. So in reality the printfs were happening (and so was most of my code), just I was not able to gain any insight on that.</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> The reason why the service was crashing with segfault was because of a simple getchar() C function call. Hint: services do not have a standard input. Because no console is created for a service.
As opposed to an application (that runs from a console, so it has stdin, stdout).</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> It was all fine and nice when printf("press any key to continue"); has been replaced with while(1);</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> Thank you.</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);">Best Regards,</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);">Andrei Porumb</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);">TL;DR version: don't use getchar() in a service.</span></div>
<div class="yiv4387372351MsoNormal"><span style="color: rgb(31, 73, 125);"> </span></div>
<div class="yiv4387372351yqt0073578609" id="yiv4387372351yqt82271"><div>
<div style="border-width: 1pt medium medium; border-style: solid none none; border-color: rgb(225, 225, 225) currentColor currentColor; padding: 3pt 0in 0in; border-image: none;">
<div class="yiv4387372351MsoNormal"><b>From:</b> snappy-app-devel-bounces@lists.ubuntu.com [mailto:snappy-app-devel-bounces@lists.ubuntu.com]
<b>On Behalf Of </b>Andrei Porumb<br clear="none">
<b>Sent:</b> Monday, April 13, 2015 10:28 AM<br clear="none">
<b>To:</b> snappy-app-devel@lists.ubuntu.com<br clear="none">
<b>Subject:</b> service with shared libraries</div>
</div>
</div>
<div class="yiv4387372351MsoNormal"> </div>
<div class="yiv4387372351MsoNormal">Hello all,</div>
<div class="yiv4387372351MsoNormal"> </div>
<div class="yiv4387372351MsoNormal"> I need to use Ubuntu Snappy to develop a service. The service has "special communication needs"... err... AMQP... so it needs a shared library to do so.</div>
<div class="yiv4387372351MsoNormal"> </div>
<div class="yiv4387372351MsoNormal"> The code is written in C. For the executable to load the shared library, it needs to have set LD_LIBRARY_PATH. So the service is started by a bash script that doesn't do much, except:</div>
<div class="yiv4387372351MsoNormal"> </div>
<div class="yiv4387372351MsoNormal"> #!/bin/sh</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">export LD_LIBRARY_PATH=./bin</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">./bin/FunkyTv</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">echo this line should never be executed.</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">This is the content of package.yaml</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">name: simplesampleamqp</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">version: 45</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">icon: meta/icon.png</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">maintainer: <a href="mailto:anporumb@microsoft.com" target="_blank" rel="nofollow" shape="rect" ymailto="mailto:anporumb@microsoft.com">mailto:anporumb@microsoft.com</a></div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">services:</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">- name: sum</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> description: SomeService</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> start: bin/sum.sh</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> caps: networking</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Problem: the service doesn't start.
</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Observations: if the app is run from the command line (as a regular app), the app runs just fine and performs as expected.</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">When run as a service, there's no life.</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Journalctl helps a little here, and this is the output:</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">ubuntu@localhost:/apps/simplesampleamqp/current$ sudo journalctl -f -u simplesampleamqp_sum_45</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">-- Logs begin at Sun 2015-04-12 16:33:29 UTC. --</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Apr 13 16:59:12 localhost.localdomain systemd[1]: Started SomeService.</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Apr 13 16:59:12 localhost.localdomain systemd[1]: Starting SomeService...</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Apr 13 16:59:12 localhost.localdomain sum.sh[2920]: Segmentation fault</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Apr 13 16:59:12 localhost.localdomain sum.sh[2920]: this line should never be executed.</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">The segmentation fault happens (as far as I can tell) before the first line in main. Since this is C code (as opposed to C++) there cannot be other code executed before main.</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Further investigation revealed that in the small script that attempts to start the service there cannot be just any command. For example, "ldd" cannot be there (if it is, there's going to be a DENIAL something like
: Apr 12 19:53:10 localhost.localdomain kernel: audit: type=1400 audit(1428868390.904:62): apparmor="DENIED" operation="exec" profile="simplesampleamqp_sum_44" name="/usr/bin/ldd" pid=2310 comm="sum.sh" requested_mask="x" denied_mask="x" fsuid=0 ouid=0). Echo
is fine to be in the script...</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">I guess it all boils down to: how does one write some service in C that needs some shared libraries and loads them and executes? As an "app" the executable runs fine...</div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;">Any hints/help is greatly appreciated.</div>
<div class="yiv4387372351MsoNormal"> </div>
<div class="yiv4387372351MsoNormal">Best Regards,</div>
<div class="yiv4387372351MsoNormal">Andrei Porumb</div></div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
<div class="yiv4387372351MsoNormal" style="text-indent: 0.5in;"> </div>
</div>
</div></div><br><div class="yqt0073578609" id="yqt20949">-- <br clear="none">snappy-app-devel mailing list<br clear="none"><a href="mailto:snappy-app-devel@lists.ubuntu.com" shape="rect" ymailto="mailto:snappy-app-devel@lists.ubuntu.com">snappy-app-devel@lists.ubuntu.com</a><br clear="none">Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/snappy-app-devel" target="_blank" shape="rect">https://lists.ubuntu.com/mailman/listinfo/snappy-app-devel</a><br clear="none"></div><br><br></div> </div> </div> </div></body></html>