Skip to content

Commit

Permalink
Initial working version: just sd_journal_send() for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidstrauss committed Jul 3, 2012
1 parent c8eba0d commit 2f8f2fb
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 2 deletions.
50 changes: 50 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#*#
*.dsw
*.la
*.lo
*.ncb
*.opt
*.plg
*.tgz
*~
.#*
.deps
.libs
Debug
Debug_TS
Makefile
Makefile.fragments
Makefile.global
Makefile.objects
Release
Release_TS
Release_TSDbg
Release_TS_inline
Release_inline
acinclude.m4
aclocal.m4
autom4te.cache
build
config.cache
config.guess
config.h
config.h.in
config.log
config.nice
config.status
config.sub
configure
configure.in
conftest
conftest.c
include
install-sh
libtool
ltmain.sh
missing
mkinstalldirs
modules
scan_makefile_in.awk
*.gcda
*.gcno
run-tests.php
20 changes: 20 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (C) 2012 David Strauss, [email protected]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
journald-php
systemd-php
============

PHP extension allowing native interaction with journald
PHP extension allowing native interaction with systemd and journald

Installation
============

yum install php-devel systemd-devel
phpize
./configure --enable-systemd
make
make install
echo "extension=systemd.so" | sudo tee /etc/php.d/systemd.ini
echo "<?php echo sd_journal_send('MESSAGE=hello world');" | php

Usage
=====

Quick example:

<?php
sd_journal_send('MESSAGE=Hello world.');
sd_journal_send('MESSAGE=Hello, again, world.', 'FIELD2=Greetings!', 'FIELD3=Guten tag.');
sd_journal_send('ARBITRARY=anything', 'FIELD3=Greetings!');

Notes:

* Each argument must be in the form of a KEY=value pair, environmental variable style.
* 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.
* The base message is usually sent in the form MESSAGE=hello. The MESSAGE field is, however, not required.
* Invalid arguments result in nothing recorded in the journal.

Viewing Output
==============

Quick way to view output with all fields as it comes in:

sudo journalctl -f --output=json
54 changes: 54 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

PHP_ARG_WITH(systemd, enable support for systemd,
[ --with-systemd[=DIR] Enable systemd])

if test "$PHP_SYSTEMD" != "no"; then

SEARCH_PATH="/usr /usr/local"
SEARCH_FOR="/include/sd-journal.h"

SYSTEMD_DIR=

if test "$PHP_SYSTEMD" = "yes"; then
AC_MSG_CHECKING([for systemd headers in default path])
for i in $SEARCH_PATH ; do
if test -r $i/$SEARCH_FOR; then
SYSTEMD_DIR=$i
AC_MSG_RESULT(found in $i)
fi
done
else
AC_MSG_CHECKING([for systemd headers in $PHP_SYSTEMD])
if test -r $PHP_SYSTEMD/$SEARCH_FOR; then
SYSTEMD_DIR=$PHP_SYSTEMD
AC_MSG_RESULT([found])
fi
fi

if test -z "$PHP_SYSTEMD"; then
AC_MSG_RESULT([not found])
AC_MSG_ERROR([Cannot find systemd headers])
fi

PHP_ADD_INCLUDE($SYSTEMD_DIR/include)

LIBNAME=systemd-journal
LIBSYMBOL=sd_journal_sendv

if test "x$PHP_LIBDIR" = "x"; then
PHP_LIBDIR=lib
fi

PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
[
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $SYSTEMD_DIR/$PHP_LIBDIR, SYSTEMD_SHARED_LIBADD)
],[
AC_MSG_ERROR([wrong systemd version {44.+ is required} or lib not found])
],[
-L$SYSTEMD_DIR/$PHP_LIBDIR
])

PHP_ADD_EXTENSION_DEP(systemd, sockets, true)
PHP_SUBST(SYSTEMD_SHARED_LIBADD)
PHP_NEW_EXTENSION(systemd, systemd.c, $ext_shared)
fi
11 changes: 11 additions & 0 deletions php_systemd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef PHP_SYSTEMD_H
#define PHP_SYSTEMD_H 1
#define PHP_SYSTEMD_VERSION "0.1"
#define PHP_SYSTEMD_EXTNAME "systemd"

PHP_FUNCTION(sd_journal_send);

extern zend_module_entry systemd_module_entry;
#define phpext_systemd_ptr &systemd_module_entry

#endif
65 changes: 65 additions & 0 deletions systemd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_systemd.h"
#include <systemd/sd-journal.h>

zend_function_entry systemd_functions[] = {
PHP_FE(sd_journal_send, NULL)
{NULL, NULL, NULL} // Sentinel
};

zend_module_entry systemd_module_entry = {
STANDARD_MODULE_HEADER,
PHP_SYSTEMD_EXTNAME,
systemd_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
PHP_SYSTEMD_VERSION,
STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_SYSTEMD
ZEND_GET_MODULE(systemd)
#endif

PHP_FUNCTION(sd_journal_send)
{
struct iovec *iov = NULL;
zval ***args;
int argc, len, i;
char *val;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) {
return NULL;
}

// Allocate sufficient iovector space for the arguments.
iov = safe_emalloc(argc, sizeof(struct iovec), 0);
if (!iov) {
// Should probably raise a more clear error.
RETURN_FALSE;
}

// Iterate through the PHP arguments and fill the iovector.
for (i = 0; i < ZEND_NUM_ARGS() TSRMLS_CC; ++i) {
convert_to_string_ex(args[i]);
val = Z_STRVAL_PP(args[i]);
len = Z_STRLEN_PP(args[i]);
iov[i].iov_base = val;
iov[i].iov_len = len;
}

// Send the iovector to journald.
sd_journal_sendv(iov, argc);

// Free the iovector. The actual strings
// are already managed by PHP.
efree(iov);

RETURN_TRUE;
}

0 comments on commit 2f8f2fb

Please sign in to comment.