<div dir="ltr">Phil,<div>Thanks a lot for the code</div><div><br></div><div>Anand Neeli</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 8, 2015 at 2:05 AM, Philippe Proulx <span dir="ltr"><<a href="mailto:eeppeliteloop@gmail.com" target="_blank">eeppeliteloop@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Anand,<br>
<br>
On Tue, Apr 7, 2015 at 4:19 PM, Anand Neeli <<a href="mailto:anand.neeli@gmail.com">anand.neeli@gmail.com</a>> wrote:<br>
> For C<br>
<br>
lttng-relayd does not write its PID to a file.<br>
<br>
For C, here's the programmatic way of getting the PID of the first<br>
lttng-relayd process found (inspired by [1]):<br>
<br>
#include <stdlib.h><br>
#include <stdio.h><br>
#include <string.h><br>
#include <dirent.h><br>
<br>
/* checks if the string is purely an integer<br>
* we can do it with `strtol' also<br>
*/<br>
int check_if_number (char *str)<br>
{<br>
int i;<br>
for (i=0; str[i] != '\0'; i++)<br>
{<br>
if (!isdigit (str[i]))<br>
{<br>
return 0;<br>
}<br>
}<br>
return 1;<br>
}<br>
<br>
#define MAX_BUF 1024<br>
#define PID_LIST_BLOCK 32<br>
<br>
int *pidof (char *pname)<br>
{<br>
DIR *dirp;<br>
FILE *fp;<br>
struct dirent *entry;<br>
int *pidlist, pidlist_index = 0, pidlist_realloc_count = 1;<br>
char path[MAX_BUF], read_buf[MAX_BUF];<br>
<br>
dirp = opendir ("/proc/");<br>
if (dirp == NULL)<br>
{<br>
perror ("Fail");<br>
return NULL;<br>
}<br>
<br>
pidlist = malloc (sizeof (int) * PID_LIST_BLOCK);<br>
if (pidlist == NULL)<br>
{<br>
return NULL;<br>
}<br>
<br>
while ((entry = readdir (dirp)) != NULL)<br>
{<br>
if (check_if_number (entry->d_name))<br>
{<br>
strcpy (path, "/proc/");<br>
strcat (path, entry->d_name);<br>
strcat (path, "/comm");<br>
<br>
/* A file may not exist, it may have been removed.<br>
* dut to termination of the process. Actually we need to<br>
* make sure the error is actually file does not exist to<br>
* be accurate.<br>
*/<br>
fp = fopen (path, "r");<br>
if (fp != NULL)<br>
{<br>
fscanf (fp, "%s", read_buf);<br>
if (strcmp (read_buf, pname) == 0)<br>
{<br>
/* add to list and expand list if needed */<br>
pidlist[pidlist_index++] = atoi (entry->d_name);<br>
if (pidlist_index == PID_LIST_BLOCK * pidlist_realloc_count)<br>
{<br>
pidlist_realloc_count++;<br>
pidlist = realloc (pidlist, sizeof (int) *<br>
PID_LIST_BLOCK * pidlist_realloc_count); //Error check todo<br>
if (pidlist == NULL)<br>
{<br>
return NULL;<br>
}<br>
}<br>
}<br>
fclose (fp);<br>
}<br>
}<br>
}<br>
<br>
<br>
closedir (dirp);<br>
pidlist[pidlist_index] = -1; /* indicates end of list */<br>
return pidlist;<br>
}<br>
<br>
int main (void)<br>
{<br>
int *list = pidof("lttng-relayd");<br>
<br>
if (!list || list[0] == -1) {<br>
fprintf(stderr, "lttng-relayd is not running!\n");<br>
free(list);<br>
return 1;<br>
}<br>
<br>
printf("PID of first lttng-relayd found: %d\n", list[0]);<br>
free(list);<br>
<br>
return 0;<br>
}<br>
<br>
[1] <a href="http://phoxis.org/2013/09/13/find-process-ids-of-a-running-process-by-name/" target="_blank">http://phoxis.org/2013/09/13/find-process-ids-of-a-running-process-by-name/</a><br>
<br>
Hope it helps,<br>
Phil<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
><br>
><br>
><br>
> On Wed, Apr 8, 2015 at 1:45 AM, Philippe Proulx <<a href="mailto:eeppeliteloop@gmail.com">eeppeliteloop@gmail.com</a>><br>
> wrote:<br>
>><br>
>> Anand,<br>
>><br>
>> On Tue, Apr 7, 2015 at 4:07 PM, Anand Neeli <<a href="mailto:anand.neeli@gmail.com">anand.neeli@gmail.com</a>> wrote:<br>
>> ><br>
>> > Hi All,<br>
>> > Is there any better way of getting lttng-relayd PID programmatically?<br>
>> ><br>
>> > like sessiond pid is in /var/run/lttng/lttng-sessiond.pid, is there any<br>
>> > similar way to get relayd PID?<br>
>><br>
>> For which language?<br>
>><br>
>> Phil<br>
>> ><br>
>> ><br>
>> > Thanks,<br>
>> > Anand Neeli<br>
>> ><br>
>> > _______________________________________________<br>
>> > lttng-dev mailing list<br>
>> > <a href="mailto:lttng-dev@lists.lttng.org">lttng-dev@lists.lttng.org</a><br>
>> > <a href="http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev" target="_blank">http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev</a><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br></div>