# devd guide



## balanga (Apr 30, 2018)

Is anyone aware of a guide for using devd in terms of initiating actions through the use of a devd.conf?

I would like to create a /usr/local/etc/devd/test.conf which would trigger some action based on the VID/PID of a particular USB device when it is inserted.  Is that possible?  If so, how would I go about it?

I presume that devd would automatically read every conf file under /usr/local/etc/devd...


----------



## SirDice (Apr 30, 2018)

Why not read the man page? devd.conf(5)



balanga said:


> I presume that devd would automatically read every conf file under /usr/local/etc/devd...


As far as I know, it doesn't.


```
The devd utility reads /etc/devd.conf or the alternate configuration file
     specified with a -f option and uses that file to drive the rest of the
     process.
```
From devd(8).


----------



## tobik@ (Apr 30, 2018)

SirDice said:


> As far as I know, it doesn't.
> 
> 
> ```
> ...


In /etc/devd.conf there is this normally, so it sort of does unless you changed things.

```
options {
        # Each "directory" directive adds a directory to the list of
        # directories that we scan for files.  Files are loaded in the order
        # that they are returned from readdir(3).  The rule-sets are combined
        # to create a DFA that's used to match events to actions.
        directory "/etc/devd";
        directory "/usr/local/etc/devd";
...
}
```



balanga said:


> Is anyone aware of a guide for using devd in terms of initiating actions through the use of a devd.conf?
> 
> I would like to create a /usr/local/etc/devd/test.conf which would trigger some action based on the VID/PID of a particular USB device when it is inserted. Is that possible? If so, how would I go about it?


There are plenty of examples in /etc/devd/*.conf for this, but here's an example from sysutils/solaar:

```
# Allows non-root users to have raw access to Logitech Unifying USB
# Receiver devices.

notify 100 {
    match "system" "USB";
    match "subsystem" "DEVICE";
    match "type" "ATTACH";
    match "vendor" "0x046d";
    match "product" "(0xc52b|0xc532|0xc52f|0xc526|0xc52e|0xc51b|0xc531|0xc517|0xc518|0xc51a|0xc521|0xc525|0xc534)";
    action "chgrp solaar /dev/$cdev && chmod 660 /dev/$cdev";
};
```
After you have created /usr/local/etc/devd/test.conf make sure to restart devd for it to take effect.


----------



## vince66 (Apr 30, 2018)

balanga said:


> Is anyone aware of a guide for using devd in terms of initiating actions through the use of a devd.conf?



Maybe the book:
ABSOLUTE FREEBSD - 2ND EDITION - THE COMPLETE GUIDE TO FREEBSD
could be help you.

/etc/devd.conf , pag 303

You can find it online. My pdf copy is too big and the "attach files" procedure fails.

Let me know.

Bye.


----------



## balanga (Apr 30, 2018)

tobik@ said:


> In /etc/devd.conf there is this normally, so it sort of does unless you changed things.
> 
> ```
> # Allows non-root users to have raw access to Logitech Unifying USB
> ...



I just want to do something simple to test whether it works... For action what would I write to output 'Hello world!' to the screen?


----------



## vince66 (Apr 30, 2018)

I think you need to run a custom shell script that write to output "Hello world!" to the screen.
Arrange what is shown at pag. 259.

action "/home/mwlucas/bin/jobnetwork.sh";

Bye !


----------



## drhowarddrfine (Apr 30, 2018)

vince66 said:


> Maybe the book:
> ABSOLUTE FREEBSD - 2ND EDITION - THE COMPLETE GUIDE TO FREEBSD
> could be help you.
> 
> ...


I'm sure Michael Lucas will be thrilled you posted that the next time he comes to this forum.


----------



## ShelLuser (Apr 30, 2018)

drhowarddrfine said:


> I'm sure Michael Lucas will be thrilled you posted that the next time he comes to this forum.


That was my first reaction as well, but let's not jump to conclusions here. First of all the book is 10 years old (released in 2007). I'm not fully familiar with copyright laws but I do know that over time those can expire, after which it's legally permitted to share such works ("abandonware"). However, to my knowledge this period lies more in the range of 20 or 30 years and even after that it's still a gray area.

But the reason for my comment is that if I search for the name of this book then I can find several sites where you can apparently read it online as well as download it as PDF. And those websites (I won't share links) don't look like your usual shady warez sites to me. For whatever that's worth of course ("looks can be deceiving").

Even so... Don't shoot the messenger. I can honestly fully understand that some people would not immediately associate this with possible illegal actions. Heck, even I'm not fully sure (but I'm lazy and can't be bothered to look this up).

Of course legal or not, it's always much more ethical to buy the book so that the author also gets something in return for all his efforts. Let there be no mistake about that!


----------



## vince66 (Apr 30, 2018)

ShelLuser said:


> That was my first reaction as well, but let's not jump to conclusions here. First of all the book is 10 years old (released in 2007). I'm not fully familiar with copyright laws but I do know that over time those can expire, after which it's legally permitted to share such works ("abandonware"). However, to my knowledge this period lies more in the range of 20 or 30 years and even after that it's still a gray area.
> 
> But the reason for my comment is that if I search for the name of this book then I can find several sites where you can apparently read it online as well as download it as PDF. And those websites (I won't share links) don't look like your usual shady warez sites to me. For whatever that's worth of course ("looks can be deceiving").
> 
> ...



Hei Shell !

What's there illegal about recommending a book that you can read online?
I bought the book (more than ten years ago) and I have a pdf copy for personal use useful to read on the pc (I can not take a library with me every time I move).
Is it illegal to advise you to read some pages of a book online or send two pages to view it? It is then up to the reader to buy it if he finds it useful.

If anything, it's me that I've advertised (free).

The book in question, although having more than ten years, remains a good reference bibliography to be added to the FreeBSD Handbook.

Have you a nice day.


----------



## PMc (May 1, 2018)

balanga said:


> I would like to create a /usr/local/etc/devd/test.conf which would trigger some action based on the VID/PID of a particular USB device when it is inserted.  Is that possible?  If so, how would I go about it?



That should be possible. A simple approach to figure out things:
1. stop the thing (service devd stop).
2. run it in the foreground: devd -d
3. watch the output, while plugging the usb stuff.
4. when satisfied, stop it with ^C, and
5. get back to normal operation (service devd start).

In step 3, one can see things like these:

```
Processing event '!system=DEVFS subsystem=CDEV type=CREATE cdev=ugen2.4'
Processing event '!system=DEVFS subsystem=CDEV type=CREATE cdev=usb/2.4.1'
Processing event '!system=USB subsystem=DEVICE type=ATTACH ugen=ugen2.4 cdev=ugen2.4 vendor=0xabcd product=0x1234 devclass=0x00 devsubclass=0x00 sernum="1312100811244045187502" release=0x0100 mode=host port=5 parent=ugen2.2'
Processing event '!system=USB subsystem=INTERFACE type=ATTACH ugen=ugen2.4 cdev=ugen2.4 vendor=0xabcd product=0x1234 devclass=0x00 devsubclass=0x00 sernum="1312100811244045187502" release=0x0100 mode=host interface=0 endpoints=2 intclass=0x08 intsubclass=0x06 intprotocol=0x50'
Processing event '+umass0 at bus=2 hubaddr=2 port=5 devaddr=4 interface=0 ugen=ugen2.4 vendor=0xabcd product=0x1234 devclass=0x00 devsubclass=0x00 devproto=0x00 sernum="1312100811244045187502" release=0x0100 mode=host intclass=0x08 intsubclass=0x06 intprotocol=0x50 on uhub3'
Processing event '!system=GEOM subsystem=disk type=GEOM::physpath devname=da0'
```

And so on and so on...
These are the events (and their parameters) where one can trigger actions on. With such, looking at the provided examples should give You a start.


----------



## drhowarddrfine (May 1, 2018)

vince66 said:


> What's there illegal about recommending a book that you can read online?


Except when you pay for the right to read it online from the publisher, it's a copyright violation subject to penalty of law. That one can find such sites does not make the activity any less reputable and sites that make such things available are never reputable.


vince66 said:


> If anything, it's me that I've advertised (free).


That and a couple of dollars will get Michael Lucas a cup of coffee somewhere.


vince66 said:


> The book in question, although having more than ten years, remains a good reference bibliography to be added to the FreeBSD Handbook.


It already is.


----------



## balanga (May 1, 2018)

Well I managed to get devd to do someting with my USB modem when it is inserted, ie run usb_modeswitch, which was a major success! But I also want to be able to run `dhclient ue0` and `route change default IP_addr`.

Should I do this via a script, and where should I keep it?


----------



## balanga (May 3, 2018)

So, I've figured out how to run usb_modeswitch, but can't get `dhclient ue0` to run. 

How do I figure out what the problem might be? I guess it could be timing related...


----------



## Phishfry (May 3, 2018)

You might need to run dhclient on a delayed basis. It may be trying to start too soon before your modem gets initialized.
Can you run it by hand ok?


----------



## balanga (May 3, 2018)

Phishfry said:


> You might need to run dhclient on a delayed basis. It may be trying to start too soon before your modem gets initialized.
> Can you run it by hand ok?



Yes, after I insert the modem, devd initiates usb_modeswitch and when I run ifconfig then ue0 is available, so than I need to manually run dhclient ue0. I'd just like this to be done automatically.  How do I run it on a delayed basis?


----------



## Phishfry (May 3, 2018)

Maybe insert a delay at /boot/loader.conf and make/modify the rc.d startup script for dhclient and have it BEFORE login as that will be last to load to give it time for the interface to come up.
kern.cam.boot_delay="10000"
https://lists.freebsd.org/pipermail/freebsd-stable/2017-February/086825.html

You really need the delay after your devd process runs. A delay before is useless.
So bone up on this to figure out what is running when:
https://www.freebsd.org/cgi/man.cgi?query=rcorder&sektion=8


----------



## Phishfry (May 3, 2018)

Well loader boots the kernel so it would be before any rc script, So scratch that. Use this instead.



DutchDaemon said:


> If the network comes up too slowly, take a look at /etc/rc.d/netwait


The netwait script helps handle two situations:
#  - Systems with USB or other late-attaching network hardware which
#    is initialized by devd events.  The script waits for all the
#    interfaces named in the netwait_if list to appear.

/etc/rc.conf settings
netwait_enable="YES"        # Enable rc.d/netwait (or NO)
netwait_timeout="60"        # Total number of seconds to perform pings.
netwait_if="ue0"            # Wait for active link on each intf in this list.

You might need to jigger the /etc/rc.d/netwait file and add an "BEFORE" line with "dhclient" as a value.


----------



## balanga (May 4, 2018)

Many thanks, I have it working now. That covers booting, so I guess if I should happen to remove the modem, the easiest way of restoring the ue0 interface by rebooting, or coming up with script to run after reinserting the modem..


----------



## phoenix (May 4, 2018)

balanga said:


> Many thanks, I have it working now. That covers booting, so I guess if I should happen to remove the modem, the easiest way of restoring the ue0 interface by rebooting, or coming up with script to run after reinserting the modem..



If you have it all configured nicely in /etc/rc.conf (like it should be for boot-time configuration), then it's a simple `service netif restart; service routing start` to reconfigure things.

IOW, you should be configuring network interfaces in /etc/rc.conf and not via devd(8).  If you really want to do it via devd, then have it execute a shell script that runs `service netif restart; service routing start`.  That covers booting and insertion-time configuration.


----------



## balanga (May 5, 2018)

phoenix said:


> IOW, you should be configuring network interfaces in /etc/rc.conf and not via devd(8).



The problem is that I do not have this network interface (ue0) until I run usb_modeswitch() and the only automated way I have found of running it is  using devd(8). 

I'm open to alternative suggestions.


----------



## Phishfry (May 5, 2018)

Well you could write your own rc.d script for usb_modeswitch instead of using devd.

Just make a script that executes your usb_modeswitch command (For Example):
`usb_modeswitch -v 12d1 -p 1505 -V 12d1 -P 1506 -M "55534243123456780000000000000011062000000100000000000000000000" -n 1 -W -I`

You might need to give your script a setting of BEFORE: netwait


----------



## balanga (May 5, 2018)

Phishfry said:


> Well you could write your own rc.d script for usb_modeswitch instead of using devd.
> 
> Just make a script that executes your usb_modeswitch command (For Example):
> `usb_modeswitch -v 12d1 -p 1505 -V 12d1 -P 1506 -M "55534243123456780000000000000011062000000100000000000000000000" -n 1 -W -I`
> ...



How did you come up with that lot? 

I'd be interested in a source for the long number parameter - the rest I can find here...

I was told elsewhere I needed '-J'  -  (HuaweiNewMode)


----------



## Phishfry (May 5, 2018)

It is called MessageContent. Needs the -M switch. Used for disabling the SCSI/Mass Storage Device mode(Windows Drivers).
You might not need it if you are only switching PID.
http://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?t=185

That particular string I found on the internet. It shows how to change the PID and turn off the SCSI Mass Storage Device.
So to discect:
usb_modeswitch -v 12d1 -p 1505 ---The small -v and -p are the devices original VID(VendorID) and PID(ProductID)
-V 12d1 -P 1506 ---This switches it to another PID (PID=Product ID = idProduct in FreeBSD usbconfig)
-M "55534243123456780000000000000011062000000100000000000000000000"  ---MessageContent
-n 1  ---need-responce  = this is for the SCSI disk commands
-W  ---verbose output
-I  ---no-inquire  = do not scan for a SCSI disk device

I deleted a related post because I am not sure where is best to launch usb_modeswitch.
For dynamic hotplug I think devd is best. The rc.d approach would require reload service to re-launch usb_modeswitch.
Not sure though.

I did want to mention that the `netwait_timeout=60` line for /etc/rc.conf is the default.
You can experiment with this as it may take less time to bring up the interface before dhclient.


----------

