Skip to content

Commit 2f8f2fb

Browse files
committed
Initial working version: just sd_journal_send() for now.
1 parent c8eba0d commit 2f8f2fb

File tree

6 files changed

+237
-2
lines changed

6 files changed

+237
-2
lines changed

.gitignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#*#
2+
*.dsw
3+
*.la
4+
*.lo
5+
*.ncb
6+
*.opt
7+
*.plg
8+
*.tgz
9+
*~
10+
.#*
11+
.deps
12+
.libs
13+
Debug
14+
Debug_TS
15+
Makefile
16+
Makefile.fragments
17+
Makefile.global
18+
Makefile.objects
19+
Release
20+
Release_TS
21+
Release_TSDbg
22+
Release_TS_inline
23+
Release_inline
24+
acinclude.m4
25+
aclocal.m4
26+
autom4te.cache
27+
build
28+
config.cache
29+
config.guess
30+
config.h
31+
config.h.in
32+
config.log
33+
config.nice
34+
config.status
35+
config.sub
36+
configure
37+
configure.in
38+
conftest
39+
conftest.c
40+
include
41+
install-sh
42+
libtool
43+
ltmain.sh
44+
missing
45+
mkinstalldirs
46+
modules
47+
scan_makefile_in.awk
48+
*.gcda
49+
*.gcno
50+
run-tests.php

LICENSE.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (C) 2012 David Strauss, [email protected]
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
1-
journald-php
1+
systemd-php
22
============
33

4-
PHP extension allowing native interaction with journald
4+
PHP extension allowing native interaction with systemd and journald
5+
6+
Installation
7+
============
8+
9+
yum install php-devel systemd-devel
10+
phpize
11+
./configure --enable-systemd
12+
make
13+
make install
14+
echo "extension=systemd.so" | sudo tee /etc/php.d/systemd.ini
15+
echo "<?php echo sd_journal_send('MESSAGE=hello world');" | php
16+
17+
Usage
18+
=====
19+
20+
Quick example:
21+
22+
<?php
23+
sd_journal_send('MESSAGE=Hello world.');
24+
sd_journal_send('MESSAGE=Hello, again, world.', 'FIELD2=Greetings!', 'FIELD3=Guten tag.');
25+
sd_journal_send('ARBITRARY=anything', 'FIELD3=Greetings!');
26+
27+
Notes:
28+
29+
* Each argument must be in the form of a KEY=value pair, environmental variable style.
30+
* Unlike the native C version of journald's sd_journal_send(), printf-style substitution is not supported. Perform any substitution using PHP's sprintf() or similar capabilities first.
31+
* The base message is usually sent in the form MESSAGE=hello. The MESSAGE field is, however, not required.
32+
* Invalid arguments result in nothing recorded in the journal.
33+
34+
Viewing Output
35+
==============
36+
37+
Quick way to view output with all fields as it comes in:
38+
39+
sudo journalctl -f --output=json

config.m4

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
PHP_ARG_WITH(systemd, enable support for systemd,
3+
[ --with-systemd[=DIR] Enable systemd])
4+
5+
if test "$PHP_SYSTEMD" != "no"; then
6+
7+
SEARCH_PATH="/usr /usr/local"
8+
SEARCH_FOR="/include/sd-journal.h"
9+
10+
SYSTEMD_DIR=
11+
12+
if test "$PHP_SYSTEMD" = "yes"; then
13+
AC_MSG_CHECKING([for systemd headers in default path])
14+
for i in $SEARCH_PATH ; do
15+
if test -r $i/$SEARCH_FOR; then
16+
SYSTEMD_DIR=$i
17+
AC_MSG_RESULT(found in $i)
18+
fi
19+
done
20+
else
21+
AC_MSG_CHECKING([for systemd headers in $PHP_SYSTEMD])
22+
if test -r $PHP_SYSTEMD/$SEARCH_FOR; then
23+
SYSTEMD_DIR=$PHP_SYSTEMD
24+
AC_MSG_RESULT([found])
25+
fi
26+
fi
27+
28+
if test -z "$PHP_SYSTEMD"; then
29+
AC_MSG_RESULT([not found])
30+
AC_MSG_ERROR([Cannot find systemd headers])
31+
fi
32+
33+
PHP_ADD_INCLUDE($SYSTEMD_DIR/include)
34+
35+
LIBNAME=systemd-journal
36+
LIBSYMBOL=sd_journal_sendv
37+
38+
if test "x$PHP_LIBDIR" = "x"; then
39+
PHP_LIBDIR=lib
40+
fi
41+
42+
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
43+
[
44+
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $SYSTEMD_DIR/$PHP_LIBDIR, SYSTEMD_SHARED_LIBADD)
45+
],[
46+
AC_MSG_ERROR([wrong systemd version {44.+ is required} or lib not found])
47+
],[
48+
-L$SYSTEMD_DIR/$PHP_LIBDIR
49+
])
50+
51+
PHP_ADD_EXTENSION_DEP(systemd, sockets, true)
52+
PHP_SUBST(SYSTEMD_SHARED_LIBADD)
53+
PHP_NEW_EXTENSION(systemd, systemd.c, $ext_shared)
54+
fi

php_systemd.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef PHP_SYSTEMD_H
2+
#define PHP_SYSTEMD_H 1
3+
#define PHP_SYSTEMD_VERSION "0.1"
4+
#define PHP_SYSTEMD_EXTNAME "systemd"
5+
6+
PHP_FUNCTION(sd_journal_send);
7+
8+
extern zend_module_entry systemd_module_entry;
9+
#define phpext_systemd_ptr &systemd_module_entry
10+
11+
#endif

systemd.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifdef HAVE_CONFIG_H
2+
#include "config.h"
3+
#endif
4+
#include "php.h"
5+
#include "php_systemd.h"
6+
#include <systemd/sd-journal.h>
7+
8+
zend_function_entry systemd_functions[] = {
9+
PHP_FE(sd_journal_send, NULL)
10+
{NULL, NULL, NULL} // Sentinel
11+
};
12+
13+
zend_module_entry systemd_module_entry = {
14+
STANDARD_MODULE_HEADER,
15+
PHP_SYSTEMD_EXTNAME,
16+
systemd_functions,
17+
NULL,
18+
NULL,
19+
NULL,
20+
NULL,
21+
NULL,
22+
PHP_SYSTEMD_VERSION,
23+
STANDARD_MODULE_PROPERTIES
24+
};
25+
26+
#ifdef COMPILE_DL_SYSTEMD
27+
ZEND_GET_MODULE(systemd)
28+
#endif
29+
30+
PHP_FUNCTION(sd_journal_send)
31+
{
32+
struct iovec *iov = NULL;
33+
zval ***args;
34+
int argc, len, i;
35+
char *val;
36+
37+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) {
38+
return NULL;
39+
}
40+
41+
// Allocate sufficient iovector space for the arguments.
42+
iov = safe_emalloc(argc, sizeof(struct iovec), 0);
43+
if (!iov) {
44+
// Should probably raise a more clear error.
45+
RETURN_FALSE;
46+
}
47+
48+
// Iterate through the PHP arguments and fill the iovector.
49+
for (i = 0; i < ZEND_NUM_ARGS() TSRMLS_CC; ++i) {
50+
convert_to_string_ex(args[i]);
51+
val = Z_STRVAL_PP(args[i]);
52+
len = Z_STRLEN_PP(args[i]);
53+
iov[i].iov_base = val;
54+
iov[i].iov_len = len;
55+
}
56+
57+
// Send the iovector to journald.
58+
sd_journal_sendv(iov, argc);
59+
60+
// Free the iovector. The actual strings
61+
// are already managed by PHP.
62+
efree(iov);
63+
64+
RETURN_TRUE;
65+
}

0 commit comments

Comments
 (0)