Re: [Toybox] [NEW TOYS] hardware rescan tool, FIFREEZE/FITHAW wrapper...(possibly out of scope?)

2014-04-01 Thread Rob Landley
On 03/29/14 02:22, Isaac Dunham wrote:
 I've written a couple of new toys that might well be out of
 scope, but might be interesting or useful for parts.

 I'll describe the commands and the use cases.

Ok.

 1-hwrs:
 This was originally intended as a SUID helper cludge for my
 Acer Aspire One, which has a PCI-bus based SD card reader
 that only shows up when the SD card is inserted. Kernel PCI
 hotplug does not work properly, with the card reader frequently
 failing to show up, dmesg getting spammed when I enable it,
 and the wireless card sporadically failing for reasons that
 seem to be related. However, it works to manually trigger a
 PCI bus rescan. Additionally, neither mdev nor udev (as
 configured in Debian Squeeze)  probe for partitions on slow
 external media, which makes configuring a  user mount in
 /etc/fstab pointless.

They just respond to hotplug events.

The toybox mdev is only halfway implemented, and part of the
reason is devtmpfs exists now, so half of what mdev used to
do is unnecessary. It needs a bit of a redesign, and I haven't
gotten around to looking hard it since.

 I didn't want to have to log in as root every time I plugged
 in an SD card, so I wrote this. It simply triggers a PCI bus
 rescan (hwrs -p), then walks /dev opening every block device
 so as to make the kernel look for partitions (hwrs -b).

This does strike me as an mdev function... except that mdev isn't
currently an suid command. Normal users can't trigger any useful
behavior out of mdev, and trying to add that capability opens fun holes
where mdev is called as /sbin/hotplug and does something based on
environment variables...

I can distinguish suid from actual root via getuid() != geteuid() and
just have TOYBOX_STAYROOT without TOYBOX_NEEDROOT and then do the if
(!geteuid()  getuid()  setuid(getuid())) perror_exit(setuid);
dance that main.c is currently doing. (I'm aware this ignores capability
bits, and am actually pretty happy about that.)

 Default behavior is the same as hwrs -pb.
 The sole reason I wrote it as a new toy was that was the
 quickest way to get it done.

 If you think that this isn't in scope but the probe for
 partitions sounds interesting for mdev, I'd be happy to write
 that.

I think a command line option to tell mdev to give the PCI bus a good
smack and then rescan all the block devices sounds fine. It's not the
default behavior, but an mdev command line option (possibly with a
config option) is probably where this belongs.

 (Coincidentally, I've thought of a way to make mdev
 handle hotplugging with minimal code changes: use the environment
 to find the right uevent file.)

The one I wrote for busybox did hotplugging. The busybox one has changed
a huge amount since then and I want to read up on it to see if people
have expectations. I also want to make it work well with devtmpfs.

I also need to figure out what horrible things strikedevfsd/strike
strikehald/strike systemd is doing in this area, so I can obsolete them.

 2. xfs_freeze:
 This is only a wrapper for the FIFREEZE/FITHAW ioctls.

man 8 fsfreeze

Apparently part of util-linux since 2010 or so.

 The name is a historical artefact; this is a clone of the
 xfs_freeze utility from xfsprogs.

I renamed it to the generic one. (Then hit some strange command line
option parsing bug I need to fix when testing it, so haven't checked it
in yet...)

Thanks,

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [NEW TOYS] hardware rescan tool, FIFREEZE/FITHAW wrapper...(possibly out of scope?)

2014-03-29 Thread Isaac Dunham
I've written a couple of new toys that might well be out of scope, but
might be interesting or useful for parts.

I'll describe the commands and the use cases.

1-hwrs: 
This was originally intended as a SUID helper cludge for my Acer 
Aspire One, which has a PCI-bus based SD card reader that only
shows up when the SD card is inserted. Kernel PCI hotplug does not work
properly, with the card reader frequently failing to show up, dmesg
getting spammed when I enable it, and the wireless card sporadically
failing for reasons that seem to be related.
However, it works to manually trigger a PCI bus rescan.
Additionally, neither mdev nor udev (as configured in Debian Squeeze) 
probe for partitions on slow external media, which makes configuring a 
user mount in /etc/fstab pointless.

I didn't want to have to log in as root every time I plugged in an SD
card, so I wrote this. It simply triggers a PCI bus rescan (hwrs -p),
then walks /dev opening every block device so as to make the kernel look
for partitions (hwrs -b).
Default behavior is the same as hwrs -pb.
The sole reason I wrote it as a new toy was that was the quickest way
to get it done.

If you think that this isn't in scope but the probe for partitions
sounds interesting for mdev, I'd be happy to write that.
(Coincidentally, I've thought of a way to make mdev handle hotplugging
with minimal code changes: use the environment to find the right uevent
file.)


2. xfs_freeze:
This is only a wrapper for the FIFREEZE/FITHAW ioctls.
FIFREEZE makes all writes to a filesystem (specified as a mountpoint!)
block until FITHAW is called.
The point here is to allow a filesystem to be put in a state that's
consistent enough for filesystem backup, checking, or repair, without
preventing reads or killing/waiting for programs that had open files on 
the filesystem.

The name is a historical artefact; this is a clone of the xfs_freeze
utility from xfsprogs.  These ioctls derive from and replace the 
XFS-specific XFS_IOC_FREEZE/XFS_IOC_THAW ioctls, which did exactly the
same thing.  But currently, at least xfs, ext*, jfs, gfs2, nilfs2, and
reiserfs support this ioctl.

I mainly wrote this because I hadn't done much with ioctls.


Thanks,
Isaac Dunham

/* hwrs - rescan hardware
 *
 * Copyright 2014 Isaac Dunham
USE_HWRS(NEWTOY(hwrs, bp, 
TOYFLAG_NEEDROOT|TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN))

config HWRS
  bool hwrs
  default n
  help
usage: hwrs [-bp]

rescan hardware
-b scan block devices for partitions
-p scan PCI busses for new hardware
*/

#define FOR_hwrs
#include toys.h

int blockopen(struct dirtree *node)
{
  int pfd = dirtree_parentfd(node);

  if S_ISBLK(node-st.st_mode) {
close(openat(pfd, node-name, O_RDONLY));
  }
  return dirtree_notdotdot(node)  DIRTREE_RECURSE;
}

void hwrs_main(void)
{
  if (!toys.optflags) toys.optflags = (FLAG_b|FLAG_p);
  if (toys.optflags  FLAG_p) {
int fd = xopen(/sys/bus/pci/rescan, O_WRONLY | O_TRUNC);
write(fd, 1, 2);
close(fd);
  }
  if (toys.optflags  FLAG_b) {
dirtree_read(/dev/, blockopen);
  }
}
/* xfs_freeze.c - freeze or thaw filesystem
 *

USE_XFS_FREEZE(NEWTOY(xfs_freeze, 11f|u|[!fu], TOYFLAG_USR|TOYFLAG_SBIN))


config XFS_FREEZE
  bool xfs_freeze
  default n
  help
usage: xfs_freeze {-f  | -u} /PATH/TO/MOUNT
Freeze or unfreeze a filesystem.
-f  freeze
-u  unfreeze

*/

#define FOR_xfs_freeze
#include toys.h
#include linux/fs.h


void xfs_freeze_main(void)
{
  long p = 1;
  int io_call = toys.optflags  FLAG_f ? FIFREEZE : FITHAW;
  toys.exitval = ioctl(xopen(*toys.optargs,O_RDONLY), io_call, p);
}
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net