* [lttng-dev] how to get relayd pid
@ 2015-04-07 20:07 Anand Neeli
2015-04-07 20:15 ` Philippe Proulx
0 siblings, 1 reply; 5+ messages in thread
From: Anand Neeli @ 2015-04-07 20:07 UTC (permalink / raw)
Hi All,
Is there any better way of getting lttng-relayd PID programmatically?
like sessiond pid is in /var/run/lttng/lttng-sessiond.pid, is there any
similar way to get relayd PID?
Thanks,
Anand Neeli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lttng.org/pipermail/lttng-dev/attachments/20150408/28ce8e0a/attachment.html>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [lttng-dev] how to get relayd pid
2015-04-07 20:07 [lttng-dev] how to get relayd pid Anand Neeli
@ 2015-04-07 20:15 ` Philippe Proulx
2015-04-07 20:19 ` Anand Neeli
0 siblings, 1 reply; 5+ messages in thread
From: Philippe Proulx @ 2015-04-07 20:15 UTC (permalink / raw)
Anand,
On Tue, Apr 7, 2015 at 4:07 PM, Anand Neeli <anand.neeli at gmail.com> wrote:
>
> Hi All,
> Is there any better way of getting lttng-relayd PID programmatically?
>
> like sessiond pid is in /var/run/lttng/lttng-sessiond.pid, is there any similar way to get relayd PID?
For which language?
Phil
>
>
> Thanks,
> Anand Neeli
>
> _______________________________________________
> lttng-dev mailing list
> lttng-dev at lists.lttng.org
> http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [lttng-dev] how to get relayd pid
2015-04-07 20:15 ` Philippe Proulx
@ 2015-04-07 20:19 ` Anand Neeli
2015-04-07 20:35 ` Philippe Proulx
0 siblings, 1 reply; 5+ messages in thread
From: Anand Neeli @ 2015-04-07 20:19 UTC (permalink / raw)
For C
On Wed, Apr 8, 2015 at 1:45 AM, Philippe Proulx <eeppeliteloop at gmail.com>
wrote:
> Anand,
>
> On Tue, Apr 7, 2015 at 4:07 PM, Anand Neeli <anand.neeli at gmail.com> wrote:
> >
> > Hi All,
> > Is there any better way of getting lttng-relayd PID programmatically?
> >
> > like sessiond pid is in /var/run/lttng/lttng-sessiond.pid, is there any
> similar way to get relayd PID?
>
> For which language?
>
> Phil
> >
> >
> > Thanks,
> > Anand Neeli
> >
> > _______________________________________________
> > lttng-dev mailing list
> > lttng-dev at lists.lttng.org
> > http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lttng.org/pipermail/lttng-dev/attachments/20150408/e03002f8/attachment.html>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [lttng-dev] how to get relayd pid
2015-04-07 20:19 ` Anand Neeli
@ 2015-04-07 20:35 ` Philippe Proulx
2015-04-08 13:42 ` Anand Neeli
0 siblings, 1 reply; 5+ messages in thread
From: Philippe Proulx @ 2015-04-07 20:35 UTC (permalink / raw)
Anand,
On Tue, Apr 7, 2015 at 4:19 PM, Anand Neeli <anand.neeli at gmail.com> wrote:
> For C
lttng-relayd does not write its PID to a file.
For C, here's the programmatic way of getting the PID of the first
lttng-relayd process found (inspired by [1]):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
/* checks if the string is purely an integer
* we can do it with `strtol' also
*/
int check_if_number (char *str)
{
int i;
for (i=0; str[i] != '\0'; i++)
{
if (!isdigit (str[i]))
{
return 0;
}
}
return 1;
}
#define MAX_BUF 1024
#define PID_LIST_BLOCK 32
int *pidof (char *pname)
{
DIR *dirp;
FILE *fp;
struct dirent *entry;
int *pidlist, pidlist_index = 0, pidlist_realloc_count = 1;
char path[MAX_BUF], read_buf[MAX_BUF];
dirp = opendir ("/proc/");
if (dirp == NULL)
{
perror ("Fail");
return NULL;
}
pidlist = malloc (sizeof (int) * PID_LIST_BLOCK);
if (pidlist == NULL)
{
return NULL;
}
while ((entry = readdir (dirp)) != NULL)
{
if (check_if_number (entry->d_name))
{
strcpy (path, "/proc/");
strcat (path, entry->d_name);
strcat (path, "/comm");
/* A file may not exist, it may have been removed.
* dut to termination of the process. Actually we need to
* make sure the error is actually file does not exist to
* be accurate.
*/
fp = fopen (path, "r");
if (fp != NULL)
{
fscanf (fp, "%s", read_buf);
if (strcmp (read_buf, pname) == 0)
{
/* add to list and expand list if needed */
pidlist[pidlist_index++] = atoi (entry->d_name);
if (pidlist_index == PID_LIST_BLOCK * pidlist_realloc_count)
{
pidlist_realloc_count++;
pidlist = realloc (pidlist, sizeof (int) *
PID_LIST_BLOCK * pidlist_realloc_count); //Error check todo
if (pidlist == NULL)
{
return NULL;
}
}
}
fclose (fp);
}
}
}
closedir (dirp);
pidlist[pidlist_index] = -1; /* indicates end of list */
return pidlist;
}
int main (void)
{
int *list = pidof("lttng-relayd");
if (!list || list[0] == -1) {
fprintf(stderr, "lttng-relayd is not running!\n");
free(list);
return 1;
}
printf("PID of first lttng-relayd found: %d\n", list[0]);
free(list);
return 0;
}
[1] http://phoxis.org/2013/09/13/find-process-ids-of-a-running-process-by-name/
Hope it helps,
Phil
>
>
>
>
> On Wed, Apr 8, 2015 at 1:45 AM, Philippe Proulx <eeppeliteloop at gmail.com>
> wrote:
>>
>> Anand,
>>
>> On Tue, Apr 7, 2015 at 4:07 PM, Anand Neeli <anand.neeli at gmail.com> wrote:
>> >
>> > Hi All,
>> > Is there any better way of getting lttng-relayd PID programmatically?
>> >
>> > like sessiond pid is in /var/run/lttng/lttng-sessiond.pid, is there any
>> > similar way to get relayd PID?
>>
>> For which language?
>>
>> Phil
>> >
>> >
>> > Thanks,
>> > Anand Neeli
>> >
>> > _______________________________________________
>> > lttng-dev mailing list
>> > lttng-dev at lists.lttng.org
>> > http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
>> >
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [lttng-dev] how to get relayd pid
2015-04-07 20:35 ` Philippe Proulx
@ 2015-04-08 13:42 ` Anand Neeli
0 siblings, 0 replies; 5+ messages in thread
From: Anand Neeli @ 2015-04-08 13:42 UTC (permalink / raw)
Phil,
Thanks a lot for the code
Anand Neeli
On Wed, Apr 8, 2015 at 2:05 AM, Philippe Proulx <eeppeliteloop at gmail.com>
wrote:
> Anand,
>
> On Tue, Apr 7, 2015 at 4:19 PM, Anand Neeli <anand.neeli at gmail.com> wrote:
> > For C
>
> lttng-relayd does not write its PID to a file.
>
> For C, here's the programmatic way of getting the PID of the first
> lttng-relayd process found (inspired by [1]):
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> #include <dirent.h>
>
> /* checks if the string is purely an integer
> * we can do it with `strtol' also
> */
> int check_if_number (char *str)
> {
> int i;
> for (i=0; str[i] != '\0'; i++)
> {
> if (!isdigit (str[i]))
> {
> return 0;
> }
> }
> return 1;
> }
>
> #define MAX_BUF 1024
> #define PID_LIST_BLOCK 32
>
> int *pidof (char *pname)
> {
> DIR *dirp;
> FILE *fp;
> struct dirent *entry;
> int *pidlist, pidlist_index = 0, pidlist_realloc_count = 1;
> char path[MAX_BUF], read_buf[MAX_BUF];
>
> dirp = opendir ("/proc/");
> if (dirp == NULL)
> {
> perror ("Fail");
> return NULL;
> }
>
> pidlist = malloc (sizeof (int) * PID_LIST_BLOCK);
> if (pidlist == NULL)
> {
> return NULL;
> }
>
> while ((entry = readdir (dirp)) != NULL)
> {
> if (check_if_number (entry->d_name))
> {
> strcpy (path, "/proc/");
> strcat (path, entry->d_name);
> strcat (path, "/comm");
>
> /* A file may not exist, it may have been removed.
> * dut to termination of the process. Actually we need to
> * make sure the error is actually file does not exist to
> * be accurate.
> */
> fp = fopen (path, "r");
> if (fp != NULL)
> {
> fscanf (fp, "%s", read_buf);
> if (strcmp (read_buf, pname) == 0)
> {
> /* add to list and expand list if needed */
> pidlist[pidlist_index++] = atoi (entry->d_name);
> if (pidlist_index == PID_LIST_BLOCK * pidlist_realloc_count)
> {
> pidlist_realloc_count++;
> pidlist = realloc (pidlist, sizeof (int) *
> PID_LIST_BLOCK * pidlist_realloc_count); //Error check todo
> if (pidlist == NULL)
> {
> return NULL;
> }
> }
> }
> fclose (fp);
> }
> }
> }
>
>
> closedir (dirp);
> pidlist[pidlist_index] = -1; /* indicates end of list */
> return pidlist;
> }
>
> int main (void)
> {
> int *list = pidof("lttng-relayd");
>
> if (!list || list[0] == -1) {
> fprintf(stderr, "lttng-relayd is not running!\n");
> free(list);
> return 1;
> }
>
> printf("PID of first lttng-relayd found: %d\n", list[0]);
> free(list);
>
> return 0;
> }
>
> [1]
> http://phoxis.org/2013/09/13/find-process-ids-of-a-running-process-by-name/
>
> Hope it helps,
> Phil
>
> >
> >
> >
> >
> > On Wed, Apr 8, 2015 at 1:45 AM, Philippe Proulx <eeppeliteloop at gmail.com
> >
> > wrote:
> >>
> >> Anand,
> >>
> >> On Tue, Apr 7, 2015 at 4:07 PM, Anand Neeli <anand.neeli at gmail.com>
> wrote:
> >> >
> >> > Hi All,
> >> > Is there any better way of getting lttng-relayd PID programmatically?
> >> >
> >> > like sessiond pid is in /var/run/lttng/lttng-sessiond.pid, is there
> any
> >> > similar way to get relayd PID?
> >>
> >> For which language?
> >>
> >> Phil
> >> >
> >> >
> >> > Thanks,
> >> > Anand Neeli
> >> >
> >> > _______________________________________________
> >> > lttng-dev mailing list
> >> > lttng-dev at lists.lttng.org
> >> > http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
> >> >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lttng.org/pipermail/lttng-dev/attachments/20150408/9959b146/attachment-0001.html>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-04-08 13:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-07 20:07 [lttng-dev] how to get relayd pid Anand Neeli
2015-04-07 20:15 ` Philippe Proulx
2015-04-07 20:19 ` Anand Neeli
2015-04-07 20:35 ` Philippe Proulx
2015-04-08 13:42 ` Anand Neeli
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox