1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/*
* Copyright (c) 2012-2013 Red Hat.
* Copyright (c) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* Thread-safe note
*
* Unlike most of the other __pmSend*() routines, there is no wrapper
* routine in libpcp for __pmSendLogRequest() so there is no place in
* the library to enforce serialization between the sending of the
* PDU in __pmSendLogRequest() and reading the result PDU.
*
* It is assumed that the caller of __pmSendLogRequest() either manages
* this serialization or is single-threaded, which is true for
* the only current user of this routine, pmlc(1).
*/
#include <ctype.h>
#include "pmapi.h"
#include "impl.h"
/*
* PDU for general pmlogger notification (PDU_LOG_REQUEST)
*/
typedef struct {
__pmPDUHdr hdr;
int type; /* notification type */
} notify_t;
int
__pmSendLogRequest(int fd, int type)
{
notify_t *pp;
int sts;
if ((pp = (notify_t *)__pmFindPDUBuf(sizeof(notify_t))) == NULL)
return -oserror();
pp->hdr.len = sizeof(notify_t);
pp->hdr.type = PDU_LOG_REQUEST;
pp->hdr.from = FROM_ANON; /* context does not matter here */
pp->type = htonl(type);
#ifdef PCP_DEBUG
if (pmDebug & DBG_TRACE_PDU) {
int version = __pmVersionIPC(fd);
fprintf(stderr, "_pmSendRequest: sending PDU (type=%d, version=%d)\n",
pp->type, version==UNKNOWN_VERSION? LOG_PDU_VERSION : version);
}
#endif
sts = __pmXmitPDU(fd, (__pmPDU *)pp);
__pmUnpinPDUBuf(pp);
return sts;
}
int
__pmDecodeLogRequest(const __pmPDU *pdubuf, int *type)
{
const notify_t *pp;
const char *pduend;
pp = (const notify_t *)pdubuf;
pduend = (const char *)pdubuf + pp->hdr.len;
if (pduend - (char*)pp < sizeof(notify_t))
return PM_ERR_IPC;
*type = ntohl(pp->type);
#ifdef PCP_DEBUG
if (pmDebug & DBG_TRACE_PDU) {
int version = __pmLastVersionIPC();
fprintf(stderr, "__pmDecodeLogRequest: got PDU (type=%d, version=%d)\n",
*type, version==UNKNOWN_VERSION? LOG_PDU_VERSION : version);
}
#endif
return 0;
}
|