<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span style="color:#1F497D">Hello,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">                After a lot of debugging and with the help of people much more knowledgeable than I am, I return with updated information.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">                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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">                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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">                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).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">                It was all fine and nice when printf("press any key to continue"); has been replaced with while(1);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">                Thank you.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Best Regards,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Andrei Porumb<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">TL;DR version: don't use getchar() in a service.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><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>
<b>Sent:</b> Monday, April 13, 2015 10:28 AM<br>
<b>To:</b> snappy-app-devel@lists.ubuntu.com<br>
<b>Subject:</b> service with shared libraries<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Hello all,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">                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.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">                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:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">                #!/bin/sh<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">export LD_LIBRARY_PATH=./bin<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">./bin/FunkyTv<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">echo this line should never be executed.<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">This is the content of package.yaml<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">name: simplesampleamqp<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">version: 45<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">icon: meta/icon.png<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">maintainer: <a href="mailto:anporumb@microsoft.com">
anporumb@microsoft.com</a><o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">services:<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">- name: sum<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">   description: SomeService<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">   start: bin/sum.sh<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">   caps: networking<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Problem: the service doesn't start.
<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Observations: if the app is run from the command line (as a regular app), the app runs just fine and performs as expected.<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">When run as a service, there's no life.<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Journalctl helps a little here, and this is the output:<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">ubuntu@localhost:/apps/simplesampleamqp/current$ sudo journalctl -f -u simplesampleamqp_sum_45<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">-- Logs begin at Sun 2015-04-12 16:33:29 UTC. --<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Apr 13 16:59:12 localhost.localdomain systemd[1]: Started SomeService.<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Apr 13 16:59:12 localhost.localdomain systemd[1]: Starting SomeService...<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Apr 13 16:59:12 localhost.localdomain sum.sh[2920]: Segmentation fault<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Apr 13 16:59:12 localhost.localdomain sum.sh[2920]: this line should never be executed.<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.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.<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.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...<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.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...<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">Any hints/help is greatly appreciated.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best Regards,<o:p></o:p></p>
<p class="MsoNormal">Andrei Porumb<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in"><o:p> </o:p></p>
</div>
</body>
</html>