Use ENOMEM instead of ENOSPC

From errno(3):
   ENOMEM          Not enough space (POSIX.1)
   ENOSPC          No space left on device (POSIX.1)

when we run out memory the reason is a failed malloc, for which ENOMEM
seems more appropriate.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Return EBADF when trying to read from an uninitalized device

All other functions that check the fd for validity return EBADF, which also makes it
easier to debug if the actual device goes away.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Return -EBADF for functions that need the fd initialized

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
uinput: don't try to set the syspath twice

If we have it, stop searching for it. Otherwise a second device with the
same name would overwrite the first, causing a leak.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Add some gcc/ld optimizations and magic

There are several gcc/ld flags that optimize size and performance without
requiring explicit code changes. In no particular order, this adds:
 - gcc -pipe to avoid temporary files and use pipes during compilation
 - gcc -fno-common avoids putting uninitialized global variables not
   marked as "extern" into a common section. This catches compilation
   errors if we didn't mark global variables explicitly as "extern".
 - gcc -fno-strict-aliasing allows us to use unions for some binary magic.
   Otherwise, -O2 might assume that two different types never point at the
   same memory. We currently don't rely on this but it's common practice
   so avoid any non-obvious runtime errors later.
 - gcc -ffunction-sections and -fdata-sections put each function and
   variable into a separate section. This enables ld's --gc-sections to
   drop any unused sections (sections which aren't referenced from an
   exported section). This is very useful to avoid putting dead code into
   DSOs. We can now link any helper function into libevdev and the linker
   removes all of them if they're unused.
 - gcc -fstack-protector adds small stack-corruption protectors in
   functions which have big buffers on the stack (>8bytes). If the
   stack-protectors are corrupted, the process is aborted. This is highly
   useful to debug stack-corruption issues which often are nearly
   impossible to catch without this.
 - ld --as-needed drops all linked libraries that are not actually
   required by libevdev. So we can link to whatever we want and the linker
   will drop everything which is not actually used.
 - ld -z now, resolve symbols during linking, not during runtime.
 - ld -z relro, add relocation-read-only section. This allows to put
   read-only global variables and alike into a read-only section. This is
   useful for variables that need a relocation and thus cannot be
   explicitly put into a read-only section. This option tells the linker
   to mark them read-only after relocations are done. (that's why -z now
   makes sense in combination with this)

All of these options are common in other open-source projects, including
systemd and weston. Don't ask me why they are not marked as default..

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Remove -g from default GCC_CFLAGS

-g should be set by debugging-options or in the default CFLAGS="", we
shouldn't force it in GCC_CFLAGS.

Reported-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Drop per-device logging function, use per-library one instead

There's no need to have separate logging function for each device created.
More likely, libevdev will be hooked up once into the logging system and
expected to deal with it.

Plus, this allows us to log from the uinput code where we don't
have the context anyway.

Requires a rename to libevdev_set_log_function to avoid ABI breaks, and
while we're breaking the ABI make the logging function more sophisticated
to log line, number, etc.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Log a few errors, specifically application bugs

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Warn if there are multiple devices with the same syspath

Change to the previous code in that we continue looking at devices
even after we've found one. However, this way we can warn
the user when we can't guarantee syspath correctness.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Check for GNU ld and use the flags depending on the outcome

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: David Herrmann <dh.herrmann@gmail.com>
Match the kernel define for SYN_MAX

Will be defined as 0xf in 3.12, see
http://git.kernel.org/cgit/linux/kernel/git/dtor/input.git/commit/?h=next&id=52764fed5049655926bcecaefd52f0a415ceb105

And add the required ifdef guards for kernels before that.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Fix comment typo

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Log to stderr by default

The logging we do use atm inside the library is largely
to spot application errors. Log that to stderr by default so
it doesn't get lost.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
test: test for logging function

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Merge branch 'logging-fix'
Support GNOME Build API

By adding an autogen.sh file with NOCONFIGURE support.

Signed-off-by: Giovanni Campagna <scampa.giovanni@gmail.com>
Reviewed-by: David Herrmann <dh.herrmann@gmail.com>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
configure: add missing comma

Without that, it becomes impossible to build without gcov.

Signed-off-by: Giovanni Campagna <scampa.giovanni@gmail.com>
Reviewed-by: David Herrmann <dh.herrmann@gmail.com>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
build: remove -fstack-protector

If gcc is built without libssp support, it loudly fails linking due to
missing __stack_chk_*() helpers. Unfortunately, gcc isn't smart enough to
disable it automatically.

systemd recently got a CC_CHECK_FLAG_APPEND helper to work around such
issues:
  http://cgit.freedesktop.org/systemd/systemd/tree/m4/attributes.m4

I didn't want to add it now, so let's just drop -fstack-protector. If we
want it, we can try adding it later again.

This partially reverts:
  commit 43752ec17d09b132621a86f5cbc546ca6ab9e977
  Author: David Herrmann <dh.herrmann@gmail.com>
  Date:   Sun Sep 1 17:45:04 2013 +0200

      Add some gcc/ld optimizations and magic

All other gcc/ld options are kept.

Reported-by: Giovanni Campagna <gcampagn@redhat.com>
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>